Pattern Relations: Bridge vs Adapter vs Strategy vs Decorator vs State
Five patterns that share composition and interface delegation—how their intents, lifecycles, and trade-offs differ.
TL;DR
Bridge, Adapter, Strategy, Decorator, and State all look nearly identical in a class diagram—one object delegates to an injected interface—but their intent differs: Bridge splits two hierarchies up front, Adapter translates an incompatible API, Strategy swaps an algorithm, Decorator stacks layers over the same interface, and State drives lifecycle transitions.
Why it exists
When you first learn GoF patterns, every diagram seems to be "Class A holds a reference to Interface B." Without a clear mental model of intent, teams misname wrappers, force State machines into Strategy switches, or build speculative Bridge hierarchies where a single Adapter was needed.
Mental model
Compare the five patterns by why the delegation boundary exists and who controls it:
| Pattern | Category | Structural shape | Intent | Who picks / swaps it |
|---|---|---|---|---|
| Bridge | Structural | Abstraction → Implementor | Decouple two independent hierarchies (M + N instead of M × N) | Caller pairs Abstraction + Implementor at construction |
| Adapter | Structural | Client → Target ← Adapter → Adaptee | Translate an incompatible existing/third-party interface | Composition root wires the vendor adapter |
| Decorator | Structural | Wrapper → Component (same interface) | Layer optional behaviors (logging, cache, retry) recursively | Caller stacks wrappers at construction |
| Strategy | Behavioral | Context → Strategy | Swap one interchangeable algorithm (rolling vs canary) | Client chooses and swaps the strategy |
| State | Behavioral | Context → State | Alter behavior and transition states as lifecycle events occur | State objects trigger transitions on the Context |
Do it
Decision guide: pick by intent
- Two orthogonal dimensions are multiplying subclasses (
M × N)? Use Bridge up front. - A third-party SDK or legacy module has the wrong method signatures? Wrap it with Adapter.
- You want to stack optional cross-cutting layers (retry, cache, log) over the same interface? Stack Decorator wrappers.
- The caller needs to swap one algorithm at runtime? Inject a Strategy.
- An object's behavior changes based on internal lifecycle phase (
Pending -> Running -> Failed) and transitions itself? Use State.
Same shape, different intent in TypeScript
Notice how Strategy and State share the exact same TypeScript composition mechanics, yet differ in who triggers the swap:
// Strategy: caller explicitly chooses the algorithm
interface DeployStrategy {
execute(service: string): string;
}
class DeployRunner {
constructor(private strategy: DeployStrategy) {}
setStrategy(next: DeployStrategy) { this.strategy = next; }
run(service: string) { return this.strategy.execute(service); }
}
// State: each state controls the next transition on the Context
interface PipelineState {
advance(ctx: PipelineContext): void;
readonly name: string;
}
class PipelineContext {
constructor(public state: PipelineState) {}
next() { this.state.advance(this); }
}
class QueuedState implements PipelineState {
name = 'queued';
advance(ctx: PipelineContext) {
ctx.state = new RunningState();
}
}
class RunningState implements PipelineState {
name = 'running';
advance(_ctx: PipelineContext) {
/* terminal or transition to CompletedState */
}
}When NOT to use it
Overengineering happens when you force a pattern label onto a simple 20-line module or introduce an interface hierarchy just to "match the pattern book." If a plain function parameter replaces Strategy, or a switch (status) over three enum values is clearer than five State classes, keep the simpler code.
Real-world example in this stack
Across components/diagram/ and lib/, this site uses Strategy for layout mode switching ('flow' vs 'tree' in components/diagram/layout.ts), Adapter in lib/source.ts to filter and adapt Fumadocs' MDX source into Next.js loader() routes, and Observer (IntersectionObserver / ResizeObserver) to lazy-mount <Flow> canvases.
Gotchas
- Confusing Adapter with Decorator: Adapter changes the interface (e.g.
AwsS3Sdk.send()->BlobStore.putObject()); Decorator keeps the exact same interface (HttpClient->HttpClient). - Turning a 3-state enum into State classes: A state machine with three tiny transitions is often clearer as a typed
switchwith exhaustiveneverchecking in TypeScript than a set of mutually referencing State classes. - Mislabeling Bridge as Strategy: If you only have one
Contextclass (no refined abstraction subclasses), you are using Strategy, not Bridge.
Production checklist
- Name classes after their domain role (
S3BlobAdapter,CanaryStrategy,RetryHttpClient)—never invent pattern boilerplate without a second variant or incompatible API. - Keep interfaces narrow (1–3 methods) so Adapters, Strategies, and Decorators stay easy to compose and fake in tests.
- When choosing between Strategy and State, check who owns transitions: caller = Strategy, internal lifecycle = State.
Check yourself
Check Yourself
1. Which pattern preserves the wrapped object's exact interface so multiple layers can be stacked recursively?
2. What is the key behavioral difference between Strategy and State?
Sources
- TypeScript Handbook: Object Types
- Refactoring.Guru: Strategy (Relations with Other Patterns)
- Dive Into Design Patterns (Alexander Shvets), chapters "Structural Design Patterns" and "Behavioral Design Patterns"