Singleton Design Pattern

Singleton is one of the simple design pattern among all other design pattern that are available in Java. At any point of time Singleton ensures that JVM creates only one instance for particular class. This may be useful when you need to coordinate the action across the system.”

While preparing the class design for model , I made the configuration class instance as member to other classes that may required configuration services. The point here is multiple instance for same configuration class , that is  all other classes will invoke configuration class constructor to create an object (results in creating multiple memory allocation) then each class gets it sown configuration services from its allocated resource that it has created. As long as the model is simple ( that is no of classes that require configuration services is less ) there will not be any problem of resource miss consumption.

singleton implementation

singleton implementation

But in any complex system almost all major classes may require configuration services (or it could be any other specific class services) so creating configuration class as member to all those classes may not be effective solution. This is because for every class there will be new configuration class instance created that is multiple allocation of memory for same configuration class which is wast of resource.

Singleton Class

Singleton Class

Alternative approach for the problem is to create a single object (resource) and make it visible  to others classes that requires access the resources (services). This is Alternative approach is nothing but Singleton (Singleton design pattern) which ensures creating only one instance and provide a public visibility to other classes , that is the instance can be used from anywhere without calling the constructor directly that mean only one time memory allocation (thus optimised resource utilisation ).

Caution :Failing of synchronizing the singleton class instance on implementation part may create serious thread to the model/system. Before trying make sure that singleton design pattern has got its own limitations and pitfalls.

1 Comment

  1. pooja wrote
    at 3:49 PM - 20th July 2012 Permalink

    have u written code to implement singleton design pattern……….please send me the link…

    thanks in advance

Post a Comment

Your email is never published nor shared. Required fields are marked *