Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

Name: Singleton

Classification:

Scope Object
Purpose : Creational
Intent
• Ensure a class only has one instance, and
provide a global point of access to it.
Also Known As
• ---
Motivation
• It’s important for some classes to have exactly one instance.
– Many printers – but only one printer spooler
– One file system , one window manager
– An accounting system will be dedicated serving to one company
• How do we ensure that a class has only one instance and that the
instance is easily accessible?
– A global variable makes an object accessible, but it dosen’t keep you
from instantiating multiple objects.
• Solution
– 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)
– It can provide a way to access the instance.
– This is singleton pattern 
Applicability
• Use Singleton pattern when
– There must be exactly one instance of a
class, and it must be accessible to clients
from a well-known access point.
– When the sole instance should be extensible
by sub classing, and clients should be able to
use an extended instance without modifying
their code.
Participants
• Singleton
– Defines an Instance operation that lets clients
access its unique instance.
– Instance operation is a class operation (i.e
static method)
– May be responsible for creating its own
unique instance.
Collaborations
• Clients access Singleton instance solely through
Singleton’s Instance operation.
Consequences
The Singleton pattern has several benefits
• Controlled access to sole instance.
– Can have strict control over how and when clients access it.
• Reduce name space
– Improvement over global variables
– Avoids polluting the namespace with global variables that store sole
instances.
• Permits refinement of operations and representation
– Singleton instance can be subclasses, easy to configure instance.
• Permits a variable number of instances
– You can change your mind – can allow more than one instance of the
Singleton.
• More flexible than class operations.
– Static methods cannot be overridden polymorphically.
– This technique makes it hard to change a design to allow more than one
instance of a class.
Implementation
• Ensuring a unique instance
– Hide the operation that creates the instance behind a class operation that
guarantees only one instance is created.
– This operation has access to the variable that holds the unique instance
and it ensures the variable initialized with the unique instance before
returning its value.
• Sub classing the Singleton class
– Here the issue is about installing the subclasses' unique instance so that
clients will be able to use it.
– The variable that refers to the singleton instance must get initialized with
an instance of the subclass.
• Determine which singleton you want to use in the Singleton’s Instance
operation. (using environment variables)
• Take the implementation of Instance out of the parent class and put it in
subclass.
• More flexible approach is to use a registry of singletons :- instead of having
Instance define the set of possible Singleton classes, the Singleton classes
can register their singleton instance by name in a well-known registry.
Known uses
• Interviews user interface toolkit uses the
Singleton pattern to access the unique
instance of its Session class.
Related Patterns
• Many patterns can be implemented using
the Singleton Pattern
– Abstract Factory
– Builder
– Prototype

You might also like