Uses Of Singleton Design Pattern In Java

Posted By : Avaneesh Kumar Verma | 06-Jun-2018

The Singleton Design pattern is a creational Design pattern it is one of the Simplest design patterns in java. According to the Singleton Design pattern, the class provides the same single object for each call--that is, Singleton Design pattern restricts the instantiation of a class to one object and gives a global purpose of access to that class. So the class is responsible for creating an object and also ensures that only a single object should be created for each client call for this object. this class doesn't allow a direct instantiation of an object of this class. it allows you to get an object instance only by an exposed static method.

 

Basic steps for implementing the Java Singleton class

Singleton class - Singleton class means only one instance of that class can be created.

Private Constructor - Constructor of that class has to be made as private to avoid instantiation from external classes.

Private Static variable - Static variable to store the instance for that class.

Declare a public Factory method that returns the instance of that class.

 

You can create a singleton pattern using two forms, as listed here ;



 Early instantiation: Creation of instance at load time.

 

 

 

						public class EagerInitializationDemo {
                
                        private static final EagerInitializationDemo instance = new EagerInitializationDemo();
                                private EagerInitializationDemo(){
                            
                                }
                        public static EagerInitializationDemo getInstance(){
                                return instance;
                        }
                }
        

Lazy instantiation: Creation of instance when required.

               
                public class SingletonDemo {

                    private SingletonDemo() {
                    }

                    private static SingletonDemo singleton = null;

                    public static SingletonDemo getInstance() {
                        if (singleton == null) {
                            singleton = new SingletonDemo();
                        }
                        return singleton;
                    }

                } 
        

 

Benefits of the Singleton pattern:


-It provides controller access to crucial (usually heavy object ) classes,such as the connection class for DB and the SessionFactory class in Hibernate -it Saves heaps of memory - it is a very efficient design for multithreaded environments - it is more flexible because the class controls the instantiation process, and the class has the flexibility to change the instantiation process - it has low latency common problems where you should apply Singleton pattern: This Singleton pattern solves only one problem-- if you have a resource that can only have a Single instance, and you need to manage that single instance, then you need a singleton. Normally if you want to create a database connection with the given configuration in the distributed and multithreaded environment, it might be the case that every thread can create a new database connection with different configuration object, if you don't follow the Singleton design. It is mostly used in multithreaded, database, logging, caching, thread pool, configuration settings and so on.

 

About Author

Author Image
Avaneesh Kumar Verma

Avaneesh is a Software developer Having Knowledge Java , J2EE ,Data Structure and Algorithm ,SQL.

Request for Proposal

Name is required

Comment is required

Sending message..