background
when you guarantee that there is only one instance, do you think of a singleton pattern
1. Intention to use
Ensure that there is only one instance, such as a help form, that there is no need to maintain multiple instances
2. Life Example
The earth has only one (do not know the embodiment of a single case)
3. Java Example (framework, JDK, JEE)
Examples of singleton patterns are numerous, and a very common example is that the spring controller is a singleton, but if the servlet is not a singleton according to the single-mode encoding rule, he does single-instance, meaning that the Web container maintains only one Servlet object. In fact, it can be equated with the results of a single case. Also, do not see the class name. getinstance () thought it was a singleton pattern, such as calendar.getinstance () he is not a singleton, and the objects are different instances.
/** * gets a calendar using the default time zone and locale. the * <code>calendar </code> returned is based on the current time * in the default time zone with the default locale. * * @return a Calendar. */ public static calendar getinstance () { calendar cal = createcalendar ( Timezone.getdefaultref (), locale.getdefault ()); cal.sharedzone = true; return cal; }
4. Pattern class Diagram
5. Advantages of the Model
Singleton mode (Singleton), which guarantees that a class has only one instance, and provides a global access point to access it.
usually we can have a global variable that makes an object accessible, but it doesn't prevent you from instantiating multiple objects. One of the best ways is to have the class itself responsible for preserving its only instance. This class guarantees that no other instance can be created, and it can provide a way to access the instance.
Because the Singleton class encapsulates its only instance, it can strictly control how the client accesses it and when it accesses it. Simply put, controlled access to the only instance.
One of the most critical points in single-case mode writing is to ensure that the method is private and that only one object instance is guaranteed when the instance is fetched.
6. Comparison with similar models
There are two main problems to be solved in the singleton mode: Global access and instantiation control problems.
The static initialization method is to instantiate itself when it is loaded, and is called the A Hungry Man type singleton class by the image. This is an object that is instantiated on a class-loaded basis and consumes system resources in advance.
the original singleton mode of processing the first time you are quoted, you will instantiate yourself, so called the lazy Singleton class. This approach faces the security problem of multi-threaded access, which requires double locking to ensure security.
Referring to the Singleton, I think he and static class still want to say, such as mathematical class This static class, static class one load, all methods will be loaded into memory, he is a stateless class, that is, no instance has its own unique properties, this class is generally the tool class; However, the Singleton is different, He kept an instance of himself, and this instance was maintained in its own state, so it was different; In addition, because static all methods are static, so, can not inherit, but the singleton is not the same, the Singleton method is also not static, he can inherit. The common denominator is that the construction method is privatized.
7. Code implementation
1) Ordinary single case (general single case as long as the construction method is private), this singleton is unsafe, because multi-threaded access may lead to more than one instance
/**
* Singleton model (Lazy singleton) * @author: heweipo * @version 1.00 * */public class singleton { private static object lock = new object (); private static singleton instance; private singleton () { // do nothing } public static singleton getinstance () { // can actually be in the outermost yoke of the method, but that will inevitably affect performance, because only one occurrence of the empty case if (instance == null) { // Shackle Lock, before this is possible there are several methods that have not initialized the singleton when entered, so there is also a judgment synchronized (lock) { // If the reference is empty, then instantiate the singleton, why should we judge it here? The goal is to prevent multiple methods from entering if (instance == null) { //Singleton first instantiation instance = new singleton (); } } } return instance; }}
2) Multi-threaded single case, double-check Locking double lock (when the first time is referenced, it will instantiate itself, so also known as the Lazy Singleton)
/**
* Singleton model (Lazy singleton) * @author: heweipo * @version 1.00 * */public class singleton { private static object lock = new object (); private static singleton instance; private singleton () { // do nothing } public static singleton getinstance () { // can actually be in the outermost yoke of the method, but that will inevitably affect performance, because only one occurrence of the empty case if (instance == null) { // Shackle Lock, before this is possible there are several methods that have not initialized the singleton when entered, so there is also a judgment synchronized (lock) { // If the reference is empty, then instantiate the singleton, why should we judge it here? The goal is to prevent multiple methods from entering if (instance == null) { //Singleton first instantiation instance = new singleton (); } } } return instance; }}
3) static initialization (when self is loaded, it instantiates itself, known as the A Hungry man type singleton class, pre-occupies system resources, but it is more recommended to use this, concise)
/** * Singleton model (Hungry sweat type Singleton) * @author: Heweipo * @version 1.00 * */public class Singleton {//class when loading to instantiate object private static Si Ngleton instance = new Singleton (); Private Singleton () {//Do not} public static Singleton getinstance () {//return directly to a single return instance;}
The design pattern of the Plutella Xylostella--Singleton mode