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!!

Introductions to REST


Rest architecture style is stateless client server protocol and always it uses HTTP .Rest stands for Representational  State Transfer. In Restful application Rest use HTTP for all curd operations (Create, Read , Update, Delete). So Rest is very lightweight  by comparing other RPC(Remote Producer  Calls) and web services.

Rest API has some advantage like platform independent ,language independent ,run top of HTTP,able to use in the presence of firewalls easily. But there is no inbuilt security features in Rest. But  security features can be added like username/password tokens.

Rest operations  are self contained and each and every request carry all the information to the server  that it need to complete the request. So there is no cookies in good Rest design.

Rest request :-
http://www.mytutorialparadise.com/useraccount/111  -It’ll retrieve the user details of 111th  user.

http://www.mytutorialparadise.com/useraccount?name=Kamal$nic=12234342  It’ll retrieve the details of person which named Kamal and have nic of 12234342.

The server response in Rest is often an XML file but CSV(Commar seperater value) and JSON (JavaScript Object Notations) formats can use.XML is easy to expand and type safe.CSV is more compact and JSON is  easy to parse.

The Hypervisor

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