This is not the only motivation for the Singleton pattern.
The Gang of Four suggests several other motivations including:
- File and window managers
- A/D converters in digital filters
- The company an accounting system serves
It is important for some classes to have exactly one instance.
Although there can be many printers in a system, there should be only one printer spooler. There should be only file system and one window manager.
A digital filter will have one A/D converter. An accounting system will be dedicated to serving one company.
How do we ensure that a class has only one instance and that the instance is easily accessible?
Make the class itself responsible for keeping track of its sole instance.
The class can ensure that no other instance can be created ( by intercepting requests to create new objects), and it can provide a way to access the instance.
Later in this module, you will use the Singleton pattern to guarantee that the traffic light manager has only one instance.
This allows that instance to insure that the lights stay in sync.
Most patterns have many different motivations.
There would not be much point to a design pattern that was only useful in one context. Design patterns are supposed to be adaptable to a broad range of problems.