14 — Behavioral Patterns II¶
Driving question: How can objects collaborate without creating invisible control flow?
Learning objectives¶
- Use Observer, Mediator, Chain of Responsibility, Iterator, Visitor, Memento, and Interpreter selectively.
- Analyze coupling introduced by event-driven collaboration.
- Compare Visitor with alternatives when data structure or operations change.
- Recognize traceability costs in callback/event chains.
Collaboration patterns¶
Observer reduces direct knowledge between publishers and subscribers, but can introduce hidden temporal coupling and difficult-to-trace side effects.
Mediator centralizes interaction policy and can reduce many-to-many dependencies; an oversized mediator can become a god object.
Visitor is attractive when the object structure is stable and new operations are frequent. It becomes expensive when the set of element types changes frequently.
Pattern consequences matter¶
For every pattern instance, ask:
- What dependency did we remove?
- What new dependency did we introduce?
- What behavior became easier to add?
- What became harder to trace, test, or understand?
Design / research exercise¶
Build a sequence diagram for an Observer/event-bus interaction involving at least four participants. Mark where ordering, failure, retry, or duplicate-delivery assumptions are hidden.
Suggested reading¶
- GoF, Behavioral Patterns chapters.