As an object's creation pattern [GOF95], the singleton pattern ensures that a class has only one instance, and instantiates it and supplies the instance to the entire system. This class is called a single instance class.
Note: This is the 15th chapter of Dr. Shanhong's "Java and Schema" book.
Introduction
The main points of single case model
Single case
It is clear that the main points of the Singleton model are three; one can only have one instance of a certain category; the other is that it must create its own case; third, it must provide this instance to the whole system. In the following object graph, there is a single instance object, and customer A, customer B, and customer C are three client objects of a single instance object. As you can see, all client objects share a single instance object. And from the single example object to its own connection line, it can be seen that the single Case object holds its own reference.
Resource Management
Some resource managers are often designed as a single case pattern.
In a computer system, resources that need to be managed include software external resources, such as each computer can have several printers, but only one printer Spooler to prevent two print jobs from being exported to the printer at the same time. Each computer can have several fax cards, but there should only be one software responsible for managing the fax card to avoid situations where two fax jobs are uploaded to the fax card at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to avoid a single communication port being invoked simultaneously by two requests.
The resources that need to be managed include the internal resources of the software, for example, most of the software has one (or even multiple) attributes (properties) file storage System configuration. Such a system should be managed by an object to manage a property file.
The internal resources that need to be managed include, for example, parts that are responsible for recording the number of visitors to the site, recording parts of the software system's internal events, error messages, or checking the performance of the system. These components must be centrally managed and not run out of bulls.
These resource manager artifacts must have only one instance, which is the first; they must initialize themselves, this is the second; Allow the entire system to access itself this is its third. Therefore, they all satisfy the condition of single case mode, and are the application of single case mode.
One example: Windows Recycle Bin
There is a recycle Bin in Windows 9x window System, and the following figure shows the Recycle Bin for Windows 2000.
In the entire Windows system, the Recycle Bin can only have one instance, the entire system uses this unique instance, and the Recycle Bin provides its own instance. Therefore, the Recycle Bin is the application of single case mode.