Java Singleton Design Pattern Example

As we know design pattern are created to solve a specific problem. Singleton solve the problem of creating only one object of one class. Singleton design pattern is one of the most popular design patterns, it restricts any class to have only one object. And that object is used where ever required. There are certain conditions where we just require only one object of class, like for database connection we require only one object to deal every query related database, instead of creating new object every time. We create only one object of database connection and assign it global access, so it can be accessed anywhere.

Singleton Design Pattern with Eager Initialization

As the name suggest eager initialization means an instance of a class is created much before it is actually required. Mostly it is done on system startup. In an eager initialization singleton pattern, the singleton instance is created irrespective of whether any other class actually asked for its instance or not. This method works fine, but it has one drawback. The instance is created irrespective of it is required in runtime or not. If this instance is not a big object and you can live with it being unused, this is the best approach.

Singleton Design Pattern with Lazy Initialization

As we are very well aware of lazy initialization, it simply follows the logic of lazy person, he never does any thing until its urgently required, same is with lazy initialization. The object of the classes is created only when it is required, not like the above. So, in singleton object is created only one time, when it is required very first time.




Benefits of using Singleton Design pattern

Saves Memory: It’s obvious that whenever we create object of any class, it occupies space in memory, so creating many objects means more space occupied in memory. As singleton class may have only one object, simply it saves memory.

Flexibility: As instantiation process is controlled by class, so class has flexibility to change the instantiation process.

How this Design Pattern Works

Singleton pattern has private constructor, as we know private has accessibility to only its class. So, it restricts to create the object of class out side the class.

Singleton pattern has static member, as static member gets space in memory only once. And this member contains the instance of singleton class.

Lastly Singleton pattern has static factory method, which gives global access to the singleton only object. It is responsible for returning object to the caller so that they can use it.

Real Life Scenario for Singleton Design Pattern

As we all are very familiar with university, its structure, almost everybody has attended university of will attend. While designing system for university management, Vice President class will be singleton. Because there is only one voice president of one university, we can’t make more than one object of Vice President. There will be employee classes, an employee can be a teacher, peon, lab assistant or etc.  All of these can have multiple objects, because there are many teachers, peons, lab assistants in any university. But there is only one Vice President, so by using Singleton pattern we can restrict it.

Diagram of Singleton Pattern

Above Diagram shows and explains implementation of the singleton pattern

Above Diagram shows and explains implementation of the singleton pattern

Eager Singleton Design Pattern Java Example

public class EagerSingleton {
    
    // private static member of class which holds the object of the class.
    private static EagerSingleton instance = new EagerSingleton ();
 
    // private constructor
    private EagerSingleton () {
    }
 
    //public method which is responsible making the global access of object  
    public static EagerSingleton get Instance () {
        return instance;
    }
}

 

Eager Singleton Design Pattern Java Example

public class Lazy Singleton {
   
    // private static member which will hold the object when initialized
    private static Lazy Singleton instance = null;
 
    // private constructor
    private LazySingleton () {
    }
 
    // While any request comes it checks when object is created already or else it creates
    // return the object 
    public static LazySingleton getInstance () {
        if (instance == null) {
            synchronized (LazySingleton.Class) {
                // Double check
                if (instance == null) {
                    instance = new LazySingleton ();
                }
            }
        }
        return instance;
    }
}

 

5 1 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments