Design Patterns Java™ Workbook

(Michael S) #1
Chapter 8. Singleton

Figure 8.1. Which classes appear to apply Singleton?

CHALLENGE 8.4


For each class in Figure 8.1, say whether it appears to be a singleton class, and why.

Summary........................................................................................................................................................


Code that supports SINGLETON ensures that a class has only one instance and provides
a global point of access to it. A common way to achieve this is through lazy-initialization of
a singleton object, instantiating it only when the singleton is first needed. In a multithreaded
environment, you must take care to manage the collaboration of threads that may access
a singleton's methods and data at approximately the same time. Regardless of the mechanics
that you apply, the value of SINGLETON lies in centralizing authority in a single object.

Free download pdf