1. Preface
The singleton pattern may be relatively simple in design mode and the most frequently used design pattern in the work, such as the Read tool class of the system configuration file, which is required to guarantee its instance uniqueness. For example, when you need to invoke the bottom of the computer, you need a Java to use JNI to interact with other languages, you may want to ensure that instances of the Java class are unique.
So what do you need to consider when writing a singleton pattern?
2. Simple implementation:
The principle of General singleton mode implementation:
1. Privatization of instance variables;
2. Privatization of construction methods;
3. Provide a static class-based method to put back to the instance;
Following these three principles, a singleton pattern can be designed in the most simple environment. The simplest implementation of the singleton pattern is the lazy and a hungry man-type, as follows:
A Hungry man type:
publicclass SingleTon { publicstaticnew SingleTon(); privateSingleTon(){} publicstaticgetInstance(){ return instance; }}
Features: Thread safety, which completes the instantiation of classes at the beginning of class loading;
Lazy Type:
publicclass SingleTon { publicstaticnull; privateSingleTon(){} publicstaticgetInstance(){ if(null == instance) new SingleTon(); return instance; }
Features: Thread insecure, to complete the instantiation of the class when the instance is first obtained;
3. Double Lock:
Of course, in the design of a singleton mode, you also need to consider concurrency problems, such as a hungry man-style design, such as the simple, concurrency, there will be problems, may produce multiple instances;
Double locking mechanism:
Public class SingleTon { Public StaticSingleTon instance =NULL;//: Internal definition object lock, when concurrency condition, need to compete SyncLock lock. Private Static FinalObject SyncLock =NewObject ();Private SingleTon(){} Public StaticSingleTongetinstance(){if(NULL= = Instance) {//: A thread that competes SyncLock locks can execute code block content, other threads wait for synchronized(SyncLock) {if(NULL= = Instance) {instance =NewSingleTon (); } } }returnInstance }
Features: can prevent multi-threaded environment due to concurrency caused by multiple instances of the problem;
4.enum Enumeration Implementation Single example:
Of course, even with these methods, in the face of reflection and bytecode technology, there will be no resistance, still can easily produce multiple instances. Enum enum types are introduced in JDK1.5, and depending on the enumeration type, the simple interest pattern can be implemented, and the problem of multiple instances caused by concurrency and emission planes can be solved.
An enum Singleton implementation:
publicenum SingleTon { insatnce; privateSingleTon(){}}
Features: The singleton mode based on Enum can prevent multi-instancing of reflection completely.
Summary: no matter what the implementation of the singleton mode, the purpose is the same, in the operating environment to produce a unique instance. However, the specific implementation of the single-case pattern needs to be based on the specific scenario.
I strongly recommend that enum implement a singleton pattern, because enum prevents the instantiation of classes at various levels. Of course, if the operating environment is determined to be single-threaded, then you can use the simplest a hungry man or lazy implementation, depending on the scenario.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java design Pattern (i) detailed single-case mode