1. Singleton mode

definition:

    The singleton pattern is a commonly used software design pattern. In its core structure it contains only a special class called a singleton. The singleton pattern ensures that there is only one instance of a class in the system. That is, a class has only one object instance.

Features:

    1. A singleton class can only have one instance.

    2. A singleton class must create its own unique instance by itself

    3. The singleton class must provide this instance to all other objects

The main points of the singleton pattern:

    1. Private constructor

    2. A private static reference to its own instance

    3. A static public method with its own instance as the return value

The singleton pattern is divided into two types according to the timing of instantiating the object:

    One is the hungry singleton, and the other is the lazy singleton.

 Hungry-style singleton instantiates an object for its own reference when the singleton class is loaded; while lazy-style singleton instantiates the object only when the instance method is called.

code show as below:

Hungry singleton

public class Singleton {  
    private static Singleton singleton = new Singleton();  
    private Singleton(){}  
    public static Singleton getInstance(){  
        return singleton;  
    }  
}

lazy singleton

public class Singleton {
      private static Singleton singleton;
      private Singleton(){}
      public static synchronized Singleton getInstance(){
          if(singleton==null){
              singleton = new Singleton();
          }
          return singleton;
      }
 }

There is also a more common form of the singleton pattern : the form of double locks

public class Singleton{    
    private static volatile Singleton instance=null;    
    private Singleton(){        
    //do something
    }    
    public static  Singleton getInstance(){        
        if(instance==null){            
            synchronized(SingletonClass.class){                
                if(instance==null){
                    instance=new Singleton();
                }
            }
        }        
    return instance;
     }
}

    This mode will synchronize the content below to the inside of if, which improves the efficiency of execution. It is not necessary to synchronize every time an object is acquired. It is only synchronized for the first time, and it is unnecessary after it is created.

   The double-judgment and synchronization method in this mode is much more efficient than the first example, because if a single-level if judgment is used, if the server allows it, assuming there are 100 threads, the time spent is 100*( Synchronous judgment time + if judgment time), and if double if judgment, 100 threads can judge if at the same time, the theoretical consumption time is only the time of if judgment.

        So if you are facing high concurrency, and you are using the lazy mode, the best choice is double judgment and synchronization.

Advantages of the singleton pattern:

1. There is only one object in memory, saving memory space

2. Avoid frequent creation and destruction of objects, which can improve performance

3. Avoid multiple occupation of shared resources

4, can be accessed globally

Disadvantages of singleton pattern:

1. It is difficult to expand, because the getInstance static function has no way to generate an instance of the subclass. If you want to extend, you can only override that class.

2. Implicit use causes unclear class structure

3. Problems that cause program memory leaks

Applicable scene:

        Due to the above advantages of the singleton pattern, it is a design pattern that is used more in programming. The following are scenarios using the singleton pattern:

        1. Objects that need to be frequently instantiated and then destroyed.

        2. Objects that take too much time or consume too many resources when creating objects, but are often used.

        3. In the case of resource sharing, avoid performance or loss caused by resource operation, etc.

        4. In the case of controlling resources, it is convenient to communicate with each other between resources.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325071750&siteId=291194637