Overview

The Singleton pattern is used when you need to coordinate actions across a system from a single central location, such as a database connection pool or a configuration manager.

Implementation

Typically involves a private constructor and a static method that returns the single instance of the class.

Pros and Cons

  • Pros: Controlled access to a single instance, reduced memory footprint.
  • Cons: Can be difficult to test (global state), can hide dependencies, may violate the Single Responsibility Principle.

Related Terms