Singleton

Purpose Ensure that a class has only 1 instance, which is accessible from a well-known access point.

Use Singleton for objects that are unique in the system (e.g., an operating system has only 1 instance of a FileSystem).
Structure Singleton Structure
Consequences
  • The Singleton class can be subclassed. For example, in Jicos, each JVM in the system is running exactly 1 of either a hosting service provider (HSP), task server, or host. Each of these extend CellImpl, which therefore should be a Singleton.
Implementation
  • Enforce the singleton property with a private constructor.
  • If the singleton class is to be Serializable, override readResolve to ensure that no other instances are constructed unintentionally.
Sample Code
Related Patterns

 cappello@cs.ucsb.edu © Copyright 2010 Peter Cappello                                           2010.04.29