The creational patterns solved the birth of PideYa's objects; the structural patterns, their anatomy. That leaves the question that closed the previous module, the most dynamic of the three: with the objects created and properly wired together, how do they collaborate at runtime? Who notifies whom when an order changes status? How do you undo an action in the restaurant panel? Where does the algorithm live that decides which courier takes each order? Behavioral patterns answer exactly this: they distribute responsibilities among objects and organize their communication without coupling them. They are the largest family in the GoF catalog — eleven patterns — and this lesson is your map: what shared problem they attack, who is who, and which concrete PideYa pains each one is waiting for.
Contents
- The shared problem: collaborating without coupling
- Overview of the eleven behavioral patterns
- Class scope and object scope
- Symptoms in PideYa that call for a behavioral pattern
- How we will read each pattern in this module
- Exercises and conclusion
The shared problem: collaborating without coupling
Creational patterns answered who decides which class gets instantiated?; structural patterns, how do the pieces fit together?. Behavioral patterns answer two intertwined questions:
- Responsibility assignment: which object should do what? Where does an algorithm live, who holds a piece of state, who decides a transition?
- Communication: how do objects talk to each other to complete a task none of them can finish alone?
And the enemy is the usual one, showing its third face: coupling, this time in the conversations. In module 2, coupling sneaked in through new; in module 3, through structural connections; here it sneaks in through the calls:
- An object that, to do its job, directly calls every party interested in it (remember
Order.changeStatus(...)from the very first lesson of the course? This module settles that debt). - An algorithm hard-wired inside the class that uses it, impossible to change without touching the class.
- A tangle of conditionals (
switchover statuses,ifover types) repeated all over the codebase and growing with every new case. - Objects that all know each other in order to coordinate: a mesh where touching one drags along the rest.
- Requests that only exist as ephemeral calls: they cannot be queued, undone, logged, or forwarded.
The family's strategy is always the same one, and you already know it from the principles in module 1: reify — turn into an object what used to be loose code. An algorithm becomes an object (Strategy), a request becomes an object (Command), a state becomes an object (State), a snapshot of the past becomes an object (Memento), a traversal becomes an object (Iterator). Once something is an object, it can be swapped, injected, queued, stored, and tested. It is "program to interfaces" and "composition over inheritance" applied to behavior.
Overview of the eleven behavioral patterns
The full picture of the module, each pattern in one line. Don't memorize it now: we will come back to it, expanded and with head-to-head matchups, in the final comparison.
| Pattern | Intent in one line |
|---|---|
| Chain of Responsibility | Pass a request along a chain of handlers until one of them handles it |
| Command | Encapsulate a request as an object: queue it, log it, undo it |
| Interpreter | Define the grammar of a mini-language and an interpreter that evaluates its sentences |
| Iterator | Traverse the elements of a collection without exposing its internal structure |
| Mediator | Centralize many-to-many interactions between colleagues in a single object |
| Memento | Capture an object's state so it can be restored later, without breaking its encapsulation |
| Observer | One-to-many subscription: when the subject changes, all its observers find out |
| State | An object whose behavior changes with its internal state, without giant conditionals |
| Strategy | A family of interchangeable algorithms, encapsulated behind a common interface |
| Template Method | The skeleton of an algorithm in a base class, with steps that subclasses redefine |
| Visitor | Add new operations to a stable object hierarchy without modifying it |
Eleven is a lot, so it helps to group them mentally by the conversation they organize:
- Encapsulating what varies: Strategy (an algorithm), State (state-dependent behavior), Command (a request), Template Method (steps of an algorithm), Interpreter (sentences of a language).
- Communicating without coupling: Observer (one notifies many it doesn't know), Mediator (many talk through one), Chain of Responsibility (the request looks for someone to handle it).
- Working over structures: Iterator (traversing them), Visitor (operating on them), Memento (snapshotting and restoring them).
Class scope and object scope
As in the previous families, each pattern has a scope: class scope if the collaboration is fixed through inheritance at compile time, or object scope if it is established through composition at runtime. The tally here is almost as lopsided as in the structural family: nine of the eleven are object-scoped. The two class-scoped ones are:
- Template Method: the algorithm's skeleton lives in the superclass and subclasses redefine steps by inheriting. It is the inheritance pattern par excellence.
- Interpreter: the grammar is captured as a class hierarchy (one class per rule), fixed at compile time.
The other nine compose: the context contains a strategy, the subject contains observers, the invoker contains commands... The Template Method / Strategy pair is the perfect head-to-head for this difference — same problem, one solves it by inheriting and the other by composing — and we will put them face to face in its lesson.
Symptoms in PideYa that call for a behavioral pattern
As in the previous modules: first the symptom, then the pattern. All of these pains exist in PideYa today; each one has its lesson:
| Symptom in PideYa | Design smell | Pattern that treats it |
|---|---|---|
An incoming order must pass a series of checks (fraud, stock, delivery zone, minimum amount) and today they are one mile-long method of nested ifs |
Serial validations hard-wired into a single block, impossible to reorder or reuse | Chain of Responsibility |
| The restaurant panel needs "undo" (accept, cancel, mark as preparing...) and a queue of pending operations, but each action is just a method call that vanishes | Ephemeral requests that cannot be queued, logged, or reverted | Command |
| Marketing wants to write promotion rules ("total > 30 AND day == FRIDAY") without deploying code | Business rules expressed as text that someone has to evaluate | Interpreter |
| Traversing the Composite menu forces every client to know there are sections inside sections | Internal structure exposed to anyone who wants to traverse it | Iterator |
| Ready orders, couriers, and the kitchen call each other to coordinate: every class knows all the others | Many-to-many mesh coupling | Mediator |
| The customer edits their cart and wants "undo" without the cart exposing its internals | Need to save and restore state without breaking encapsulation | Memento |
Order.changeStatus(...) creates the push, SMS, and statistics services with new — the pain from lesson 01-01, still unresolved |
The subject knows and calls every interested party | Observer |
What can be done with an order depends on whether it is created, paid, out for delivery...; the code is a switch over the status repeated in every method |
State-dependent behavior scattered across duplicated conditionals | State |
| Delivery fees are calculated three different ways (distance, flat rate, free with a promotion) chosen at runtime | Alternative algorithms hard-wired with conditionals in the client | Strategy |
| The daily closing reports repeat the same flow (load → aggregate → format → distribute) with different steps depending on the format | An algorithm duplicated across variants that differ only in some steps | Template Method |
Exporting the menu to JSON, computing allergens, and auditing prices threaten to fill Dish and MenuSection with methods foreign to their responsibility |
New operations that force modifying a stable hierarchy | Visitor |
How we will read each pattern in this module
The eleven pattern lessons follow the skeleton of the previous modules, so comparing them stays easy:
- The problem in PideYa, with the code of the first attempt (the bad one).
- Structure: the GoF intent and a mermaid
classDiagramwith the roles, as in the UML lesson. In this family we will often add asequenceDiagram, because what matters is the conversation over time, not just the static snapshot. - Complete Java implementation, explained step by step.
- Relevant variants of each pattern.
- When to use it and when not to, with the usual honesty about costs.
- Relationship with other patterns: mentions with links only.
- Common mistakes, exercises with solutions, and a conclusion.
And we keep building on what we've built: the Composite menu from module 3 will be traversed by Iterator and visited by Visitor; the Order.Builder from module 2 will produce the orders that Observer watches and State governs; the decorated Notifier objects from module 3 will be the final receivers of Observer's notifications; the CheckoutFacade will invoke Chain of Responsibility's validation chain. One system, many conversations.
Exercises
Exercise 1: match symptom and pattern
For each new PideYa situation, say which behavioral pattern from the table it seems to point to (matching the symptom against the one-line intent is enough):
- When a dish's price changes, the search engine, the catalog cache, and the price history must all find out — and tomorrow maybe someone else too.
- Support wants a complaint to go first through the bot, then through an agent, then through a supervisor, with each level either resolving it or escalating it.
- We want to try two ways of ranking restaurants on the home page (by rating, by proximity) and pick one through configuration.
- When the restaurant hits "save draft" on a menu being edited, it wants to be able to come back later to exactly that point.
Exercise 2: reify
This module repeats one trick: turning into an object something that used to be loose code. State what becomes an object in each of these patterns: Strategy, Command, Memento, Iterator, State.
Solutions
Solution 1:
- Observer: one change, many interested parties, unknown and variable.
- Chain of Responsibility: the request travels through handlers until one handles it (or escalates it).
- Strategy: alternative, interchangeable algorithms behind a common interface.
- Memento: capture the state to restore it later without exposing the object's internals.
Solution 2: Strategy reifies an algorithm; Command, a request (a call together with its arguments); Memento, a state snapshot; Iterator, a traversal (the position and the advancing logic); State, a state and its associated behavior.
Conclusion
You now have the map of the largest family in the catalog: eleven patterns that distribute responsibilities and organize communication between objects without coupling them, almost all through composition, and almost all applying the same trick — reifying behavior so it can be swapped, queued, stored, or handed out. You know which PideYa pain awaits each one, including the oldest in the course: that changeStatus from the first lesson that has spent three modules waiting for Observer.
But we start at the business's front door: every order that reaches PideYa must survive an obstacle course — is it fraud? is there stock? do we deliver to that zone? does it reach the minimum amount? — and today that course is one monolithic method nobody wants to touch. We are going to turn it into a chain of independent, recombinable links. See you in Chain of Responsibility.
Software Design Patterns Course
Module 1: Introduction to Design Patterns
- What Are Design Patterns?
- History and Origin of Design Patterns
- Design Principles: SOLID and Other Foundations
- Essential UML for Understanding Patterns
- Classification of Design Patterns
- Advantages and Disadvantages of Using Design Patterns
Module 2: Creational Patterns
- Introduction to Creational Patterns
- Singleton
- Factory Method
- Abstract Factory
- Builder
- Prototype
- Comparing and Choosing Creational Patterns
Module 3: Structural Patterns
- Introduction to Structural Patterns
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Proxy
- Comparing and Choosing Structural Patterns
Module 4: Behavioral Patterns
- Introduction to Behavioral Patterns
- Chain of Responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor
- Comparing and Choosing Behavioral Patterns
Module 5: Applying Design Patterns
- How to Select the Right Pattern
- Practical Examples of Pattern Usage
- Design Patterns in Real Projects
- Refactoring with Design Patterns
- Anti-Patterns: When Patterns Become a Problem
Module 6: Advanced Design Patterns
- Design Patterns in Modern Architectures
- Design Patterns in Microservices
- Design Patterns in Distributed Systems
- Concurrency Patterns
- Design Patterns in Agile Development
