Which pattern, when
medium 45 minA pattern is an answer to one kind of pressure.
Watch lesson4:38
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
In the room
When to say a pattern's name
| Moment | Say | Do not say |
|---|---|---|
| Requirements | Which rules will change? | I will use Strategy |
| Class design | The formula varies, so it goes behind one method | This is the classic pattern for it |
| Code | The rule is injected here | Let me add a factory as well |
| Extension | A new rule is one class and one line | We would refactor |
The interviewer asks: which pattern did you use for notifications?
BreaksNone, I call the mail server in the order class
- How
- The order knows the mail server.
- Limit
- The second receiver edits the order. A slow mail server slows orders.
- Choose when
- Never for a growing list.
WorksObserver with a list of listeners
- How
- The order calls each listener in turn.
- Limit
- A failing or slow listener is still felt.
- Choose when
- Listeners are in the same process and fast.
StrongObserver, with failures isolated and slow work queued
- How
- Tell after the change, catch per listener, hand slow work to a queue.
- Limit
- The news may arrive late, and after a crash it may be lost unless it is stored first.
- Choose when
- As soon as a listener leaves the process. See pub and sub.