Java design Pattern--Singleton mode
- The principle of the classic single case pattern
1.1 Meaning: Some classes, in practical applications, can have only one object. may not use, and can only use this object, such as printers, graphics card log objects, etc., so that does not cause conflicts, so that the results are consistent.
1.2 Problem Solving: is it possible to consider static variables as a way to achieve (privatization of construction methods)? or negotiate global variables between programmers.
1.3 Single-case mode: ensure that a class has at most one instance and provides a global access point.
1.4 actual operation: using private own internal calls
Singleton Pattern class Diagram:
Case: Chocolate Factory
In the ordinary class, because there is only one factory, but there are multiple instances, this can lead to inconsistent state, you may consider using singleton mode.
Public classChoclatefactory {Private Booleanempty; Private Booleanboiled; Private StaticChoclatefactory uniqeinstance=NULL ; Privatechoclatefactory () {Empty=true; Boiled=false; } //Constructing class Objects Public Staticchoclatefactory getinstance () {//is empty and the colleague remains unchanged when called if(uniqeinstance==NULL) {uniqeinstance=Newchoclatefactory (); } returnuniqeinstance; } Public voidFill () {if(empty) {//Addempty=false; Boiled=false; } } Public voidBoil () {if((!empty) && (!boiled)) { //HeatingBoiled=true; } } Public voidDrain () {if(!empty&&boiled) { //Heatingempty=true; } }}
During use, some problems can occur, such as multithreading (two threads get two objects and cannot guarantee uniqueness).
Workaround:① Add a sync lock in getinstance (to ensure that one thread finishes executing the next thread)
Public Static synchronized Choclatefactory getinstance ()
However, the synchronization lock consumes resources, it can solve the problem, but it is not recommended.
② "Eager" to create an instance
Private Static Choclatefactory uniqeinstance=New choclatefactory ();
When the runtime is called, the object is created directly, and getinstance () returns directly, compensating for the resource consumption of the synchronization lock, but also creating an object-consuming resource if the factory is not used.
③ double check Plus lock method:
//volatile compilers use to handle multithreading security Private volatile StaticChocolatefactory uniqeinstance=NULL ;//Constructing class Objects Public Staticchocolatefactory getinstance () {//is empty and the colleague remains unchanged when called if(uniqeinstance==NULL){
The first thread executes here, and is empty. If the second thread is also here, it should also be empty, and the new object will be returned. They can only enter one
Gets to an object, returns, and when another thread re-enters, the judge is not empty and jumps out (as soon as it enters once, it will not be empty). synchronized(Chocolatefactory.class){ if(uniqeinstance==NULL) {uniqeinstance=Newchocolatefactory (); } } } returnuniqeinstance; }
2. Classification of singleton patterns
There are many forms of the singleton pattern, the common lazy type, the Hungry Han style, the registration type
Lazy: means that the creation time of the object reserved by the class is done when needed (only when it is created);
Hunger: In contrast to the lazy type, the objects reserved by the class are created on the first visit (consistent with the "eager" creation method above);
Java design Pattern--Singleton mode