single-case mode (Singleton)
limits the The number of instantiations can only be once ; (If the instance does not exist, a new instance is created; If the instance exists, a reference to that instance is returned)
In JavaScript, Singleton acts as a shared resource namespace, isolating code implementations from the global namespace, providing a single point of access for functions. --"JavaScript design pattern P39" (in the 1th Point of explanation)
Applicability of a single case model
When a class can have only one instance and the customer can access it from a well-known access point;
the only instance should be the subclass extensible, and the customer should be able to use an extended instance without changing the code. --"JavaScript design mode P40"
Advantages:
Only one time is instantiated;
Save memory.
Disadvantages:
The modules in the system appear or are tightly coupled;
Or its logic is too scattered;
It is difficult to create multiple instances;
testing can be difficult;
~ ~ And so on.
Ideas:
Don't overdo it.
Application Scenarios:
the system does just one object to coordinate other objects time. (e.g. use in communication coordination)
Example:
Learning materials:
the JavaScript design model;
Uncle Tom understands the JavaScript series in depth.
Single-Case mode