C ++ base 8: Singleton

Introduction of a single case:
intent: to ensure that only one instance of a class, and provide a global access point to access it.
Mainly to solve: a global classes used to create and destroy frequently.
When to use: when you want to control the number of instances, to save system resources.
How to solve: to determine whether the system already has this single case, if it returns, if not then created.
The key code: the constructor is private.

Application examples:
Windows is a multi-process multi-threaded, when a file operation, it inevitably multiple processes or threads simultaneously operating a file of the phenomenon, the processing of all files must be done by a unique instance.
Some devices are often designed as a single Manager embodiment mode, such as a computer printer has two, when both the output will not process the same document printer.

Advantages:
In only one instance in memory, reducing memory overhead, especially frequent create and destroy instances (such as School of Management Home page cache).
Avoid multiple assignment of resources (such as file write operations).

Cons:
no interface, not inherited, and the conflict single responsibility principle, a class should only be concerned about the internal logic, not on the outside how to be instantiated.

Usage scenarios:
the requirements of production unique serial number.
WEB counters, do not always have time to refresh, single cases of congenital cached in the database Riga.
A plethora of objects created need to consume resources, such as I / O connections to the database and so on.
Note: getInstance () method requires the use of synchronization lock synchronized (Singleton.class) to prevent multiple threads into the cause instance is instantiated multiple times.

Sluggard

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

Hungry man

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

Under lazy style how to ensure thread safety?

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

Singleton create thread-safe implementations are those?
Double lock detection / double checking lock (DCL, i.e., double-checked locking)

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

Register-based / static inner classes

public class Singleton {  
    private static class SingletonHolder {  
    private static final Singleton INSTANCE = new Singleton();  
    }  
    private Singleton (){}  
    public static final Singleton getInstance() {  
    return SingletonHolder.INSTANCE;  
    }  
}
Published 69 original articles · won praise 37 · views 180 000 +

Guess you like

Origin blog.csdn.net/xi_gua_gua/article/details/105176712