It is a special class, for which we can not have more than one instance in system at a time. It provides a single global point of access to that instance. Hence it ensures that only one instance can be created for this class and every user is forced to use only that single instance for any operation.
- Any Utility Class: Generally utility classes do not have states with these and so a single instance of that class can serve the whole system.
- Memory Manager: An object which is responsible to manage the memory in system. Obviously only there should be only one instance of this object so that it can update the objects with the current memory availability and other objects can update its state for expected memory requirement.
When to Use
- When only one instance can solve the purpose like utility classes, and does not contain any state with it.
- When only one instance is must to preserve and synchronize the entire system states, so to share the state.
- Helps to ensure only one object in memory.
- Helps to share the states across the system.
- Difficult to extend any singleton class as it will work only if super class has not been instantiated by now.