- What is the Singleton pattern?
The Singleton pattern enforces the uniqueness of an object; only one instantiation of a particular object can exist in the entire program.
- When can the Singleton pattern be used and when should it be avoided?
In a poorly designed program, a Singleton can be used as a substitute for a global variable. To justify its use, there have to be additional reasons for the uniqueness of the object. These reasons can reflect the nature of the reality that is modeled by the program (one car per driver and one Sun in the solar system) or an artificially imposed design restriction (one central source of memory for the whole program). In either case, the programmer should consider how likely it is that the requirements will change and multiple instances will be needed, and weigh it against the work necessary to maintain more complex code with multiple...