The Singleton design pattern is used to limit a class to one instance and provide global access to that instance. In many cases, you need to limit a class to only one instance. Common examples include classes that manage resources that are intrinsically singular such as selection focus, navigation history, and window depth. Consider the case of a class that manages the state of the user's cursor. Because there is only ever one cursor, you shouldn't have more than one instance of that class. Another example is a class that loads application settings from an XML file and provides access to those settings. You wouldn't want to waste resources by having two instances of that class or by loading the XML file more than once. The dilemma in such cases is how to ensure that only one instance of a class exists and how to make that one instance globally accessible. The Singleton design pattern is a time-tested solution to this problem. Essentially, three features make up a Singleton class:
|