One of Java design patterns: Singleton

introduction

Design patterns are summed up by our predecessors in the actual development process to develop routines. From this series of articles began to introduce Java's 23 design patterns. Each design pattern corresponding description will be described as an auxiliary code.

  • What is a singleton
  • Singleton how to write
  • to sum up

First, what is the singleton

The so-called singleton pattern is only to create a unique instance of the class. In many cases, the entire platform only need to have a global object, this will help us to coordinate the behavior of the overall platform. For example, in the backend, the configuration information stored in a file server, the configuration data is read by a unified singleton object, and other objects in the process of re-acquiring service configuration information through the singleton object. This approach simplifies the configuration management in a complex environment is conducive to configure unified data merge;

Second, how to write the singleton pattern

(1) formula lazy

public class Singleton {
     
    private Singleton() {}
    private static Singleton single = null;
    
    public static Singleton getInstance() {
    		//被动创建,需要时加载
           if (null == single) {  
                single = new Singleton();
             }  
            return single;
        }
}

(2) starving formula:
the so-called hungry man is the type to be created when the class loader instance, whether or not consumers current needs. Since the process is determined by the class loader class loader (ClassLoader)to be executed, the process of JVMsynchronization to ensure, so it is inherently thread-safe.

public class Singleton {

//类加载时就已创建实例
 private static final Singleton Singleton = new Singleton()//私有构造方法
 private Singleton (){}private static Singleton getInstance() {
  return Singleton;
 }
}

(3) well-fed mode (lazy mode) - double check the lock DCL (Double Check Lock)

public class Singleton {

    private volatile static Singleton instance = null;
    //私有构造函数
    private Singleton(){}

    public static Singleton getInstance(){
    //1、检查实例是否存在
        if(instance == null){
        //同步块,线程安全的创建实例
			synchronized(Singleton.class){
    //2、再次检查实例是否存在,如果不存在才真的创建实例
                if(instance == null){
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

Third, the summary

There are several single writing mode embodiment, may be properly selected according to the scene. Example summarizes the advantages and disadvantages of a single mode.
Advantages:
1, the only objects in memory, you can save memory space;
2, can be done to avoid frequent create destroying objects, to improve performance;
3, to avoid the occupation of the different objects shared resources, optimization of access to data;
4, for the entire platform provides a global access point.
Disadvantages:
1, does not apply to the object for constantly changing;
2, a single case of abuse will bring some negative issues, in order to save resources such as database connection pool object as a singleton class design, may lead to an object through a shared connection pool the emergence of multi-connection pool overflow;
3, if you instantiate an object for a long time not being used, the system will think the object is garbage to be collected, which may lead to loss of status objects;

Published 88 original articles · won praise 49 · Views 100,000 +

Guess you like

Origin blog.csdn.net/Diamond_Tao/article/details/100618615