1. Single-case mode
1.1. Introduction: As far as I know, the singleton mode should belong to a simple design pattern in Java design mode, as the beginning of the design pattern, first from the simple, to slowly understand the design pattern.
1.2. Definition: Single case mode, is a common software design mode. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that there is only one instance of a class in the system. That is, a class has only one instance of an object.
2. Characteristics of the Singleton mode:
2.1. A class can always have only one instance.
2.2. The operation to create the object needs to be done by this class.
2.3. The class must leak an interface (method) that can access an instance of the class to the outside world.
3. Key points for creating a singleton pattern:
3.1. Private construction methods.
3.2. A private static reference to your own instance.
3.3. A public interface (method) that takes its own instance as the return value.
4. Classification:
4.1. A hungry man type single example: A Hungry man single example when a singleton class is loaded, it instantiates a reference that an object is given to itself.
4.1.1. code example: Hsingleton.java
1 PackageCom.cn.singleton;2 /**3 * A hungry man type Single case class4 * @authorAdministrator5 *6 */7 Public classHsingleton {8 Private StaticHsingleton Singleton =NewHsingleton ();9 PrivateHsingleton () {}Ten Public StaticHsingleton getinstance () { One returnSingleton; A } -}View Code
4.2. Lazy-type singleton: Lazy Singleton is the instantiation of an object when the instance method is invoked.
4.2.1. Code implementation: Lsingleton.java
1 PackageCom.cn.singleton;2 /**3 * Lazy Single-case mode4 * @authorAdministrator5 *6 */7 Public classLsingleton {8 Private StaticLsingleton Singleton;9 PrivateLsingleton () {}Ten Public Static synchronizedLsingleton getinstance () { One if(Singleton = =NULL) ASingleton =NewLsingleton (); - returnSingleton; - } the}View Code
4.3. Double lock mode: This mode writes the synchronization content to the if inside, improves the execution efficiency, does not have to synchronize every time obtains the object, only then synchronizes only first, does not have the necessity after the creation
4.3.1. Code implementation: Dlsingleton.java
1 PackageCom.cn.singleton;2 /**3 * Double Lock mode single case4 * @authorAdministrator5 *6 */7 Public classDlsingleton {8 Private StaticDlsingleton Singleton;9 PrivateDlsingleton () {}Ten Public StaticDlsingleton getinstance () { One if(Singleton = =NULL){ A synchronized(Dlsingleton.class) { - if(Singleton = =NULL) -Singleton =NewDlsingleton (); the } - } - returnSingleton; - } +}View Code
5. Advantages and Disadvantages:
5.1. Advantages:
A. There is only one object in memory that saves memory space.
B. Avoid frequent creation of destroyed objects, which can improve performance.
C. Avoid multiple uses of shared resources.
D. can be accessed globally.
5.2. Disadvantages:
A. The extension is difficult because the getinstance static function has no way to generate instances of subclasses. If you want to expand, only rewrite that class.
B. Implicit use causes the class structure to be unclear.
C. An issue that causes program memory leaks.
6. Application Scenario:
A. Objects that require frequent instantiation and destruction.
B. Objects that are too time consuming or resource intensive to create objects, but are often used.
C. In case of resource sharing, avoid performance or loss caused by resource operation
D. In the case of resource control, facilitate mutual communication between resources.
Java design pattern----Singleton mode