Behavioral Design Patterns Cheat Sheet
Template Method
Defines a fixed algorithm structure with customizable steps using inheritance.
abstract class GameAI {
void turn(){ step1(); step2(); }
abstract void step2();
}
Strategy
Encapsulates interchangeable algorithms and allows switching at runtime.
interface Strategy { void execute(); }
class Context { Strategy s; }
Iterator
Provides sequential access to elements without exposing internal structure.
interface Iterator { boolean hasNext(); Object next(); }
Mediator
Centralizes communication between objects.
interface Mediator { void notify(); }
class Colleague { Mediator m; }
Observer
Notifies multiple objects automatically when state changes.
interface Observer { void update(); }
class Subject { notifyObservers(); }