Applied Design Patterns with Java
Creational :: Singleton (127) {C ch 6}
Collaborations
- Clients access a Singleton instance solely
through Singleton's Instance operation.
Consequences
The
Singleton pattern has several benefits:
- Controlled
access to sole instance. Because the Singleton
class encapsulates its sole instance, it can have strict control over how and when clients access it.
- Reduced
name space. The Singleton pattern is an
improvement over global variables. It avoids polluting the name space with global variables that store sole instances.
- Permits
refinement of operations and representation. The Singleton
class may be subclassed, and it's easy to configure an application with an instance of this extended class. You
can configure the application with an instance of the class you need at run-time.
- Permits
a variable number of instances. The pattern makes it
easy to change a program and allow more than one instance of the Singleton class. Also, the same approach is used to control the number of
instances that the application uses. Only the operation that grants access to the Singleton instance needs
to change.
- More
flexible than class operations. Another way to package
a Singleton's functionality is to use class operations (that is, static member functions in C++ or
static
class methods in Java). But both of these language techniques make it hard to change a design to allow more than
one instance of a class. Note that static member functions are never virtual, so subclasses can't define
them polymorphically.
Catalog Creational Prev Next