Card 03/ 05
Observer vs Mediator and Chain of Responsibility vs Decorator
Two pairs that share a shape: many listeners or one coordinator, and passing on or wrapping.
Two more pairs are often confused, each because the two patterns move a call between objects in a similar way. Observer and Mediator both stop objects referring to each other directly, and they differ in whether anything decides what happens next. Chain of Responsibility and Decorator both line objects up behind one interface, and they differ in whether every object passes the call on. This card gives each pair its table and its two sequence diagrams.
| Question | Observer | Mediator |
|---|---|---|
| Who reacts to a change? | Every subscriber, each deciding for itself | The mediator, which decides for all |
| Does the sender know the receivers? | No; it notifies whoever subscribed | No; it tells the mediator |
| Where is the logic of who does what? | Spread across the subscribers | In the mediator |
| Direction of communication | One to many, outward | Many to one, then one to many |
The question for this pair is: after the change, does one object decide what everyone else does? If yes, it is a mediator. If each listener decides for itself what the change means to it, it is an observer. A mediator often uses observer notifications to hear about changes, which is why the two appear together.
| Question | Chain of Responsibility | Decorator |
|---|---|---|
| Does every object pass the call on? | No; a handler may stop the chain | Yes; each calls the object it wraps |
| How many objects do the work? | Usually one, the first that can | All of them, each adding a layer |
| What if nothing handles it? | It falls off the end of the chain | Not possible; the innermost object always runs |
| Family | Behavioural | Structural |
The question for this pair is: can an object in the line stop the call? If it can, and the point is to find the one that handles it, it is a chain; the director never sees the £800 request. If every object passes the call on and the point is the combined layers, it is a decorator. Web middleware sits between the two: each layer can stop a request, as a chain does, and most pass it on with something added, as a decorator does.
← → move through sections and cards