Sunday, January 21, 2018

Design Pattern and Implementation of Singleton Design Pattern in java


What is Desigen  Pattern ?
A design pattern is a well-proved solution for solving the specific problem/task.  We must use the design patterns during the analysis and requirement phase of SDLC(Software Development Life Cycle).
Design patterns ease the analysis and requirement phase of SDLC by providing information based on prior hands-on experiences.
 

Categorization of design patterns:

Basically, design patterns are categorized into two parts:
1.     Core java (or JSE) Design Patterns.
2.     JEE Design Patterns.


What is  Singleton Design Pattern . also write the code?
Singleton Pattern says that just"define a class that has only one instance  
and provides a global point of access to it".
 A class must ensure that only single instance should be created and single object can be used by all other classes.

Advantage of Singleton design pattern

Saves memory because object is not created at each request. Only single instance is reused again and again.
This has advantages in memory management, and for Java, in garbage collection. Moreover, restricting the number of instances may be necessary or desirable for technological or business reasons--for example, we may only want a single instance of a pool of database connections.

Usage of Singleton design pattern

Singleton pattern is mostly used in multi-threaded and database applications. It is used in logging, caching, thread pools, configuration settings etc.

How to create Singleton design pattern?
To create the singleton class, we need to have static member of class, private constructor and static factory method.
1-Static member: It gets memory only once because of static, itcontains the instance of the Singleton class.
2-Private constructor: It will prevent to instantiate the Singleton class from outside the class.
3-Static factory method: This provides the global point of access to the Singleton object and returns the instance to the caller.
Ex:
Ex->
class Demo
{
   private static Demo obj=null;
  
   static
   {
               obj = new Demo();
   }
  
   private Demo()
   {
               System.out.println("Demo");
   }
  
   public static Demo getObject()
   {
               return obj;
   }
}

 public class Demo1 {
             public static void main(String[] args) {
                                    //Demo d1 = new Demo();
                                     Demo d2 = Demo.getObject();
                                     Demo d3 = Demo.getObject();
                                     System.out.println(d2);
                                     System.out.println(d3);
                        }
}


How can be create only 5 object of the class

     A:
    public class MSInt
    {
      private static MSInt instance = null;
      private static int count = 0;

    private MSInt()
    {
       System.out.println("MSINT");
    }

   public static MSInt getInstance()
    {
        if(count < 5){
            instance = new MSInt();
             count++;
             return instance;
        }
        else
        {
            return null;
        }
     }
     public static void main(String[] args)
     {
MSInt m1 =  MSInt.getInstance();
MSInt m2 = MSInt.getInstance();
MSInt m3 = MSInt.getInstance();
MSInt m4 = MSInt.getInstance();
MSInt m5 = MSInt.getInstance();
MSInt m6 = MSInt.getInstance();
System.out.println("m1"+m1);
System.out.println("m2"+m2);
System.out.println("m3"+m3);
System.out.println("m4"+m4);
System.out.println("m5"+m5);
System.out.println("m6"+m6);
    }

}

If you have any concern and any query , leave us comment !!

Thanks for reading
Noeik

0 comments:

Post a Comment