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.

What is a Java Design Pattern?

Grouping of Patterns (by Wikipedia)

Help, where do I start?

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.
UML diagram from Wikipedia.
Gof: Provide an interface for creating families of related or dependent objects without specifying their concrete classes. (pg 87)

OOP Principles (my comments):
Encapsulates object creation through object composition. You can group a family of related behaviors (products) that belong together and vary based on the object created. This gives you the ability to take a complex Factory Method and simplify by further loosely coupling through composition vs inheritance.

When to use (my comments):
 

Builder
(Group: Creational Patterns)
Wikipedia: Builder constructs complex objects by separating construction and representation.
UML diagram from Wikipedia.
Gof: Separate the construction of a complex object from its representation so that the same construction processes can create different representations. (pg 97)

OOP Principles (my comments):

When to use (my comments):
 
Simple Factory
(Group: Creational Patterns)
NOTE: This pattern is not apart of the Gof's 23 design patterns.
My definition - A superclass (single class) returns an instance of a class (usually a subclass) which implements the needed behavior based on data parameters passed into factory's builder method. 

OOP Principles (my comments):
Encapsulates object creation through a superclass.

When to use (my comments):
  • You want to loosely couple object creation into one simple class.  The creation of objects is becoming complex and too coupled to class inheritance.  You want your classes closed to modification and open to extension so you want to make object creation cohesive to one factory class that you change.
  • You want to organize the creation of subclasses into one class which decides which subclass to create based on arguments passed to it's creation method.  The decision of which subclass to create could be a simple or complex algorithm.

  •  
Factory Method
(Group: Creational Patterns)
Wikipedia: Factory Method creates objects without specifying the exact class to create.
UML diagram from Wikipedia.
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)

OOP Principles (my comments):
Encapsulates object creation through inheritance (subclass).

When to use (my comments):
  • You want to loosely couple object creation to a subclass. The creation of objects is becoming complex and too coupled to class inheritance.  You want your classes closed to modification and open to extension so you want to make object creation cohesive to a set of factory subclass that you change.
  • The creation of objects is becoming complex and to coupled to class inheritance.
Prototype
(Group: Creational Patterns)
Wikipedia: Prototype creates objects by cloning an existing object.
UML diagram from Wikipedia.
Gof: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. (pg 117)

OOP Principles (my comments):

When to use (my comments):
 

Singleton
(Group: Creational Patterns)
Free Singleton Tutorial
Wikipedia's Singleton: Singleton restricts object creation for a class to only one instance.
UML diagram from Wikipedia.
Gof: Ensure a class only has one instance, and provide a global point of access to it. (pg 127)

OOP Principles (my comments):

When to use (my comments):
 

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.
UML diagram from Wikipedia.
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)

OOP Principles (my comments):

When to use (my comments):
 
Bridge
(Group: Structural patterns)
Wikipedia: Bridge decouples an abstraction from its implementation so that the two can vary independently.
UML diagram from Wikipedia.
Gof: Decouple an abstraction from its implementation so that the two can vary independently. (pg 151)

OOP Principles (my comments):

When to use (my comments):
 
Composite
(Group: Structural patterns)
Wikipedia: Composite composes zero-or-more similar objects so that they can be manipulated as one object.
UML diagram from Wikipedia.
Gof: Compose objects into tree structures to represent part-whole hierarchies.  Composite lets clients treat individual objects and compositions of objects uniformly. (pg 163)

OOP Principles (my comments):

When to use (my comments):
 
Decorator
(Group: Structural patterns)
Wikipedia: Decorator dynamically adds/overrides behavior in an existing method of an object.
UML diagram from Wikipedia.
Gof: Attach additional responsibilities to an object dynamically.  Decorators provide a flexible alternative to subclassing for extending functionality. (pg 175)

OOP Principles (my comments):

When to use (my comments):
 
Facade
(Group: Structural patterns)
Wikipedia: Facade provides a simplified interface to a large body of code.
UML diagram from Wikipedia.
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)

OOP Principles (my comments):

When to use (my comments):
 
Flyweight
(Group: Structural patterns)
Wikipedia: Flyweight reduces the cost of creating and manipulating a large number of similar objects.
(UML diagram from Wikipedia is not available.)
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)

OOP Principles (my comments):

When to use (my comments):
 
Proxy
(Group: Structural patterns)
Wikipedia: Proxy provides a placeholder for another object to control access, reduce cost, and reduce complexity.
UML diagram from Wikipedia.
Gof: Provide a surrogate or placeholder for another object to control access to it. (pg 207)

OOP Principles (my comments):

When to use (my comments):
 

Behavioral Patterns

Chain of Responsibility
(Group: Behavioral patterns)
Wikipedia: Chain of responsibility delegates commands to a chain of processing objects.
(UML diagram from Wikipedia is not available.)
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)

OOP Principles (my comments):

When to use (my comments):
 
Command
(Group: Behavioral patterns)
Wikipedia: Command creates objects which encapsulate actions and parameters.
UML diagram from Wikipedia.
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)

OOP Principles (my comments):

When to use (my comments):
 
Interpreter
(Group: Behavioral patterns)
Wikipedia: Interpreter implements a specialized language.
UML diagram from Wikipedia.
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)

OOP Principles (my comments):

When to use (my comments):
 
Iterator
(Group: Behavioral patterns)
Wikipedia: Iterator accesses the elements of an object sequentially without exposing its underlying representation.
(UML diagram from Wikipedia is not available.)
Gof: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representations. (pg 257)

OOP Principles (my comments):

When to use (my comments):
 
Mediator
(Group: Behavioral patterns)
Wikipedia: Mediator allows loose coupling between classes by being the only class that has detailed knowledge of their methods.
(UML diagram from Wikipedia is not available.)
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)

OOP Principles (my comments):

When to use (my comments):
 
Memento
(Group: Behavioral patterns)
Wikipedia: Memento provides the ability to restore an object to its previous state (undo).
(UML diagram from Wikipedia is not available.)
Gof: Without violating encapsulation, capture and internalize an object's internal state so that the object can be restored to this state later. (pg 283)

OOP Principles (my comments):

When to use (my comments):
 
Observer
(Group: Behavioral patterns)
Wikipedia: Observer is a publish/subscribe pattern which allows a number of observer objects to see an event.
UML diagram from Wikipedia.
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)

OOP Principles (my comments):

When to use (my comments):
 
State
(Group: Behavioral patterns)
Wikipedia: State allows an object to alter its behavior when its internal state changes.
UML diagram from Wikipedia.
Gof: Allow an object to alter its behavior when its internal state changes.  The object will appear to change its class. (pg 305)

OOP Principles (my comments):

When to use (my comments):
 
Strategy
(Group: Behavioral patterns)
Wikipedia: Strategy allows one of a family of algorithms to be selected on-the-fly at runtime.
UML diagram from Wikipedia.
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)

OOP Principles (my comments):
Favor composition over inheritance.
Encapsulates behavior

When to use (my comments):
 

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)
UML diagram from Wikipedia.
Gof: Define the skeleton of an algorithm without changing the algorithm's structure.

OOP Principles (my comments):

When to use (my comments):
 
Visitor
(Group: Behavioral patterns)
Wikipedia: Visitor separates an algorithm from an object structure by moving the hierarchy of methods into one object.
UML diagram from Wikipedia.
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)

OOP Principles (my comments):

When to use (my comments):