Describe what a singleton is and when you would use it.
the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This concept is also sometimes generalized to restrict the instance to more than one object
common use:
The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation.
Facade objects are often Singletons because only one Facade object is required.
State objects are often Singletons.
Implementation:
The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made protected (not private, because reuse and unit test could need to access the constructor).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment