Library

Volume 1 · Design Patterns

Notebook
Sign in

Cheat Sheet — Part 4: Design Patterns

696 words · about 3 min

Key Terms

Pattern In one line
Singleton One instance for the app lifetime — prefer a DI-registered singleton over a static one
Factory Method Create one product; a condition picks the concrete type
Abstract Factory Create families of related products that must stay consistent with each other
Builder Construct a complex object step by step; fluent, good for many optional parameters
Decorator Wrap an object to add behaviour without changing it — retry, telemetry, caching
Facade One clean interface over a messy subsystem
Adapter Make an incompatible interface fit the one you need
Proxy Stand-in that adds access control, laziness or caching
Sidecar Out-of-process decorator — the container-era version of the same idea
Strategy Swap an algorithm at runtime
Chain of Responsibility Pass a request along handlers until one deals with it — middleware pipelines
CQRS Separate read and write models
Event sourcing Store the sequence of events, derive state by replay
Repository / Unit of Work Collection-like data access / one transactional boundary across repositories

Numbers That Matter

Figure Value Where it bites
GoF categories Creational · structural · behavioural Plus a fourth enterprise tier you should name separately
ADR sections Status · context · decision · alternatives · consequences Alternatives is the section that proves you thought
Design question shape Clarify → high-level → deep dive → trade-offs → failure modes Never invert this order

Decision Rules

  • Prefer a DI-registered singleton to a static Singleton. Same one-instance-per-app guarantee, but the class depends on an abstraction, stays testable, and doesn’t hide a global. Good uses: connection pools, config, caches, distributed rate limiters.
  • Factory Method when creation is conditional and callers shouldn’t know the concrete type. Abstract Factory when you must guarantee a consistent family — mixing a Windows button with a Mac scrollbar is the failure it prevents.
  • Decorator over inheritance for cross-cutting concerns. Compose: core service → wrapped in retry → wrapped in telemetry. Each wrapper is independently testable and the stack is explicit at composition.
  • Use CQRS when read and write workloads scale differently, write-side domain logic is complex while reads are flat, or you genuinely need different models rather than different queries. It costs you eventual consistency between the sides — don’t pay that without the reason.
  • CQRS + event sourcing when you need a complete audit trail (financial, healthcare, compliance) or temporal queries. Event sourcing without a genuine audit or replay requirement is complexity for its own sake.
  • Strangler fig for every legacy migration. Route slices to the new system behind a facade, shrink the old one, delete it last. Big-bang rewrites fail for reasons that have nothing to do with engineering skill.
  • In a “design X” question, resist implementation detail. The failure modes are diving into SQL syntax or class names before the shape is agreed, and designing for a scale nobody asked for.

Say This Out Loud

“I wouldn’t reach for a classic static Singleton by default — I’d register the type as a singleton in the DI container. You get exactly the same guarantee, one instance for the application’s lifetime, but the consumer depends on an interface rather than a static accessor. That means I can substitute it in tests, I can see the dependency in the constructor signature instead of hidden inside a method body, and I haven’t introduced a global that any code anywhere can reach.”

“I’d use CQRS when the read and write sides have genuinely different shapes — complex invariant-heavy domain logic on writes, flat denormalized projections on reads, and very different scaling profiles. What it costs is eventual consistency between the two sides, and a user who saves something and doesn’t see it in the list is a real product problem. So I’d only pay it when the asymmetry is real, and I’d add event sourcing on top only when there’s an actual audit or replay requirement.”

“For a ‘design X’ question I clarify requirements first, then agree the high-level shape, then go deep where the interviewer steers, then state trade-offs and failure modes. The two ways I’ve seen strong engineers lose that question are diving into specific SQL syntax before the boxes are agreed, and designing for a scale nobody asked for.”