single case (Singleton) mode is also called a single state mode
Overview: the Singleton (Singleton) pattern requires that a class has and has only one instance, and provides a global access point. This raises the question of how to bypass conventional constructors and provide a mechanism to ensure that a class has only one instance. When a client invokes a class, it does not consider the problem of whether the class can have only one instance, so this should be the responsibility of the Class Designer, not the user of the class. From another perspective, the Singleton model is also a duty-oriented model. Because we created an object that plays a unique role, in this single object instance, it centralizes all the powers of the class it belongs to, and it has the responsibility to exercise that power.
Core Role : Ensure that a class has only one instance, and provide a global access point to access the instance
When to use the single case mode.
As a small example, on our Windows desktop, we opened a recycle Bin, and when we tried to open a new Recycle Bin again, the Windows system did not pop up a new Recycle Bin window for you. , which means that the system maintains only one instance of the Recycle Bin during the entire system operation. This is a typical single case mode of application.
Continue to say Recycle Bin, we do not have in the actual use of the need to open two Recycle Bin windows at the same time. If I need to consume a lot of resources each time I create a recycle Bin, and the resources are shared between each recycle Bin, creating multiple instances without having to recreate the instance multiple times is unnecessarily burdensome to the system, resulting in a waste of resources.
Another example, the site's counter, is generally a single example of the implementation of the model, if you have more than one counter, each user's access to refresh the value of the counter, so that your actual count of the value is difficult to sync. However, this is not a problem if you implement a single case pattern, and you can avoid thread-safety issues. The same multithreading thread pool design is generally a single case mode, because the thread pool needs to facilitate the control of the threads in the pool
Similarly, for some application log applications, or Web development to read configuration files are appropriate to use a single case mode, such as HttpApplication is a single example of the typical application.
From the example above we can summarize the scenarios and advantages and disadvantages that are appropriate for using the single case pattern:
applicable scenario: 1. Environments that need to generate unique sequences
2. Objects that need to be frequently instantiated and then destroyed.
3. Objects that are too time-consuming or too expensive to create, but often used.
4. An environment that facilitates communication between resources
Advantages: 1. Enables controllable access to unique instances
2. For some objects that need to be created and destroyed frequently, the performance of the system can be improved.
Disadvantages: 1. Does not apply to frequently changing objects
2. The misuse of a single example will create some negative problems, such as a single class that is designed to conserve resources to design a database connection pool object, which can cause too many programs to share connection pool objects and overflow the connection pool.
3. if the instantiated object is not exploited for a long time, the system considers the object to be garbage and is reclaimed, which may result in the loss of the object's state.