Singleton Design Pattern

Singleton Design Pattern


Intro :
Meaning of Singleton :
n. a single object individual etc separated or distinguished from a pair
n. (Mathematics) Maths a set containing only one member.

Singleton design pattern deals with restricting instantiation of class to only single object.

Need & Intent of Singleton Design Pattern :

The main need of Singleton design pattern is when we need to instantiate a class only once. This instance can be used from everywhere without calling the directly the constructor each time. Suppose we have UI having one window manager class managing all the internal frames we need this pattern.

The singletons can be lazy loaded. Which is useful in case of the initialisation includes expensive resource loading or database connections.

So overall Singleton design pattern makes code maintainable. Singleton design pattern control the object creation by limiting the number to be only one and for any reason situation changes and we need more objects it allows it.

Implementation using Singleton Design Pattern:

The implementation includes a static member in the Singleton class, a private constructor and a static public method that returns a reference to the static member.
                                                                           


Singleton design pattern example


In singleton design pattern We can implement Singleton in two ways :



1. Initializing the Singleton lazily means we create it only when it is first used

Singleton Design Patten


class Singleton
{
private static Singleton single_instance;
private Singleton()
{
...
}

public static synchronized Singleton getInstance()
{
if ( single_instance == null )
       single_instance = new Singleton(); // Creating when it is first time used.

return single_instance;
}
...
public void doSomething()
{
...
}
}

In above case it gives us global point of access using getInstance() like :
Singleton.getInstance().doSomething();

2. Second method is to have single instance of the class as a static field. Instance gets created at class-loading time by assigning a newly created object to the static field in the field declaration.

Singleton Design Patten


public class Singleton {
//Assigning newly created object to static field.
 private static Singleton _instance =    new MySingleton(); 

 private MySingleton() {
  // construct object . . .
 } 

 public static Singleton getInstance() {
  return _instance;
 }

Both the above Singleton Design Pattern implementations have getInstance() method as static so it cannot be overridden polymorphically. But still we can have a factory class with a method that returns the Singleton instance which will return value at runtime which can be Singleton itself or its subclasses but note that we need to have non private constructor for it.

Applicability of Singleton Design Pattern :
Singleton design pattern is used in lot of places such as to control the database connections or sockets, logger classes, UI frames in GUI appliacations, configuration settings etc.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

1 comments:

  1. How can we create single instance in case of multi threading and how can we avoid duplicate instance to be created in case of cloning?

    ReplyDelete