Singleton Design Pattern by example

Singleton design pattern is most commonly used among all available java design patterns. It is a Creational patterns. Basic purpose of this design pattern is to maintain only one instance of a class throughout the application. This class may represent a common shared resource, functionality etc.

When to Use ? You should consider singleton design pattern in following scenario

  • A resource is shared across all application component and only one instance of resource is possible.
  • Global variables are being passed across all part of application
  • Multiple instances representing same kind of information.

How to Use ? It is very simple to implement this design pattern. Following are he key requirements and possible solution for implementing singleton design pattern.

  • Only one instance should be available at the time restrict the class instantiation using private scoped constructor.
  • Everybody should get same instance maintain a private static instance variable so that only one instance will be available
  • Easy way to access this instance provide a public static method which returns instance of class. If instance is already there it return the same otherwise create new one.

Putting all together in sequence

  1. Declare a class with a private constructor.
  2. Maintain a self reference and declare as static.
  3. Provide a static method to access this self reference. This method check whether self reference is instantiated or not , if it already instantiated then return the same otherwise  instantiate it.

UML : image

Code :

Singleton Class :

  1: package demo.singleton;
  2: /**
  3:  * Singleton Resource Class
  4:  * @author Yogesh
  5:  */
  6: public class SingletonResource {
  7:     private static SingletonResource resorce =null;
  8: 
  9:     public static SingletonResource getInstance(){
 10:         if(resorce==null){
 11:             resorce = new SingletonResource();
 12:         }
 13:         return resorce;
 14:     }
 15: 
 16:     private SingletonResource(){
 17:         // your initializing code here
 18:     }
 19: }
 20: 


Test Class : 



  1: public class Test {
  2: 
  3:     public static void main(String[] args){
  4:         SingletonResource resource1 = SingletonResource.getInstance();
  5:         SingletonResource resource2 = SingletonResource.getInstance();
  6:         if(resource1==resource2){
  7:             System.out.println("Both are equals");
  8:         } else {
  9:             System.out.println("Both are not equals");
 10:         }
 11:     }
 12: }


Output :  Both are equals

Comments

  1. Good article. Singleton is the most commonly used and most underestimated design pattern.
    Btw, if the singleton implements Serializable, would it still be a singleton? ;-)

    ReplyDelete
  2. Is your Singleton thread-safe?

    ReplyDelete
  3. No, this example is not thread safe.
    I have also posted Thread safe implementation at http://ydtech.blogspot.com/2010/05/concurrency-in-singleton-designing.html.

    ReplyDelete

Post a Comment

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester

Composite Design Pattern by example