SE450
:
Creational Patterns: Singleton
[7/12]
Name
- Singleton
Intent
- Ensure a class has only one instance, and provide global access to it
Problem
-
There must be only one instance of a class, and it must be accessible from a known point
The sole instance should be accessible by subclassing, and clients should still be able to access the instance without modifying code.
Solution
- Allow only one instance of the object to be created, and provide access to it through a static method.
Participants and Collaborators
Singleton
Consequences
Controlled access to sole instance
Reduced name space
Permits refinement of operations and representation - the instance may be selected at run time, as an example
Permits a variable number of instances
More flexible that just using static access (think about method hiding...)
Implementation
Ensuring a unique instance
Subclassing the Singleton class
GoF Reference (127)
Example files are
here