The Singleton pattern restricts a class to exactly one instance and provides a single, well-known access point to it, which is useful whenever a single shared object must coordinate state or behavior across an entire application.
Key Points: • Ensures only one instance of the class ever exists during the application's lifetime. • Provides a global access point, typically via a static getInstance() method. • Useful for coordinating shared resources like configuration, logging, thread pools, or connection pools. • Prevents conflicting or duplicated state that would arise from multiple competing instances. • Overuse can hurt testability and introduce hidden global state, so it should be used deliberately, not by default.
Example: A ConnectionPool that manages a fixed set of database connections should exist as a single shared instance, since creating multiple independent pools would waste resources and could exceed the database's connection limit.
Interview Tip: A concise interview answer is:
"Singleton guarantees a class has exactly one instance with a single global access point, which is useful for coordinating shared resources like a configuration manager or connection pool — but I'm careful not to overuse it, since it introduces global state that can make code harder to test."