Tuesday, September 9, 2014

Singleton and Multiton pattern


Singleton is creational design pattern that is responsible to create only a single instance of the specified class. and provide a global access to it.

Multiton pattern is similar to Singleton but creates a map of named instances as key-value pairs of the specified class.


Singleton Example:












Client

public class MultitonUser {

   
    public static void main(String[] args) {
   
        Singleton.getInstance("FirstVehicle").otherOperation();
        Singleton.getInstance("SecondVehicle").otherOperation();
      
    }
   
}
 Multiton Example:





Client

public class MultitonUser {

   
    public static void main(String[] args) {
   
        Multiton.getInstance("FirstVehicle").otherOperation();
        Multiton.getInstance("SecondVehicle").otherOperation();
      
    }
   
}

1 comment:

Anshuman Gaonsindhe said...

Hi Sajith,

Multiton Program doesn't go well when more than 2 threads call the getInstance.