Testing the Singleton Pattern

The Singleton pattern guarantees uniform and controlled access to a unique instance of an object.2 It acts as the object-oriented stand-in for a global variable in the cases in which such is warranted. In its purest use, it corresponds to a real and unique physical resource. It is often used to represent something that is unique to the application, such as an entry point to a remote service or an application registry.

2. Do not confuse the Singleton pattern with the broader concept of a singleton object, an object of which only one exists. The Singleton pattern is one way to ensure you have a singleton object, but there are other reasonable approaches.

There are several ways to implement singletons. A typical implementation ...

Get Quality Code: Software Testing Principles, Practices, and Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.