LOW LEVEL DESIGN · STEP 7 OF 9

Behavioral patterns

Eleven ways objects share work, pass requests and react to change. Each lesson is an animated walkthrough with a small Java example you can run, notes and the interview questions with answers.

  1. 1 Strategy patternSwap one algorithm for another at runtime behind one interface.The Strategy pattern through ride fares: metered, airport and night rules behind one FareRule interface, a TripBilling context that swaps them at runtime, lambdas as strategies, a picker that chooses, Strategy vs State and Template Method, Comparator and RejectedExecutionHandler, and its use in parking lot, rate limiter and splitwise designs.16 steps · 8 interview Q&As · Java example · used in 32 problems
  2. 2 Observer patternNotify every subscriber automatically when something changes.The Observer pattern with a parcel tracking example: why a subject should not call every consumer by name, the listener interface, subscribe and unsubscribe, push versus pull, memory leaks from forgotten listeners, failing and slow listeners, how it differs from pub/sub with a broker, where Java uses it, and where it appears in LLD interviews.16 steps · 8 interview Q&As · Java example · used in 30 problems
  3. 3 State patternLet an object change its behaviour by switching between state objects.The State pattern with a ride booking example: why a status enum with a switch in every method breaks down, one class per state, the context that delegates, who owns transitions, the transition table, terminal states, stateless states as shared instances, how State differs from Strategy and from a plain switch, and where it shows up in LLD interviews.17 steps · 8 interview Q&As · Java example · used in 14 problems
  4. 4 Command patternTurn a request into an object so it can be queued, logged or undone.The Command pattern with a playlist editor: turning a request into an object, the invoker, receiver and command roles, undo and redo with two stacks, why a new edit clears redo, macro commands, queuing and logging commands, how Command differs from Strategy and Memento, where Java uses it, and where it appears in LLD interviews.16 steps · 8 interview Q&As · Java example · used in 6 problems
  5. 5 Chain of responsibility patternPass a request along a chain of handlers until it is dealt with.The Chain of Responsibility pattern with a refund approval example: why one big if-else breaks down, handlers linked by a next reference, the two styles (first handler wins, or every handler processes and any may stop), the end of the chain, order and testing, how it differs from Decorator and Observer, servlet filters, and where it shows up in LLD interviews.15 steps · 8 interview Q&As · Java example · used in 2 problems
  6. 6 Template method patternFix the steps of an algorithm in a base class and let subclasses fill in some.The Template Method pattern in Java with an order fulfilment example: a final method in an abstract class fixes the order of steps, subclasses fill in the steps that differ, and hooks give optional extension points. Covers when to use it, its costs, how it differs from Strategy, where the JDK uses it and how it appears in LLD interviews.16 steps · 8 interview Q&As · Java example · used in 1 problems
  7. 7 Iterator patternWalk through a collection without knowing how it is stored.The Iterator pattern in Java: walk a collection one element at a time without seeing how it is stored. Covers Iterator and Iterable, how for-each uses them, a lazy iterator over paged API results, a depth-first iterator over a tree, fail-fast behaviour and ConcurrentModificationException, safe removal, and where it appears in LLD interviews.17 steps · 8 interview Q&As · Java example · used in 1 problems
  8. 8 Mediator patternMake objects talk through one coordinator instead of to each other.The Mediator pattern in Java with a cinema ticket checkout: seat picker, snack picker, coupon field, price label and pay button stop calling each other and report to one coordinator that owns the rules. Covers the participants, code, when to use it, the god mediator risk, how it differs from Observer and Facade, and where it appears in LLD interviews.17 steps · 8 interview Q&As · Java example
  9. 9 Memento patternSave and restore an object's state without exposing its insides.The Memento pattern in Java with a trip planner: the itinerary saves its state into an opaque snapshot, a caretaker keeps undo and redo stacks without reading inside, and the itinerary restores itself. Covers deep copies, a private record as the memento, history limits and memory cost, Memento versus Command for undo, and where it appears in LLD interviews.16 steps · 8 interview Q&As · Java example · used in 1 problems
  10. 10 Visitor patternAdd new operations over a fixed set of classes without editing them.How the Visitor pattern adds new operations, like postage, labels and customs checks, to a fixed family of shipment classes without editing them: accept and visit methods, double dispatch step by step, the trade-off when a new type arrives, sealed interfaces and pattern matching as the modern Java alternative, and where Visitor shows up in LLD interviews.18 steps · 8 interview Q&As · Java example · used in 1 problems
  11. 11 Interpreter patternRepresent a small language as classes and evaluate sentences in it.How the Interpreter pattern turns a small grammar into classes and evaluates an expression tree, using a tiny discount rule language of our own: terminal and non-terminal expressions, a context with cart values, a small parser that builds the tree, when a hand-written interpreter is right and when to use a library instead, and how it relates to Composite and Visitor.17 steps · 8 interview Q&As · Java example · used in 2 problems