Blog

  • Singleton Design Pattern

    Posted by Ankita Singh | Last Updated: 18-Dec-16

    Hey Guys in this blog iI will tell you the use of the singleton design pattern.It is one of the most simple design patterns in terms of modelling but most controversial in terms of the complexity of usage.In java singleton pattern will ensure that there is only one instance of a class is created in JVM It is used to provide global point of access to the object.This design pattern is used in conjunction with the FACTORY DESIGN PATTERN.

    Structure:

    See below given diagram of the Singleton Class.

    • Static member - this contain the instance of singleton class
    • Private Constuctor - this will prevent anybody else to instantiate the singleton class
    • static public method - this provides the global point of access to the singleton object and reurn the instance of the client calling class.

    public class SingletonExample {

        private static SingletonExample singletonInstance;
    
        private SingletonExample() {
        }
    
        public static SingletonExample getSingletonInstance() {
            if (null == singletonInstance) {
                singletonInstance = new SingletonExample();
            }
            return singletonInstance;
        }
    
        public void printSingleton() {
            System.out.println('Inside print Singleton');
        }
    }

    the above code is lazy initalization process.When this class will be called by calling  SingletonExample.getSingletonInstance().printSingleton(); then only first time the instance will be created.During the second time calls will be referring to the same object and getSingletonInstance() will return same instance of SingletonExample class which was created during the first time.

    You can test this by adding print statements

    public static SingletonExample getSingletonInstance() {
        if (null == singletonInstance) {
            singletonInstance = new SingletonExample();
            System.out.println('Creating new instance');
        }
        return singletonInstance;
    }

    if now we call a singleton class from the client class as :

    SingletonExample.getSingletonInstance().printSingleton();
    SingletonExample.getSingletonInstance().printSingleton();  
    SingletonExample.getSingletonInstance().printSingleton();    
    SingletonExample.getSingletonInstance().printSingleton();
    

    this will print :

    Creating new instance  
    Inside print Singleton
    Inside print Singleton
    Inside print Singleton
    Inside print Singleton

    This thing works perfectly in a single threaded environment and processess the result faster because of lazy initialization.However the above code create problem in multithreaded environment as in this situation multiple threads can possibly create multiple instances of the same SingletonExample Class if they try to access the  getSingletonInstance() method at the same time.To prevent this we must use some locking mechanism so that the second thread can not use this getInstance(); method. until a first thread completed a process.Since it is a static class variable it is stored in the premgem space of the heap same is with the getSingleInstance();  as well as it is static too.In the multithreading environment to prevent each thread to create another instance of singleton object and thus creating concurrency issue we will need to use locking mechanism.And can acheive it by using synchronized keyword. 

    e.g.    

    public static synchronized SingletonExample getSingletonInstance() 
        if (null == singletonInstance) {      
            singletonInstance = new SingletonExample();
        }   
        return singletonInstance;
       
    }

    THANKS

Tags: SingletonPattern

Mobile Applications

Video Content

Bigdata & NoSQL

SaaS Applications

Miscellaneous

Archives


Alexa Certified Site Stats for www.oodlestechnologies.com