Design patterns¶
The Gang of Four patterns plus the enterprise patterns (Repository, Unit of Work, Dependency Injection, Specification, Pipeline, Event Bus) that LLD interviews actually reward. Every page gives the intent, a classDiagram whose names match a tested module in code/patterns/, the Pythonic variant that is often enough, the classic confusions and the LLD problems where the pattern earns its place. Start with the overview and the P0 patterns - Strategy, State, Observer, Factory Method, Builder, Decorator, Command - which cover most of what interviewers ask you to name and justify.
Tiers: P0 must be solid before any interview, P1 is expected of a strong SDE2, P2 is stretch material for when the rest is done.
Start here (P0)¶
- Design patterns overview
- Singleton
- Factory Method
- Builder
- Adapter
- Decorator
- Facade
- Command
- Observer
- State
- Strategy
- Template Method
- Repository
- Dependency Injection
All pages¶
| # | Page | Tier | Covers |
|---|---|---|---|
| 1 | Design patterns overview | P0 | Catalogue table of all patterns (intent, one-line Python idiom); symptom-to-pattern guide; anti-patterns (pattern-itis); the classic… |
| 2 | Singleton | P0 | Intent: one instance, global access point. Example: thread-safe ConfigManager via new + lock (double-checked). |
| 3 | Factory Method | P0 | Intent: defer concrete class choice to a creator. Example: NotificationSenderFactory.create(channel). |
| 4 | Abstract Factory | P1 | Intent: families of related objects. Example: CloudProviderFactory producing (Storage, Queue) for AWS or GCP. |
| 5 | Builder | P0 | Intent: stepwise construction with validation. Example: fluent MeetingBuilder or HttpRequestBuilder with build() validation. |
| 6 | Prototype | P2 | Intent: clone existing objects. Example: Board.clone() for game-tree search. |
| 7 | Adapter | P0 | Intent: convert an interface into the one clients expect. Example: StripeAdapter(PaymentProcessor) wrapping a vendor SDK stub. |
| 8 | Bridge | P2 | Intent: decouple abstraction from implementation so both vary. |
| 9 | Composite | P1 | Intent: tree of part/whole treated uniformly. Example: FileSystemNode (File, Directory) with size(). |
| 10 | Decorator | P0 | Intent: add responsibilities dynamically. Example: Beverage add-ons; RetryingSender(Sender). |
| 11 | Facade | P0 | Intent: simple entry point to a subsystem. Example: CheckoutFacade.place_order(). |
| 12 | Flyweight | P2 | Intent: share intrinsic state across many objects. Example: Glyph/Style cache in an editor; shared chess piece types. |
| 13 | Proxy | P1 | Intent: control access to an object (lazy, caching, protection, remote). |
| 14 | Chain of Responsibility | P1 | Intent: pass a request along handlers until one handles it. Example: ATM cash dispenser (2000, 500, 100 notes). |
| 15 | Command | P0 | Intent: request as an object for undo, queueing and logging. |
| 16 | Interpreter | P2 | Intent: a grammar plus an evaluator. Example: rule engine for expressions such as amount greater than 100 AND country equals US. |
| 17 | Iterator | P1 | Intent: sequential access without exposing structure. Example: DFS/BFS iterator over a file tree; paginated iterator. |
| 18 | Mediator | P1 | Intent: centralize many-to-many interactions. Example: ElevatorController, ChatRoom. |
| 19 | Memento | P1 | Intent: snapshot and restore without breaking encapsulation. |
| 20 | Observer | P0 | Intent: one-to-many change notification. Example: PriceFeed notifying watchlists and alerts. |
| 21 | State | P0 | Intent: behaviour varies by state with explicit transitions. |
| 22 | Strategy | P0 | Intent: interchangeable algorithms behind one interface. Example: PricingStrategy (hourly, flat, daily cap). |
| 23 | Template Method | P0 | Intent: algorithm skeleton with overridable steps. Example: BoardGame.play() shared by tic-tac-toe and snake-and-ladder. |
| 24 | Visitor | P2 | Intent: add operations over an object structure without changing it. |
| 25 | Repository | P0 | Intent: collection-like persistence abstraction. Example: UserRepository Protocol with in-memory and sqlite3 implementations. |
| 26 | Unit of Work | P1 | Intent: track changes and commit atomically. Example: UnitOfWork context manager over repositories (sqlite3). |
| 27 | Dependency Injection | P0 | Intent: supply collaborators from outside. Example: OrderService(repo, payments, clock, notifier) with fakes in tests. |
| 28 | Null Object | P1 | Intent: a do-nothing implementation replaces None checks. Example: NullNotifier, NoDiscount. |
| 29 | Specification | P1 | Intent: composable business rules combined with and, or and not. |
| 30 | Object Pool | P1 | Intent: reuse expensive objects. Example: ConnectionPool with queue.Queue, acquire/release, health check. |
| 31 | Pipeline and Middleware | P1 | Intent: ordered stages that transform or short-circuit a request. |
| 32 | Event Bus | P1 | Intent: in-process pub/sub by topic, sync or async. Example: EventBus.subscribe(topic, handler), publish(event), worker-thread dispatch… |