Dr. Md Arafatur Rahman
Based on notes from Dr Kevan Buckley
6CS002 - Dr K 1
, Creational Patterns
• Creational design patterns provide solutions to instantiate an
Object in the best possible way for specific situations.
Creational
Patterns
Factory Abstract
Singleton Builder Prototype
Pattern Factory
6CS002 - Dr K 2
, Creational Patterns
• Singleton
o Singleton pattern restricts the instantiation of a class and
ensures that only one instance of the class exists in the Java
Virtual Machine.
o The singleton class must provide a global access point to
get the instance of the class.
o Singleton pattern is used for logging, drivers objects,
caching, and thread pool.
o Singleton design pattern is also used in other design
patterns like Abstract Factory, Builder, Prototype,
Facade, etc.
o Singleton design pattern is used in core Java classes also
(for example, java.lang.Runtime, java.awt.Desktop).
6CS002 - Dr K 3
, Creational Patterns
• Java Singleton Pattern Implementation
o Private constructor to restrict instantiation of the class from other classes.
o Private static variable of the same class that is the only instance of the class.
o Public static method that returns the instance of the class, this is the global access point for
the outer world to get the instance of the singleton class.
6CS002 - Dr K
4