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:
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)
Comments
Want to leave a comment? Visit this post's issue page
on GitHub and just post your comment as the issue's comment (you'll need a GitHub account. What? Like you don't have one yet?!).
Comments: