In-depth singleton pattern 2

Singleton pattern (Singleton), also known as the singleton pattern, is the simplest pattern in the design pattern.

 

Application scenarios:

1. The log application of the application is generally implemented in the singleton mode. This is generally because the shared log file is always open, because there can only be one instance to operate, otherwise the content is not easy to append.

2. The counter of the website is generally implemented in a singleton mode, otherwise it is difficult to synchronize.

3. The reading of the configuration object of the Web application generally also applies the singleton mode, because the configuration file is a shared resource.

4. The design of multi-threaded thread pools generally adopts the singleton mode, because the thread pool needs to facilitate the control of the threads in the pool

5. HttpApplication is also a typical application of a unit case. People who are involved in the entire request lifecycle should know that HttpApplication is also a singleton pattern, sharing an instance of HttpApplication.

6. The design of the database connection pool generally adopts the singleton mode, because the database connection is a kind of database resource. The use of database connection pools in database software systems is mainly to save the efficiency loss caused by opening or closing database connections. This efficiency loss is still very expensive, because the use of singleton mode for maintenance can greatly reduce this loss. .

7. Windows' Recycle Bin (recycle bin) is also a typical singleton application. During the whole system operation, the recycle bin maintains only one instance.

8. The file system of the operating system is also a specific example of the implementation of the large singleton pattern. An operating system can only have one file system.

9. Windows' Task Manager (task manager) is a very typical singleton mode (this is very familiar

 

Summarized as follows:

  Scenarios where the singleton pattern is applied are generally found under the following conditions:

  (1) In the case of resource sharing, avoid performance or loss caused by resource operation. Apply the configuration as in the log file above.

  (2) In the case of controlling resources, it is convenient to communicate with each other between resources. Such as thread pools, etc.

 

advantage:

1. Instance Control

The singleton pattern prevents other objects from instantiating their own copies of the singleton object, ensuring that all objects have access to a unique instance.

2. Flexibility

Because the class controls the instantiation process, the class has the flexibility to change the instantiation process.

3. Save memory

 

The Singleton pattern generally takes three forms:

1. Sloth

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

 

2. Hungry Chinese

/**
* Some explanations for the first line of static
* java allows us to define static classes inside a class. Such as inner classes (nested classes).
* The class that encloses the nested class is called the outer class.
* In java, we cannot decorate top level classes with static.
* Only inner classes can be static.
*/
public class Singleton{
    //Define an instance of yourself within yourself, only for internal calls
    private static final Singleton instance = new Singleton();

    //This provides a static method for external access to this class, which can be accessed directly
    public static Singleton getInstance(){
        return instance;
    }
    private Singleton(){
       //do something
    }
}

 

 

3. Form of double lock

/**
* This mode will synchronize the content below to inside the if,
* Improve the efficiency of execution, do not need to synchronize every time the object is acquired,
* Only the first time it is synchronized, it is not necessary after it is created
*/
public class Singleton{
    private static Singleton instance=null;
    public static Singleton getInstance(){
        if(instance==null){
            synchronized(Singleton.class){
                if(null==instance){
                    instance=new Singleton();
                }
            }
        }
        return instance;
    }
    private Singleton(){
        //do something
    }
}

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326595657&siteId=291194637