This pattern suggests that you create one and only one instance and provide a global point of access to the created object:
The DB connection in the preceding diagram is intended to be a singleton and provides a getter for its only object.
Here are its benefits:
- Controlled access to a sole instance
- Reduced namespace
- Flexibility to refinement of operations and representations
- More flexible than class operations
Impacts are as follows:
- Carry states for the whole lifetime of the application, creating additional overhead for unit tests
- Some level of violation of single responsibility principle
- By using singleton as a global instance, it hides the dependencies of the application; rather, it should get exposed through interfaces