Home > Back-end >  The singleton pattern in Java
The singleton pattern in Java

Time:12-05

the definition of singleton pattern:
- in mathematics and logic, the singleton is defined as "one and only one element of the set",
- the definition of singleton pattern first appeared in the "design patterns" (Addison weiss, 1994) : "to ensure a class has only one instance and provide a global point of access to access it,"
- the singleton pattern in Java definition: "a class and only one instance, and instantiate itself to provide the system as a whole,"

Java singleton pattern example

Public class Singleton {
Private Singleton () {
}
Private static volatile Singleton instance=null;
Public static Singleton getInstance () {
If (the instance==null) {
Synchronized (Singleton class) {
If (the instance==null) {
instance=new Singleton();
}
}
}
return instance;
}
}


usually singleton pattern in the Java language, there are two way of building:

LanHanShi - thread unsafe : the most basic way of implementation, the thread context singleton, don't need to share to all threads, also do not need to add the synchronize locks, such as to improve the performance,
LanHanShi - thread safe: with the synchronize and ensure the safety of the thread on the basis of the mode of idlers, relative performance is low, most of the time don't need synchronous
the hungry way, refers to the global singleton instance when loading a class building, [2]
double check the lock type, on the basis of LanHanShi use the synchronize keywords and volatile keyword to ensure that created for the first time no competition between threads and produce multiple instances, only created for the first time synchronization, relatively high performance
register , as create the global properties of classes exist, create classes to be loaded is created when the
enumeration, Java enumeration class itself is also a singleton

the advantages and disadvantages of the singleton pattern:

Advantages of
One instance control
Singleton pattern can prevent other objects instantiated its own copy of the singleton, to ensure that all objects to access the only instance,
Second, flexibility
Because class controls the instantiation, the class can be instantiated flexible change process,

shortcomings
A, overhead
Though few in number, but if every object request references to check whether there is any instance of the class, will still require some overhead, can solve this problem by using the static initialization,
Two, may develop confused
Using a singleton (particularly defined in the class library of objects), the developer must remember that we cannot use the new keyword instantiation object, because the code may not be able to access library, so application developers may accidentally find themselves unable to instantiate such directly,
Three, object lifetime
Can't solve the problem of removing a single object, in providing memory management language (for example based on. The language of the.net Framework), only a singleton class that can lead to instance is cancelled, because it contains a reference to the instance of private, in some languages (such as c + +), other classes can delete the object instance, but it leads to a suspended as part of a singleton class

CodePudding user response:

  • Related