COMP2511
Creational Pattern:
Singleton Pattern
Prepared by
Dr. Ashesh Mahidadia
Creational Patterns
Creational patterns provide various object creation mechanisms, which increase
flexibility and reuse of existing code.
v Factory Method
o provides an interface for creating objects in a superclass,
but allows subclasses to alter the type of objects that will be created.
v Abstract Factory
o let users produce families of related objects
without specifying their concrete classes.
v Singleton
o Let users ensure that a class has only one instance,
while providing a global access point to this instance.
v Builder
o let users construct complex objects step by step. The pattern allows users to
produce different types and representations of an object using the same
construction code.
COMP2511: Creational Design Patterns 2
Singleton Pattern
3COMP2511: Creational Design Patterns
Singleton Pattern
Intent: Singleton is a creational design pattern that lets you ensure that a class has
only one instance, while providing a global access point to this instance.
Problem: A client wants to,
v ensure that a class has just a single instance, and
v provide a global access point to that instance
Solution:
All implementations of the Singleton have these two steps in common:
v Make the default constructor private, to prevent other objects from using the new operator
with the Singleton class.
v Create a static creation method that acts as a constructor. Under the hood, this method calls the
private constructor to create an object and saves it in a static field. All following calls to this
method return the cached object.
v If your code has access to the Singleton class, then it’s able to call the Singleton’s static method.
v Whenever Singleton’s static method is called, the same object is always returned.
COMP2511: Creational Design Patterns 4
Singleton: Structure
v The Singleton class declares the static
method getInstance (1) that returns the
same instance of its own class.
v The Singleton’s constructor should be
hidden from the client code.
v Calling the getInstance (1) method
should be the only way of getting the
Singleton object.
COMP2511: Creational Design Patterns 5
Singleton: How to Implement
v Add a private static field to the class for storing the singleton instance.
v Declare a public static creation method for getting the singleton instance.
v Implement “lazy initialization” inside the static method.
o It should create a new object on its first call and put it into the static field.
o The method should always return that instance on all subsequent calls.
v Make the constructor of the class private.
o The static method of the class will still be able to call the constructor, but not the
other objects.
v In a client, call singleton’s static creation method to access the object.
Example in Java (MUST read):
https://refactoring.guru/design-patterns/singleton/java/example
COMP2511: Creational Design Patterns 6
https://refactoring.guru/design-patterns/singleton/java/example
Singleton Pattern
For more information, read:
https://refactoring.guru/design-patterns/singleton
COMP2511: Creational Design Patterns 7
https://refactoring.guru/design-patterns/singleton
End
COMP2511: Creational Design Patterns 8