In this module, you learned about the Singleton design pattern. The Singleton pattern is a creational pattern that lets you create a single instance of the class and provides and controls access to it through a public static method. A nonpublic constructor prevents clients from creating new instances of the class.
The Singleton pattern is used to ensure that only one object of a particular class is instantiated.
The Singleton pattern is used for single-threaded applications.
Review the Primary Elements that make up the Singleton Pattern in Software Development
The Singleton Pattern is a design pattern that restricts the instantiation of a class to a single instance and provides a global point of access to it. This pattern is included in the Gang of Four's creational design patterns due to its responsibility for the control of object creation.
The primary elements of the Singleton Pattern include:
Single Instance: The Singleton Pattern ensures that only a single instance of a class is ever created. This is usually achieved by making the constructor private to prevent other objects from using the new operator with the Singleton class.
Global Access: The Singleton class provides a global access point to this instance. Typically, this is a method called getInstance() or similar, which returns the reference to the singleton instance.
Laziness: In many implementations, the Singleton is "lazy" and is not created until it is needed for the first time. This can save resources if the creation of the singleton is costly and it's not used during the startup of the program.
Thread Safety: In a multithreaded environment, care must be taken to prevent multiple threads from creating the singleton at the same time. This might be achieved using the synchronized keyword in Java or similar constructs in other languages.
Although the Singleton pattern is easy to understand and implement, it must be used cautiously because it introduces a global state into an application. As such, this can lead to code that is hard to test, hard to reason about, and tightly coupled. Therefore, other design patterns and principles, such as dependency injection or the use of modules, may be more appropriate in many cases.
In academic and professional software engineering discourse, the Singleton pattern is both hailed for its simplicity and derided as an anti-pattern for its potential to introduce problems. As with all design patterns, the Singleton pattern is a tool with advantages and disadvantages, and its use should be carefully considered based on the specific needs of the software being developed.
Motivation
Sometimes it's important to have only one instance for a class. For example, in a system there should be only one window manager or only one print spooler.
Usually singletons are used for centralized management of internal or external resources and they provide a global point of access to themselves.
The singleton pattern involves only one class which is required to instantiate itself and to make sure it creates not more than one instance.
At the same time it provides a global point of access to that instance. In this case the same instance can be accessed from anywhere, making it impossible to directly invoke the constructor each time.
Intent
Ensure that only one instance of a class is created.
Provide a global point of access to the object.
Implementation
The implementation involves
a static member in the "Singleton" class,
a private constructor and
a static public method that returns a reference to the static member.
The Singleton Pattern defines a getInstance operation which exposes the unique instance which is accessed by the clients.
getInstance() is is responsible for creating its class unique instance in case it is not created yet and to return that instance.
Next Module
The upcoming modules introduce you to many more patterns.
In the next module, you will have a chance to work with another creational pattern, the "factory method" pattern.