Sunday, April 7, 2013

Singleton Design Pattern Using Java Example


The idea of the Singleton design pattern is that it ensures there is only one instance for a particular class and provide a global point of access to it by encapsulating just in time initialization or initialization on first use.

So there should be restriction for instantiation.

Now I’m going to explain how to implement the Singleton design pattern using Java example. Wait a minute. There are should be only one instance and a global access point in the program. Keep in mind always you work with the Singleton design pattern.

public class MySingleton{
    private static MySingleton singletonInstance;

    private MySingleton(){
    }

    public static MySingleton getSigletonInstance(){
        if(singletonInstance == null)
            singletonInstance = new MySingleton();
        return singletonInstance;
    }
}

Using private constructor, outside classes can not instantiate the class. So if we want to create an object, it should be within the class. By getSigletonInstance(), it provide a global access point to outsiders. It makes sure that there is only one instance by checking is there an instance, only there isn’t, it makes an instance. If there is an instance it returns the previous instance.  


What about we are working in a multithreading environment? If we use above program as it is, there may be instantiated some objects. So we have to prevent making more instance by make getSigletonInstance() method synchronize using synchronize key word.

public class MySingleton{
    private static MySingleton singletonInstance;

    private MySingleton(){
    }

    public static synchronized MySingleton getSigletonInstance(){
        if(singletonInstance == null)
            singletonInstance = new MySingleton();
        return singletonInstance;
    }
}

Now program is ok for multithreading environment. wait !!! What about performance? You should always think about the performance of the program. When you make the getSigletonInstance() method synchronize, all threads that want to access the method are in a queue and wait for acquire lock. So it is more time consuming and performance really go down.


There are some solutions for that. Did you think about an instance is created at class loading time? If not here it is.
public class MySingleton{
       private static final MySingleton singletonInstance = new MySingleton();
    private MySingleton(){
    }

    public static MySingleton getSigletonInstance(){
       return singletonInstance;
    }
}

By final instance, it ensures that  singletonInstance instance   can not be redefined. So there is only one instance. By making that instance static that instance is created at the class loading time. So when MySingleton is deployed, at that time singletonInstance instance is created.There is no synchronized method so it performs well.

Another way to do this is Enum and it is the best way. The use of an enum is very easy to implement and has no drawbacks regarding serializable objects because it is thread safe. Java program guarantee that it has only one instance. Since there is global access point it protects the singleton.

public enum MySingleton {
    INSTANCE;
    public void execute (String arg) {
            //... perform operation here ...
    }
}

Practically Singleton pattern is used in Database connector class. Mainly Singleton pattern use in resource saving activities.

According to your requirement you are free to select a way to implement the Singleton.

Your comments give encourage to grow up. Best of luck!!

No comments:

Post a Comment

The Hypervisor

 The hypervisor is a piece of software that runs on top of hardware infrastructure that creates a virtualization platform. The hypervisor a...