Design Patterns
design-patterns
Practice Problems
Design Add and Search Words
Design a data structure that supports adding words and searching for words with wildcard characters, where '.' can match any single letter.
Implement Trie (Prefix Tree)
Implement a trie (prefix tree) that supports inserting words, searching for exact words, and checking if any word starts with a given prefix.
Code Snippets
Sealed Interfaces with Pattern Matching
Sealed interfaces (Java 17+) plus switch pattern matching (Java 21+) give Java algebraic-data-type ergonomics: a closed family of subtypes that the compiler can check exhaustively in one switch. This snippet shows the modern syntax in comments and a Java 13-compatible equivalent using a visitor-style abstract class hierarchy that compiles in our test runner. Use this pattern for tagged unions like `Result<Ok, Err>`, AST nodes, and state machines.
Proxy Patterns: Validation, Logging, Defaults
`Proxy` lets you intercept the basic operations on an object (get, set, has, deleteProperty) and run your own logic before or instead of the default behavior. This snippet shows three of the most useful patterns: a validating Proxy that rejects bad writes at the source, a logging Proxy that records access for debugging, and a defaults-plus-locked Proxy that supplies fallbacks and freezes keys after init. Use sparingly because Proxy adds a small per-access cost; reach for it when you need cross-cutting policy on a plain data object.
Question Banks
Design Patterns Walk-Through
Hard drills on Singleton, Strategy, Observer, and Decorator. Concrete Java implementations to read, critique, and extend, plus one thread-safety twist.
Event Delegation and Event Bus Quiz
Practice DOM event delegation, build a minimal pub/sub event bus, and roll your own dispatcher so cross-component communication stays decoupled.
JavaScript Decorators and Metaprogramming Quiz
Wrap methods with cross-cutting concerns: a read-only decorator, a method-timing decorator, and the stage-3 decorator proposal that landed in TC39.
React Router and Code Splitting Quiz
Four drills on declarative routing with react-router and using React.lazy plus Suspense to split bundles per route or per heavy component.
React Component Patterns and Composition Quiz
Six drills on the classic React patterns: composition over inheritance, render props, higher-order components, compound components, and slot-style children.
React Lifecycle and Class Component Quiz
Four drills on legacy class lifecycle methods that still show up in interviews and codebases: mount/update/unmount, getSnapshotBeforeUpdate, setState batching, and getDerivedStateFromProps.
React Code-Splitting: Two Explanations Quiz
Two explanations of code-splitting (bundler-level vs `React.lazy` + `Suspense`) plus companions on preloading routes and on splitting non-component utilities.
React HOC for Conditional Render (Auth-Aware): Two Approaches Quiz
Two approaches to a conditional-render HOC for authentication (props-based gate vs context-based gate), plus companions on the hook equivalent and on the HOC return-type contract.
Community
Python Decorators Explained with Five Real Examples
Decorators stop being magic the moment you see five real ones in a row: timing, caching, auth, retry, and rate limiting. Here is the pattern, the gotchas, and the line where I stop reaching for them.
The Builder Pattern: When the Constructor Isn't Enough
The Builder pattern earns its keep when constructors blow up to ten parameters or when an object's invariants depend on a multi-step assembly. The shape, the cases that justify it, and the imitations that don't.
The mapStateToProps / mapDispatchToProps Cheatsheet I Wish I Had In 2018
Every React + Redux codebase from before hooks revolves around connect. Three accordions of the wiring I keep paged in for inheriting one of those repos.
Functional Core, Imperative Shell, Explained
The architecture pattern that gives you most of functional programming's testability without a rewrite. The two-layer rule, where each layer's responsibilities sit, and the failure modes of mixing them.
Type Hints, mypy, and the Runtime Truth
Python type hints are documentation that a static checker reads. The runtime ignores them. Here is what hints do, what mypy adds, and the libraries that validate at runtime on purpose.
The "Component Switch" Pattern I Use Instead of Big Render Trees
Big if/else ladders that pick a component by a tag are unreadable by the third branch. The pattern I use instead is a tiny lookup registry. Three accordions on shipping it.
Composition Over Inheritance, with Real Examples
The slogan everyone repeats and almost no one operationalizes. What composition actually looks like at the field level, where inheritance is still right, and the refactor when you've gone too deep.
Four Things I Forget About Create-React-App Every Year
Cheat sheet for the CRA quirks that keep coming back. Absolute imports via jsconfig, the HTTPS dev flag, the registerServiceWorker mystery, and the REACT_APP_ env var rules.
The HOC + Render Props Patterns I Still Read in Legacy Repos
Hooks made HOCs and render props optional, but pre-2019 codebases still ship them. Four patterns to recognize when you inherit a Redux-era React app.
The Strategy Pattern: The Cleanest Way to Kill an if-else Chain
Strategy is taught as an OO pattern, but in practice it is just a function with a name. How to recognize the if-else chains that genuinely deserve the refactor and the ones that don't.
Three React Router v4 Recipes I Inherit With Old Codebases
RRv4 still survives in long-running codebases. Three recipes I keep paged in: programmatic navigation via withRouter, query parsing, and a custom history singleton for non-React callers.
Building Smarter Code: Singleton & Factory Method
Singleton is the pattern most teams over-apply, and Factory Method is the one most teams reach for too late. Where each pays off, where each rots, and the rules I follow.
The Observer Pattern and Why React Rediscovered It
Observer is the pattern most engineers think they know, until they look at React's render loop, signals, or RxJS and realize each one is a different observer dialect. The shape, the variants, and the gotchas.
The Decorator Pattern vs Language Decorators
The GoF Decorator pattern and Python's @decorator syntax share a name and almost nothing else. What each one really does, where they overlap, and the wrap order that catches everyone.
dataclasses, attrs, and pydantic: Pick One
Three libraries solve the data-container problem and they answer different questions. dataclasses for internal objects, attrs for power-user customisation, pydantic for validating external input.
Pure Functions, Immutability, and the Trade-offs
Pure functions and immutability are not free. The honest accounting of the wins (testability, reasoning, parallelism) and the costs (allocation, ergonomics, language fit), and where the line falls in real code.
Route-Level Code Splitting With React.lazy
Our initial bundle was 2.1MB. Splitting routes via React.lazy plus Suspense dropped it to 340KB on first paint. Three accordions on how I wire it.
Python Packaging in 2026: pyproject, uv, and pipx
Python packaging finally converged. pyproject.toml is the source of truth, uv replaced pip plus venv plus pip-tools, and pipx owns global CLIs. The modern toolchain and the migration order that does not blow up CI.
The CSS-in-JS Tradeoffs I Keep Explaining to Juniors
Five conversations I keep having: the styling-approach overview, build-time vs runtime CSS-in-JS, CSS Modules vs Tailwind, the styled-components performance trap I keep flagging, and when to walk back to plain CSS.
Adapter and Facade: The Two Patterns I Actually Use
Two structural patterns that survive every codebase: Adapter (when you cannot change the other side) and Facade (when you cannot change yours). The shape of each, and the small tells that say which one fits.
Dependency Injection Without a Framework
DI is a pattern, not a framework. The plain-language version (pass things in instead of newing them inside) covers ninety percent of cases. The trade-off versus DI containers, and where each one breaks down.
