Intent
Make sure that a class has only one instance and provides a global access point for the instance.
Motivation
For some classes, it is very important to ensure only one instance. Although multiple printers can be used in the system, there should be only one Spooler (for example, alixiong comment: Who translated the Spooler into a fake offline one? It's really obscure. Is there a real offline app? Is it easier to directly translate it into a "scheduler ?). There should be only one file system and one window manager. A digital filter has only one A/D converter. A company can only have one accounting system.
How can we ensure that a class has only one instance and the instance can be easily accessed? A global variable is an object that can be accessed everywhere, but there is no guarantee that multiple objects will not be instantiated.
A better solution is to allow this class to track its own instances, ensure that no other instances are created (intercept the request for creating new objects), and provide its own access strength. This is the singleton mode.
Applicability
1. A class must have only one instance and provide the client with a well-known access point.
2. When the instance needs to be extended through subclass, the client should not need to be modifiedCodeYou can directly use the extended instance.
Structure
Participant
-Singleton
Defines an instance operation to allow the client to access a unique instance. An instance is a class operation. (For C ++, It is a static member function ).
You can create your own unique instance.
Collaborations
The client accesses a single-piece instance completely through the single-piece instance operation.
Consequences
-Benefits
1. Control access to a unique instance
Because a single-piece class encapsulates a unique instance, you can strictly control how and when the client accesses the instance.
2. Reduce the namespace usage.
The single-piece mode is an improvement for global variables. This prevents the namespace pollution caused by saving a unique instance using global variables.
3. Allow operations and expressions to be refined
Single-piece classes can be subclass and can be easily appliedProgramConfigure it to run as an instance after the extension is used. You can configure the required instance when the application is running.
4. Allows variable target instances.
The Singleton mode allows multiple instances of a singleton class to exist. In addition, you can control the number of instances used by the application in the same way. You only need to modify the operations that provide access to the instance.
5. More flexible than class operations
Another way to provide the single-piece function is to use class operations (static member functions for C ++ ). However, these language technologies make it difficult to modify the design to support multiple instances. In addition, the static member functions of C ++ cannot be virtual, so that polymorphism cannot be achieved through the subclass of overriding.