Cheatsheet
Design patterns cheatsheet: GoF families, modern additions, decision guidance.
Design patterns — cheatsheet
EXAMPLE
// ===== Creational ===== // Singleton one instance globally (use sparingly) // Factory Method subclass decides what to instantiate // Abstract Factory create families of related products // Builder step-by-step construction of complex objects // Prototype clone an existing instance // ===== Structural ===== // Adapter bridge two incompatible interfaces // Bridge separate abstraction from implementation // Composite tree of nested objects with uniform interface // Decorator stack behaviour without inheritance // Facade unified interface over a subsystem // Flyweight share immutable parts between many instances // Proxy stand-in for a real object (lazy load, access control) // ===== Behavioural ===== // Chain of Responsibility pass request through handlers // Command encapsulate an action as an object (undo, queue) // Iterator sequential access to a collection // Mediator coordinate object interactions through a hub // Memento snapshot + restore state // Observer pub/sub for state changes // State behaviour changes with internal state // Strategy interchangeable algorithms // Template Method skeleton with subclass-filled steps // Visitor operations across an object hierarchy // ===== Modern additions ===== // Repository data access layer abstraction // Unit of Work transaction-bounded persistence // Dependency Injection inject dependencies via constructor // Specification composable query predicates // CQRS command/query separation // Event Sourcing state = sequence of events // Saga long-running distributed transactions // Hexagonal / Ports + Adapters // Clean / Onion architecture // ===== Decision guidance ===== // Repeated branching on type -> Strategy / Polymorphism // Choosing impl at boot -> Factory / DI // Composable behaviour -> Decorator // Cross-cutting events -> Observer / EventBus // Many related products -> Abstract Factory // Long-running workflow -> Saga // Audit-heavy domain -> Event Sourcing + CQRS // Tree-like composition -> Composite // Legacy bridge -> Adapter // Snapshot + undo -> Memento // Persistence boundary -> Repository + Unit of Work // ===== When NOT to use ===== // - Most code does not need a pattern; one class often does // - Applying patterns without the problem -> ceremony // - Singleton as 'global by another name' -> hides dependencies // - Visitor where pattern matching would do // - 5-class factories for a single product // ===== Composability ===== // - Strategy inside a Template Method // - Observer notifying Commands // - Decorator stacking around a Repository // - State machine emitting Events that drive Sagas // ===== Languages with built-in alternatives ===== // - Pattern matching: replaces Visitor in many cases (Rust, Scala, Swift, C#) // - First-class functions: simpler Strategy / Command // - Sealed classes: closed hierarchies replace Composite + Visitor combos // - Async: removes a lot of Future / Promise pattern ceremony
Why it matters
Design patterns cheatsheet: GoF creational/structural/behavioural + modern (Repository, DI, CQRS, Event Sourcing, Saga, Hexagonal). Pick by the change driver, not the chapter title. Modern language features replace many of the classics; reach for them first.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
// Creational: Singleton Factory Builder Prototype // Structural: Adapter Decorator Facade Proxy Composite // Behavioural: Observer Strategy Command State Iterator VisitorTry it Yourself »
Discussion
Loading…