AlgoViz

Which pattern, when

medium 45 min

A 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

MomentSayDo not say
RequirementsWhich rules will change?I will use Strategy
Class designThe formula varies, so it goes behind one methodThis is the classic pattern for it
CodeThe rule is injected hereLet me add a factory as well
ExtensionA new rule is one class and one lineWe 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.