Reproduced from the Internet.
Singleton Mode Overview
Let's simulate implementing the Windows Task Manager, assuming that the Task Manager's class name is TaskManager, and that the TaskManager class contains a number of member methods, such as the constructor TaskManager (), Show the Process method displayprocesses (), show the service method displayservices (), etc., the schematic code for this class is as follows:
c Lass TaskManager public TaskManager () {...}//Initialization window public void displayprocesses () {...}//show process public void displayservices () {...}//Display service ... |
In order to achieve the uniqueness of the Windows Task Manager, we refactor the above classes in three steps as follows:
(1) Since a new object is generated each time the TaskManager class is instantiated with the New keyword, in order to ensure the uniqueness of the TaskManager instance, we need to prohibit the outer class from using new directly to create the object, so we need to The visibility of the TaskManager constructor is changed to private, as shown in the following code:
< Span style= "font-family: ' Times New Roman ';" > (2) Change the constructor to private is modified? Do not worry, although the outside of the class can no longer use new to create objects, but in taskmanager can be created, and visibility is only valid outside the class. Therefore, we can create and save this unique instance in taskmanager. In order for the outside world to have access to this unique instance, a static >taskmanager taskmanager, as shown in the following code:
(3) In order to guarantee the encapsulation of the member variables, we set the visibility of the TM object of the TaskManager type to private, but how does the outside world use the member variable and when to instantiate the member variable? The answer is to add a public static method, as shown in the following code:
Public Static TaskManager getinstance () { if (tm = = null) { TM = new TaskManager (); } return TM; } |
in tm object exists, if not present (i.e. TM = null), use new keyword creates a new taskmanager type tm object, then returns the newly created tm object Otherwise, return directly to the existing tm object.
Note that getinstance () method modifier, first it should be a public method for use by other outside objects, and second it uses static keyword, that is, it is a static method that can be accessed directly outside the class by the class name. Without creating taskmanager object, in fact, cannot create taskmanager object, because the constructor is private. &NBSP;
&NBSP; |
< strong> thinking |
|
With the above three steps, we have completed the design of one of the simplest singleton classes with the complete code as follows:
Class TaskManager { private static TaskManager TM = null; Private TaskManager () {...}//Initialization window public void displayprocesses () {...}//Show process public void Displayservices () {...}//Display service Public Static TaskManager getinstance () { if (tm = = null) { TM = new TaskManager (); } return TM; } ...... } |
Outside of the class we cannot directly create a new TaskManager object, but we can access the instance object through code taskmanager.getinstance (), and a unique instance will be created the first time the getinstance () method is called. When called again, the instance created for the first time is returned, ensuring the uniqueness of the instance object.
The above code is also one of the most typical implementation of the singleton mode, with the above basis, it is very easy to understand the definition and structure of the singleton pattern. The singleton pattern is defined as follows:
Singleton mode (Singleton pattern): Ensures that a class has only one instance, and instantiates it itself and provides it to the entire system, a class called a singleton class that provides methods for global access. Singleton mode is an object-creation pattern. |
The singleton mode has three points: first, there can be only one instance of a class, and the other is that it must create this instance on its own, and thirdly, it must provide this instance to the whole system on its own.
The singleton pattern is the simplest design pattern of the structure, which contains only a special class called a singleton class in its core structure. The singleton pattern structure is shown in 3-2:
The singleton pattern structure diagram contains only one singleton role:
Singleton (Singleton): An internal implementation of a singleton class generates only one instance, and it provides a static getinstance () factory method that gives the customer access to its unique instance, and to prevent it from being instantiated externally, its constructor is designed to be private A static object of type Singleton is defined inside a singleton class as a unique instance of an external share.
Single-Case mode