Self-awareness: A single-piece model is like a "one-man-one-wife" in China's marriage system. A husband can only have one daughter-in-law. If you want to marry a second or third daughter-in-law, the civil affairs bureau will certainly not do it for you; while the single-piece mode is that an object can only create one instance. If you want to create the second and third instances, the program will prompt you "illegal ".
Purpose: The single-piece mode aims to provide a global access point for the program class.
Other methods and disadvantages:
A. Create A single piece at the beginning of the program and pass it as A parameter to the class that needs to be used.
Disadvantage: in a program running, you may not need all the single pieces, which will affect the program performance.
B. create a registry for all single-piece classes in the program and make the Registry always available. Each time a single piece is instantiated, it is recorded in the registry, all parts of the program can use identifiers to access any single-piece instance and retrieve corresponding instance variables.
Disadvantage: The type check is reduced, because the single-piece table in the registry may keep all the single-pieces as the object type, such as the object type in Hashtable. In addition, the Registry itself may also be a single-piece, you must use constructors or other set functions to pass all the parts of a program.
C. Static Method of the class.