Behavioral Design Patterns Cheat Sheet
Strategy, Observer, Command, and the rest of the Gang of Four behavioral patterns for organizing communication and responsibility between objects.
2 PagesAdvancedMar 28, 2026
Strategy Pattern
Swapping interchangeable algorithms behind a common interface.
python
from abc import ABC, abstractmethodclass DiscountStrategy(ABC): @abstractmethod def apply(self, price: float) -> float: ...class NoDiscount(DiscountStrategy): def apply(self, price): return priceclass PercentageDiscount(DiscountStrategy): def __init__(self, percent): self.percent = percent def apply(self, price): return price * (1 - self.percent / 100)class Order: def __init__(self, strategy: DiscountStrategy): self.strategy = strategy # swap algorithm at runtime def total(self, price): return self.strategy.apply(price)order = Order(PercentageDiscount(20))order.total(100) # 80.0
Observer Pattern
Notifying subscribed objects automatically when state changes.
python
class Subject: def __init__(self): self._observers = [] def subscribe(self, observer): self._observers.append(observer) def notify(self, event): for obs in self._observers: obs.update(event)class Logger: def update(self, event): print(f"LOG: {event}")subject = Subject()subject.subscribe(Logger())subject.notify("order_created") # LOG: order_created
Command Pattern
Encapsulating a request as an object to support undo/redo.
python
from abc import ABC, abstractmethodclass Command(ABC): @abstractmethod def execute(self): ... @abstractmethod def undo(self): ...class AddTextCommand(Command): def __init__(self, document, text): self.document, self.text = document, text def execute(self): self.document.text += self.text def undo(self): self.document.text = self.document.text[:-len(self.text)]class Document: def __init__(self): self.text = ""doc = Document()history = []cmd = AddTextCommand(doc, "Hello")cmd.execute(); history.append(cmd)history.pop().undo() # reverts the change
Behavioral Pattern Catalog
One-line definitions of the classic Gang of Four behavioral patterns.
- Strategy- Encapsulate interchangeable algorithms behind a common interface and select one at runtime
- Observer- Define a one-to-many dependency so that when one object changes state, all dependents are notified
- Command- Encapsulate a request as an object, enabling undo/redo, queuing, and logging of operations
- Iterator- Provide sequential access to elements of a collection without exposing its underlying representation
- State- Let an object alter its behavior when its internal state changes, appearing to change its class
- Template Method- Define the skeleton of an algorithm in a base class, letting subclasses override individual steps
- Chain of Responsibility- Pass a request along a chain of handlers until one of them handles it
- Mediator- Centralize complex communication between objects in a mediator instead of direct object references
- Visitor- Separate an algorithm from the object structure it operates on by moving it into a visitor object
- Memento- Capture and externalize an object's internal state so it can be restored later without breaking encapsulation
Pro Tip
Reach for Strategy or State when you see a long if/elif chain switching on a type or mode flag — both replace conditional branching with polymorphism, but State is for behavior driven by an object's own lifecycle, while Strategy is for algorithms chosen by the caller.
Was this cheat sheet helpful?
Explore Topics
#BehavioralDesignPatterns#BehavioralDesignPatternsCheatSheet#Programming#Advanced#StrategyPattern#ObserverPattern#CommandPattern#BehavioralPatternCatalog#OOP#APIs#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance