Java Design Patterns Survival Guide

by Michael Thomas

Java Design Patterns Home Page

This is a survival guide for Java Design Patterns.
The 23 Gof (Gang of Four) design patterns comes from the book: Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley, by Erich Gamma, Richard Helm, Ralph Johnson, John Vissides. All 23 patterns are listed when you first open the book.

Grouping of Patterns (by Wikipedia)

The 23 Gof (Gang of Four) Design Patterns

Design Pattern Description

Creational Patterns

Abstract Factory
(Group: Creational Patterns)
Wikipedia: Abstract Factory groups object factories that have a common theme.
Gof: Provide an interface for creating families of related or dependent objects without specifying their concrete classes. (pg 87)
Builder
(Group: Creational Patterns)
Wikipedia: Builder constructs complex objects by separating construction and representation.
Gof: Separate the construction of a complex object from its representation so that the same construction processes can create different representations. (pg 97)
Factory Method
(Group: Creational Patterns)
Wikipedia: Factory Method creates objects without specifying the exact class to create.
Gof: Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method lets a class defer instantiation to subclasses. (pg 107)
Prototype
(Group: Creational Patterns)
Wikipedia: Prototype creates objects by cloning an existing object.
Gof: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. (pg 117)
Singleton
(Group: Creational Patterns)
Wikipedia's Singleton: Singleton restricts object creation for a class to only one instance.
Gof: Ensure a class only has one instance, and provide a global point of access to it. (pg 127)

Structural Patterns

Adapter
(Group: Structural patterns)
Wikipedia: Adapter allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class.
Gof: Convert the interface of a class into another interface clients expect.  Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. (pg 139)
Bridge
(Group: Structural patterns)
Wikipedia: Bridge decouples an abstraction from its implementation so that the two can vary independently.
Gof: Decouple an abstraction from its implementation so that the two can vary independently. (pg 151)
Composite
(Group: Structural patterns)
Wikipedia: Composite composes zero-or-more similar objects so that they can be manipulated as one object.
Gof: Compose objects into tree structures to represent part-whole hierarchies.  Composite lets clients treat individual objects and compositions of objects uniformly. (pg 163)
Decorator
(Group: Structural patterns)
Wikipedia: Decorator dynamically adds/overrides behavior in an existing method of an object.
Gof: Attach additional responsibilities to an object dynamically.  Decorators provide a flexible alternative to subclassing for extending functionality. (pg 175)
Facade
(Group: Structural patterns)
Wikipedia: Facade provides a simplified interface to a large body of code.
Gof: Provide a unified interface to a set of interfaces in a system.  Facade defines a higher-level interface that makes the subsystem easier to use. (pg 175)
Flyweight
(Group: Structural patterns)
Wikipedia: Flyweight reduces the cost of creating and manipulating a large number of similar objects.
Gof: Use sharing to support large numbers of fine-grained objects efficiently.  A flyweight is a shared object that can be used in multiple contexts simultaneously.  The flyweight acts as an independent object in each contect - it's indistinguishable from an instance of the object that's not shared. (pg 185)
Proxy
(Group: Structural patterns)
Wikipedia: Proxy provides a placeholder for another object to control access, reduce cost, and reduce complexity.
Gof: Provide a surrogate or placeholder for another object to control access to it. (pg 207)

Behavioral Patterns

Chain of Responsibility
(Group: Behavioral patterns)
Wikipedia: Chain of responsibility delegates commands to a chain of processing objects.
Gof: Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.  Chain the receiving objects and pass the request along the chain until an object handles it. (pg 223)
Command
(Group: Behavioral patterns)
Wikipedia: Command creates objects which encapsulate actions and parameters.
Gof: Encapsulate a request as an object, thereby letting you parameterize clients with different request, queue or log request, and support undoable operations. (pg 223)
Interpreter
(Group: Behavioral patterns)
Wikipedia: Interpreter implements a specialized language.
Gof: Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. (pg 243)
Iterator
(Group: Behavioral patterns)
Wikipedia: Iterator accesses the elements of an object sequentially without exposing its underlying representation.
Gof: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representations. (pg 257)
Mediator
(Group: Behavioral patterns)
Wikipedia: Mediator allows loose coupling between classes by being the only class that has detailed knowledge of their methods.
Gof: Define an object that encapsulates how a set of objects interact.  Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. (pg 273)
Memento
(Group: Behavioral patterns)
Wikipedia: Memento provides the ability to restore an object to its previous state (undo).
Gof: Without violating encapsulation, capture and enternalize an object's internal state so that the object can be restored to this state later. (pg 283)
Observer
(Group: Behavioral patterns)
Wikipedia: Observer is a publish/subscribe pattern which allows a number of observer objects to see an event.
Gof: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. (pg 293)
State
(Group: Behavioral patterns)
Wikipedia: State allows an object to alter its behavior when its internal state changes.
Gof: Allow an object to alter its behavior when its internal state changes.  The object will appear to change its class. (pg 305)
Strategy
(Group: Behavioral patterns)
Wikipedia: Strategy allows one of a family of algorithms to be selected on-the-fly at runtime.
Gof:  Define a family of algorithms, encapsulate each one, and make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it. (pg 315)
Template Method
(Group: Behavioral patterns)
Wikipedia: Template method defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior. (pg 325)
Gof: Define the skeleton of an algorithm without changing the algorithm's structure.
Visitor
(Group: Behavioral patterns)
Wikipedia: Visitor separates an algorithm from an object structure by moving the hierarchy of methods into one object.
Gof: Represent an operation to be performed on the elements of an object structure.  Visitor lets you define a new operation without changing the classes of the elements on which it operates. (pg 331)