ArticleDirectory
- 3.1 identify Singleton Mode
- 3.2 Implementation of lazy and hungry Chinese
3 mode explanation 3.1 recognition Singleton Mode
(1) singleton mode
the singleton mode is used to ensure that this class is created only one class instance during running, in addition, the singleton mode also provides a globally unique access point for this class instance, that is, the getinstance method. This global access point is the same no matter whether it is implemented in the lazy or hungry style.
for the singleton mode, No matter what implementation method is used, it only cares about the creation of class instances and does not care about specific business functions.
(2) the scope of Singleton mode
that is, the scope of Singleton mode?
as you can see from the above implementation, the singleton implemented in Java is the scope of a virtual machine. Because the loading class function is a virtual machine, a virtual machine will create a class instance when loading the empty class through its classloader.
this means that if one machine has multiple virtual machines, each virtual machine should have an instance of this type, but the whole machine has many instances.
note that the singleton mode discussed here is not applicable to the cluster environment. The Singleton mode in the cluster environment is not discussed here, so it is not covered here.
(3) Name of Singleton mode
generally, we recommend that you name the singleton mode method getinstance (), the return type of this method must be the type of the singleton class. The getinstance method can have parameters that are required to create a class instance. Of course, these parameters are not required in most cases.
the name of Singleton mode: Singleton, Singleton, and Singleton mode. Different Translations refer to the same mode.
3.2 Implementation of lazy and hungry Chinese
As mentioned above, the singleton model has two typical solutions: lazy and ELE. Me. How are these two methods implemented? Let's take a look at them. To make it clearer, we only implement the Basic Singleton Control Section and no longer provide the attributes and methods of the examples. We also do not consider thread security issues for the moment, this issue will be analyzed later.
1: The first scheme is lazy
(1) privatization Construction Method
To control only one instance of a class during running, the first task is to control the place where the instance is created, that is, you cannot create a class instance at will, otherwise, you cannot control the number of created instances. Now we want to create a class instance where the class is used, that is, to create a class instance outside the class.
So how can we make it impossible to create a class instance outside the class? It's easy, just private construction! ExampleCodeAs follows:
Private Singleton (){}
(2) how to obtain an instance
The constructor method has been privatized, and the external use of this class does not work. If a class instance cannot be created, the method of this object cannot be called, and function processing cannot be implemented. After thinking, the singleton mode decides to let this class provide a method to return the class instance, so that it can be used outside. The sample code is as follows:
Public Singleton getinstance (){}
(3) convert the method for obtaining an instance to static
There are new problems. The method for getting an object instance is an instance method. That is to say, the client needs to obtain a class instance before calling this method, however, this method is used to obtain the class instance. Isn't it an endless loop? This is not a typical "chicken or eggs first.
The solution is also very simple. Add static to the method so that you can call this method directly through the class without getting the class instance first. The sample code is as follows:
Public static Singleton getinstance (){}
(4) define the attributes of a storage instance
After the method is defined, how can the method be implemented internally? If you directly create an instance and return it, will this not work? The sample code is as follows:
Public static Singleton getinstance () {return New Singleton ();}
Of course, this is not the case. If a new instance is directly added during each client access, there will certainly be multiple instances, and the single-instance function cannot be implemented at all.
What should we do? The Singleton mode comes up with a way to use an attribute to record the class instances you have created. After the first creation, the instance is saved and can be reused later, instead of creating an object instance again. The sample code is as follows:
Private Singleton instance = NULL;
(5) define this property as static
Where should this property variable be used? It must be the first time you create a class instance, that is, it is used in the static method of the previous returned object instance.
This attribute is forced to become a class variable because it needs to be used in a static method. Static is mandatory. That is to say, static is not used here. The sample code is as follows:
Private Static Singleton instance = NULL;
(6) create a control instance
Now you should go to the getinstance Method to Control instance creation. The control method is simple. You just need to determine whether the instance has been created. How to judge? Check whether the attributes of the storage instance have a value. If there is a value, it indicates that the instance has been created. If there is no value, you should create one. The sample code is as follows:
Public static Singleton getinstance () {// first judge whether the instance has a value if (instance = NULL) {// if no value exists, it indicates that no instance has been created, create a // and set this instance to instance = new Singleton ();} // if there is a value or a value is created, use return instance directly ;}
(7) complete implementation
So far, we have successfully solved the requirement that only one instance can be created to control a class during running. The complete code is as follows,For your understanding, the code sequence is marked with comments.The sample code is as follows:
Public class Singleton {// 4: defines a variable to store the created class instance // 5: this variable must be used in static methods, so you need to add static modification Private Static Singleton instance = NULL; // 1: private constructor. Fortunately, the number of instances created in the internal control is private Singleton () {}// 2: define a method to provide a class instance for the client // 3: This method needs to be defined as a class method, that is, to add staticpublic static Singleton getinstance () {// 6: determine whether the storage instance variable has a value if (instance = NULL) {// 6.1: if not, create a class instance, and assign the value to the storage class instance variable instance = new Singleton () ;}// 6.2: if there is a value, directly use return instance ;}}
2: solution 2: Hungry Chinese
Compared with the first solution, the previous privatization constructor provides static getinstance methods to return instances and other steps. The difference lies in how to implement the getinstance method. In this case, the singleton mode also comes up with another method to implement the getinstance method.
Is it necessary to control the creation of only one instance? Is there any ready-made solution? Soon, the singleton mode recalled the static feature in Java:
- Static variables are initialized during class loading.
- Static variables of multiple instances share the same memory area.
This means that in Java, the static variable will only be initialized once, that is, when the class is loaded, and multiple instances will share the memory space, is this the function to be implemented in singleton mode? You really don't have to bother yourself. Based on this knowledge, I wrote the code for the second solution. The sample code is as follows:
Public class Singleton {// 4: defines a static variable to store the created class instance // create a class instance directly here, only one Private Static Singleton instance = new Singleton (); // 1: private constructor. Fortunately, the number of private Singleton () {}// 2 is controlled internally: define a method to provide a class instance for the client // 3: This method needs to be defined as a class method, that is, you do not need to control the code in the static // method. Public static Singleton getinstance () {// 5: directly use the created instance return instance ;}}
Note:This solution uses the static feature, and the first solution is useless. Therefore, the steps of the two solutions are somewhat different. In the first solution, adding static is also counted as one step. In this solution, adding static is not counted as one step.
So when you look at the code of the above two solutions, you can take a closer look at the numbers and follow the order of the numbers to see the differences between the two solutions.
No matter which method is used, only one instance is generated during running, and a global access point for accessing these classes is the static getinstance method.
3: Order of calls in singleton Mode
Because the singleton mode has two implementation methods, its call sequence is also divided into two types. Let's first look at the lazy call sequence, as shown in 2:
Figure 2 lazy Call Sequence
The call order of the hungry Chinese style, as shown in 3:
Figure 3 sequence of hungry calls
To be continued