5
One of a Kind Objects
The Singleton Pattern: your ticket to creating one-of-a-
kind objects, for which there is only one instance.
You
might be happy to know that of all patterns, the Singleton is the simplest in terms
of its class diagram; in fact the diagram holds just a single class! But don’t get
too comfortable; despite its simplicity from a class design perspective, we’ll
encounter quite a few bumps and potholes in its implementation. So buckle
up—this one’s not as simple as it seems...
the Singleton Pattern
Strategy
- defines a family of algorithms,
encapsulates each one, and makes them inter-
changeable. Strategy lets the algorithm vary
independently from clients that use it.
OO Patterns
encapsulates each one, and makes them inter-
encapsulates each one, and makes them inter-
changeable. Strategy lets the algorithm vary
changeable. Strategy lets the algorithm vary
independently from clients that use it.
independently from clients that use it.
Observer
-
defines a one-to-many
dependency between objects so that
when one object changes state, all its
dependents are notified and updated
automatically
changeable. Strategy lets the algorithm vary
changeable. Strategy lets the algorithm vary
independently from clients that use it.
independently from clients that use it.
dependency between objects so that
dependency between objects so that
when one object changes state, all its
when one object changes state, all its
dependents are notified and updated
dependents are notified and updated
when one object changes state, all its
when one object changes state, all its
dependents are notified and updated
dependents are notified and updated
dependents are notified and updated
dependents are notified and updated
when one object changes state, all its
dependents are notified and updated
when one object changes state, all its
automatically
automatically
Decorator
-
Attach additional
responsibilities to an object dynami-
cally. Decorators provide a flexible
alternative to subclassing for extending
functionality.
defines a one-to-many
defines a one-to-many
dependents are notified and updated
dependents are notified and updated
automatically
automatically
responsibilities to an object dynami-
responsibilities to an object dynami-
cally. Decorators provide a flexible
cally. Decorators provide a flexible
responsibilities to an object dynami-
responsibilities to an object dynami-
cally. Decorators provide a flexible
cally. Decorators provide a flexible
cally. Decorators provide a flexible
cally. Decorators provide a flexible
responsibilities to an object dynami-
cally. Decorators provide a flexible
responsibilities to an object dynami-
alternative to subclassing for extending
alternative to subclassing for extending
cally. Decorators provide a flexible
cally. Decorators provide a flexible
alternative to subclassing for extending
alternative to subclassing for extending
alternative to subclassing for extending
alternative to subclassing for extending
cally. Decorators provide a flexible
alternative to subclassing for extending
cally. Decorators provide a flexible
functionality.
functionality.
Abstract Factory
-
Provide an
interface for creating families of
related or depedent objects without
specifying their concrete classes.
defines a one-to-many
defines a one-to-many
functionality.
functionality.
Abstract Factory
Abstract Factory
interface for creating families of
interface for creating families of
Abstract Factory
Abstract Factory
interface for creating families of
interface for creating families of
interface for creating families of
interface for creating families of
Abstract Factory
interface for creating families of
Abstract Factory
related or depedent objects without
related or depedent objects without
specifying their concrete classes.
specifying their concrete classes.
related or depedent objects without
related or depedent objects without
specifying their concrete classes.
specifying their concrete classes.
specifying their concrete classes.
specifying their concrete classes.
related or depedent objects without
specifying their concrete classes.
related or depedent objects without
Factory Method
-
Define an
interface for creating an object, but
let subclasses decide which class to in-
stantiate. Factory Method lets a class
defer instantiation to the subclasses.
related or depedent objects without
related or depedent objects without
specifying their concrete classes.
specifying their concrete classes.
related or depedent objects without
related or depedent objects without
specifying their concrete classes.
specifying their concrete classes.
specifying their concrete classes.
specifying their concrete classes.
related or depedent objects without
specifying their concrete classes.
related or depedent objects without
defines a one-to-many
defines a one-to-many
Abstract Factory
Abstract Factory
Factory Method
Factory Method
interface for creating an object, but
interface for creating an object, but
Factory Method
Factory Method
interface for creating an object, but
interface for creating an object, but
interface for creating an object, but
interface for creating an object, but
Factory Method
interface for creating an object, but
Factory Method
let subclasses decide which class to in-
let subclasses decide which class to in-
stantiate. Factory Method lets a class
stantiate. Factory Method lets a class
defer instantiation to the subclasses.
defer instantiation to the subclasses.
stantiate. Factory Method lets a class
stantiate. Factory Method lets a class
defer instantiation to the subclasses.
defer instantiation to the subclasses.
defer instantiation to the subclasses.
defer instantiation to the subclasses.
stantiate. Factory Method lets a class
defer instantiation to the subclasses.
stantiate. Factory Method lets a class
Singleton
-
Ensure a class only has
one instance and provide a global point
of access to it.
One and only one object 170
The Little Singleton 171
Dissecting the classic Singleton Pattern 173
Confessions of a Singleton 174
The Chocolate Factory 175
Singleton Pattern defined 177
Houston, we have a problem... 178
BE the JVM 179
Dealing with multithreading 180
Singleton Q&A 184
Tools for your Design Toolbox 186
Exercise Solutions 188
Hershey, PA