Singleton Pattern   «Prev  Next»
Lesson 3 Singleton: intent and motivation
ObjectiveIntent and Motivation for Singleton Patterns

Intent and Motivation for Singleton Design Patterns

The Singleton pattern is an object creational pattern. It is a creational pattern because it concerns itself with the creation of objects, more specifically with making sure more than one object is not created. It is an object pattern because it deals with the relationships between objects (specifically the unique instance object the class holds and other objects access) rather than with relations between classes and their subclasses.
The intent of the Singleton pattern is twofold:
  1. Guarantee that a class has only one instance: Guaranteeing that a class has only one instance is critical in scenarios where a single point of control or access is necessary to ensure consistency and resource management. This approach prevents the creation of multiple instances that could lead to resource contention, inconsistent state, or unexpected behavior. For example, consider an application configuration manager; allowing multiple instances could result in different parts of the application using different settings, leading to unpredictable results. By ensuring there is only one instance, the Singleton pattern centralizes control, maintaining a consistent and synchronized state across the entire application.
  2. Provide access to that instance: Providing access to the single instance is equally important, as it allows different parts of an application to interact with the singleton object efficiently. The Singleton pattern typically includes a static method that checks whether an instance already exists; if not, it creates one and returns the reference. This method ensures that every request for the object gets the same instance, fostering uniform access and simplifying dependency management. This accessibility is crucial for shared resources such as logging systems or database connections, where a consistent point of interaction is necessary to maintain application integrity and performance.

Motivation

  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

In this case, the motivation is that some real-world objects have no more than one instance. For example, most computers have only one audio output. No more than one sound can be played at a time. Therefore a class that represents the computer's audio device should have exactly one instance. If there were two audio device objects, programmers could try to play multiple sounds simultaneously, generally with unpredictable and unwanted results. If there is only one audio device object, it can implement internal queuing as necessary to make sure that one sound file stops or finishes before it tries to play another. A queue could be shared between different audio device objects, but the overhead for maintaining the queue and communicating between the different objects is prohibitive. Furthermore, since there is only one audio device to start with, that device might as well be set up as soon as the class is loaded. Initialization is simpler because you only have to do it once for the single instance of the AudioDevice class, rather than repeatedly for different instances.

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

Apply Singleton to Traffic Light Manager

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.

Singleton Role

The purpose of the Singleton pattern is to ensure that there is only one instance of a class, and that there is a global access point to that object. The pattern ensures that the class is instantiated only once and that all requests are directed to that one and only object. Moreover, the object should not be created until it is actually needed. In the Singleton pattern, it is the class itself that is responsible for ensuring this constraint, not the clients of the class.

SEMrush Software 3 SEMrush Banner 3