View RSS Feed

Singleton Pattern

Singleton Pattern in Java

  1. Serialization

    by , 03-09-2012 at 07:50 PM
    If the java.io.Serializable interface is being implemented by the Singleton class ( when serialization and deserialization of singleton takes place more than one time) then number of Singleton instances will be created. For avoiding this, implement the readResolve method.
    Java Code:
    	public class Singleton implements Serializable {
    		...
    
    		// This method is called immediately after an object of this class is deserialized.
    		// This method returns the singleton instance.
    ...
    Categories
    Singleton Pattern
  2. Protected constructor

    by , 03-09-2012 at 07:49 PM
    For allowing the singletonís sub-classing, there is a possibility to use the protected constructor. Such technique consists of two main drawbacks which makes the inheritance of a singleton impractical.

    • If constructor is protected then instantiation of the class can be done by calling the constructor from some other class present in the similar package. Creation of the singletonís separate package can avoid it.
    • For using the derived class, change all getInstance calls present
    ...
  3. Early instantiation using implementation with static field

    by , 03-09-2012 at 07:48 PM
    The singleton object gets instantiated in case the class gets loaded & is not when it is used first, in the given implementation. This happens because the instance member has been declared static. Thatís why there is no need of synchronization of any part of the code, in such cases. Class gets loaded once when the uniquity of the object is guaranteed.
    Java Code:
    //Early instantiation using implementation with static field.
    class Singleton
    {
    	private static Singleton
    ...
  4. Lazy instantiation using double locking mechanism

    by , 03-09-2012 at 07:45 PM
    The standard implementation is a thread-safe implementation. However, it is not the best implementation as when you talk about performance, the synchronization is quite expensive.When the object gets initialized, there is no need to check the synchronized method getInstance for synchronization. When singleton object has already been created, just return it without making use of synchronized block.

    When for the first time getInstance() method is called, singleton instance is created. ...
  5. Singleton Pattern Applicability & Examples

    by , 03-09-2012 at 07:43 PM
    Singleton Pattern according to its definition shall be only used if there is exact one instance of class and also when its accessibility is given from a global access point to the clients.Few real situations are given here where singleton is used:

    Logger Classes

    In design of the logger class the singleton pattern is used. Such classes are basically implemented as singletons so that to provide a global access point for all the components of application. For this purpose ...
  6. Singleton Pattern Implementation

    by , 03-09-2012 at 07:39 PM
    A static member is involved in Singleton class, a static public method and a private constructor while doing implementation. This makes a reference to be returned to the static member.

    A getInstance operation is being defined by the Singleton pattern which gives exposure to the unique instance. This unique instance is accessible by the clients. Responsibility of the getInstance() is to create class unique instance if it is not built uptill now and also to return the instance.
  7. Singleton Pattern

    by , 03-09-2012 at 07:38 PM
    Singleton pattern is considered to be a design pattern in software engineering which is used for the implementation of the mathematical singletonís concepts. It is done by making restrictions at the instantiation of one class to 1 object. It is more beneficial in those cases when just 1 object is required to make the coordination of the actions, across the system. This concept is basically generalized to all those systems which are operating efficiently when 1 object is peresnt, or when instantiation ...