singleton : Java Glossary
Singletons are the design pattern when you want only one instance of a class created. You make the constructor
private, and give access to the instantiated object via a static instance method that creates the object
if it has not been created already. The advantages of this technique over using class variables and methods are:
Here is typical code courtesy of Chris Smith for postponing the creation of the singleton in a way that is thread
- You can change your mind later and allow multiple instances without rewriting all your code.
- You can override methods. With static methods, no overriding is possible.
- If you never use the singleton object, it never gets created, saving RAM (Random Access Memory).
- The client can choose his own short name for the singleton to represent the class.
- The official reason is it makes it easier to extend to multiple instances or a factory pattern.