[C#] Detailed explanation of five singleton patterns

There are five types of singleton patterns:

Hungry mode, lazy mode, double lock lazy mode, static inner class mode, enumeration mode.

1. Hungry Chinese style

public class SingleTon1 {
private static SingleTon1 instance = new SingleTon1();

private SingleTon1() {
}

public SingleTon1 getInstance(){
    return instance;
}

}
Hungry Chinese style, as you can see from the name, it is very hungry, so the instance has been created during initialization. The advantage is that there is no thread safety problem, and the disadvantage is that it wastes memory space.

2. Lazy style (thread-unsafe)

public class SingleTon2 {
private SingleTon2 instance;

private SingleTon2() {
}
public SingleTon2 getInstance(){
    if (instance == null){
        instance = new SingleTon2();
    }
    return instance;
}

}
Lazy style, as the name suggests, is to create an instance when it is used, and then check whether there is an instance when it is used. If there is one, it will be returned, if not, it will be created. However, this will create a thread-unsafe situation. It is possible that multiple threads may enter the if statement at the same time and determine that there is no instance. Thus, two are instantiated, which is no longer a singleton.

3. Double check lock lazy style (thread safe)

public class SingleTon3 {
private SingleTon3 instance;

private SingleTon3() {
}

public SingleTon3 getInstance() {
    if (instance == null) {
        synchronized (SingleTon3.class) {
            if (instance == null) {
                instance = new SingleTon3();
            }
        }
    }
    return instance;
}

}

The double check lock lazy man style combines the advantages and disadvantages of the lazy man style and the hungry man style. Looking at the above code implementation, the characteristic is that a layer of if conditional judgment is added inside and outside the synchronized keyword. The object is only created when it needs to be used. The first judgment INSTANCE == null is to avoid unnecessary locking. When the first The instance is locked and instantiated only when it is loaded for the first time. This not only ensures thread safety, but also improves execution efficiency and saves memory space compared to direct locking.

4. Static inner class singleton

public class SingleTon4 {
private SingleTon4() {
}

private static class SingleTonInside {
    private static final SingleTon4 INSTANCE = new SingleTon4();
}

public static SingleTon4 getInstance() {
    return SingleTonInside.INSTANCE;
}

}
The advantage of static inner classes is that the inner class does not need to be loaded immediately when the external class is loaded. If the inner class is not loaded, the INSTANCE will not be initialized, so it does not occupy memory. This approach not only ensures thread safety, but also ensures the uniqueness of the singleton, while also delaying the instantiation of the singleton.

5. Enumeration singleton

public enum SingleTon5 {
INSTANCE;

public void method() {
    
}

}
Enumerations in Java can have fields and methods just like ordinary classes, and enumeration instance creation is thread-safe. Under any circumstances, it is a singleton. But enumeration classes cannot inherit from other classes. From the code implementation point of view, it is more concise and clear. And it also automatically supports the serialization mechanism to absolutely prevent multiple instantiations. It can be called in the following ways.

SingleTon5.INSTANCE

Summary: Of the five singleton patterns, the most important, best, and most commonly used are the third double-check lock lazy style and the fourth static internal class. Frequently asked questions in interviews, you need to be able to write these two by hand, and understand the other three. If there are any errors, please point them out and make corrections.
——————————————
Copyright statement: This article is an original article by CSDN blogger “Kabuda Xiaoxin” and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source for reprinting Links and this statement.
Original link: https://blog.csdn.net/weixin_56902337/article/details/117171364

Guess you like

Origin blog.csdn.net/KJJfighting/article/details/131723410