Design Patterns: Singleton

Posted by Monik, 12 October 2010.
Programming Code Design Design Patterns
Book summary
Summary on basis of the book ,,Design Patterns: Elements of Reusable Object-Oriented Software'', by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Addison-Wesley, 1995.

jaki singleton jest kazdy widzi..
* class operation = static operation (?)

We use Singleton pattern when we want to ensure a class only has one instance, and provide a global point of access to it. Another case of using this is when we want to have the possibility of using a subclass of a class instead of the class without having to modify the code of the other classes.

Consequences and variations:
  • singleton class may be responsible for creating its own instance
  • we have full control over access to sole instance
  • it is better than global variables, as we do not pollute the namespace, also as we have lazy initialization
  • concrete singleton class can be specified at run-time, and other classes can still use it in the same way
  • singleton can also restrict the number of possible instances to more that 1 instance
  • it's better not to make the other methods of singleton class (static) methods (it's harder to change to more than 1 instance, it's not possible to override them polymorphically in C++)
  • if we sublcassed the singleton, we can statically define the concrete subclass in singleton's instance operation; or we can use a registry of singletons (then the singleton will no longer be responsible for creating the instance)
* Registry of singletons works the way that it maps singleton String name to class name; each singleton registers itself in the registry, e.g. in its constructor; a singleton's instance method asks the registry for the instance giving it its string name; (metne to jeszcze dla mnie..)

Comments


Comments: