Singleton Mechanics

The mechanics of SINGLETON are more memorable than its intent. It is easier to explain how to ensure that a class has only one instance than it is to say why you might want this restriction. You might categorize SINGLETON as a “creational” pattern, as Design Patterns (Gamma et al. 1995) does. You should, of course, think of patterns in whatever way helps you remember, recognize, and apply them. But the intent of the SINGLETON pattern implies that a specific object bears a responsibility on which other objects rely.

You have some options about how you create an object that takes on a unique role. But regardless of how you create a singleton, you have to ensure that other developers don't create new instances of the class you ...

Get Design Patterns Java™ Workbook 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.