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.
class Demo
   private static Demo obj=null;
               obj = new Demo();
   private 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();

How can be create only 5 object of the class

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

    private MSInt()

   public static MSInt getInstance()
        if(count < 5){
            instance = new MSInt();
             return instance;
            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();


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

Thanks for reading


Post a Comment