singleton-designpattern-digitalpulsion-younes-rabdi

Singleton Design Pattern

Singleton

Type : Creational design pattern

Intent

Singleton is a creational design pattern that is used when you need to ensure that a class has only one instance, the singleton class should also be providing a global access point to this instance.

Applicability

Use the Singleton pattern when

  • there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
  • when the sole instance should be extensible by sub-classing, and clients should be able to use an extended instance without modifying their code.

Structure

understand Sigleton design pattern

Members

static getInstance() : We hide the constructor and we only provide this static function to get an instance of Singleton object.

instance attribute : This private attribute will hold the only instance of Singleton object.

Outcome

  • Controlled access to sole instance. Because the Singleton class encapsulates its sole instance, it can have strict control over how and when clients access it.
  • Reduced name space. It avoids polluting the name space with global variables that store sole instances.
  • Permits a variable number of instances. The pattern makes it easy to change your mind and allow more than one instance of the Singleton class. Moreover, you can use the same approach to control the number of instances that the application uses. Only the operation that grants access to the Singleton instance needs to change.

Example Code

“Be a legend” coder designs

mockup-231fb4ff.jpg

Be a Legend and code some Java

$16.95$19.95 Select options
mockup-bf7c6882.jpg

Be a Legend and code some JavaScript

$16.95$19.95 Select options
mockup-a6b73d32.jpg

Be a Legend and code some C#

$16.95$20.95 Select options
mockup-226f098c.jpg

Be a Legend and code some C++

$16.95$20.95 Select options

Leave A Comment

Your email address will not be published. Required fields are marked *