Singleton Pattern   «Prev 

Multiple motivations for Singleton Design Pattern

This is not the only motivation for the Singleton pattern.
The Gang of Four suggests several other motivations including:
  1. File and window managers
  2. A/D converters in digital filters
  3. 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.
Question: How do we ensure that a class has only one instance and that the instance is easily accessible?
Answer: 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.
  1. Sometimes we want just a single instance of a class to exist in the system
  2. For example, we want just one window manager or just one factory for a family of products.
  3. We need to have that one instance easily accessible
  4. And we want to ensure that additional instances of the class can not be created

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.