1. Mode introduction
Definition of pattern
Make sure that a certain class has only one instance, and instantiates it itself and provides this instance to the entire system.
Mode usage scenario
Ensure that a class has only one object, for example, creating an object requires too much resources, such as accessing resources such as IO and databases.
2. UML class diagram
Character introduction:
(1) Client: High-level client.
(2) Singleton: Singleton.
3. Simple implementation of the pattern
public class Singleton { private static Singleton intance; private Singleton() {} public static Singleton getInstance() { /* * At the beginning, when multiple threads come in, they encounter locks, and one thread goes in, which is empty, new object; subsequent threads enter, not empty, no operation; finally return directly * The object is not empty, and multiple threads enter the function, not empty, no locking operation is performed, and directly return */ if (intance == null) { synchronized (Singleton.class) { if (intance == null) { intance = new Singleton(); } } return intance; } } class Singleton1 {// Lazy private static Singleton1 intance = new Singleton1();// Lazy, it loads private Singleton1() {} public static Singleton1 getInstance() { return intance; } } class Singleton2 {// Hungry private static Singleton2 intance; private Singleton2() {} public static Singleton2 getInstance() {// Loads if (intance == null) { intance = new Singleton2(); } return intance; } } class Singleton3 {// private static Singleton3 intance; private Singleton3() {} public synchronized static Singleton3 getInstance() {// Load when used, lock multi-threaded calls have a lock action if (intance == null) { intance = new Singleton3(); } return intance; } } class Singleton4 {// private static Singleton4 intance; private Singleton4() {} public static Singleton4 getInstance() {//Collapse synchronized (Singleton4.class) {// The locking efficiency is similar to 3 if (intance == null) { intance = new Singleton4(); } } return intance; } }4. Pros and disadvantages
(1) Advantages:
A. Since singleton mode has only one instance in memory, memory expenditure is reduced, especially when an object needs to be created and destroyed frequently, and the performance cannot be optimized during creation or destruction, the advantages of singleton mode are very obvious.
B. Since the singleton pattern only generates one instance, the performance overhead of the system is reduced. When the generation of an object requires a lot of resources, such as reading configuration and generating other dependent objects, it can be solved by directly generating a singleton object when the application starts and then permanently resides in memory;
C. Singleton mode can avoid multiple occupations of resources, such as a write file action, because only one instance is in memory, avoid simultaneous write operations on the same resource file.
D. Singleton mode can set global access points in the system, optimize and share resource access. For example, a singleton class can be designed to be responsible for mapping and processing of all data tables.
(2) Disadvantages
A. Singleton mode generally has no interface, and it is difficult to expand. If you want to expand, there is basically no second way to implement it except modifying the code.