Pattern Cheat Sheet
All 58 patterns on one page. Keep this open during architecture reviews, system design interviews, or code review sessions.
PatternSimple — Design Pattern Cheat Sheet
All 58 patterns · patternsimple.dev
Creational — How objects are created
5 patternsEnsure a class has only one instance, and provide a global point of access to it.
→You need exactly one shared resource — like a logger, config manager, or connection pool.
→The object is expensive to create and you want to reuse it across the application.
Define an interface for creating objects, but let subclasses decide which class to instantiate.
→You don't know at compile time which class you need to instantiate.
→You want to centralise and control object creation logic in one place.
Create families of related objects without specifying concrete classes
→You need to create families of related objects that must be used together
→Your system needs to be independent of how its products are created
Separate construction from representation
→Creating objects with many optional parameters or configurations
→Building complex objects with a multi-step construction process
Create objects by cloning an existing prototype
→Object creation is expensive (database queries, API calls, heavy computation)
→You need multiple similar objects with slight variations
Structural — How objects are composed
7 patternsConvert incompatible interfaces into compatible ones
→Integrating with third-party libraries that have incompatible interfaces
→Working with legacy code that you cannot or should not modify
Decouple abstraction from implementation so they can vary independently
→Separating abstraction from implementation when both should be extensible independently
→Avoiding permanent binding between abstraction and implementation
Compose objects into tree structures to represent hierarchies
→Building hierarchical tree structures (file systems, organizational charts, UI components)
→When you need to treat individual objects and groups of objects identically
Add responsibilities to objects dynamically
→Adding features to objects without modifying their original class
→Creating flexible combinations of behaviors (coffee with milk, sugar, and chocolate)
Provide a simplified interface to a complex subsystem of classes.
→A subsystem is complex and most callers only need a simple subset of its functionality.
→You want to layer your architecture — expose a clean API while hiding implementation details.
Share fine-grained objects efficiently to reduce memory usage
→Creating many similar objects where memory is a concern
→Objects have much shareable (intrinsic) state vs. unique (extrinsic) state
Control access to another object through a surrogate
→Lazy loading: delay expensive object creation until actually needed
→Access control: restrict who can use an object or what they can do
Behavioral — How objects communicate
10 patternsPass a request along a chain of handlers. Each handler decides either to process the request or pass it along the chain
→Multiple objects can handle a request and the handler isn't known in advance
→Request handlers should be determined dynamically at runtime
Encapsulate a request as an object, allowing you to parameterize clients with different requests, queue them, and log them
→You need to queue requests and execute them later
→Implementing undo/redo functionality
Provide a way to access the elements of an object sequentially without exposing its underlying representation
→You need to access collection elements without exposing the internal structure
→You want to support multiple simultaneous traversals of the same collection
Define an object that encapsulates how a set of objects interact
→Objects have many interdependencies creating complex communication
→You want to decouple objects from directly knowing each other
Capture and externalize an object's internal state without violating encapsulation, allowing it to be restored later
→You need undo/redo functionality
→You want to save and restore object state
Define a one-to-many dependency where when one object changes state, all its dependents are notified automatically
→You need to notify multiple objects when state changes occur
→Objects interested in updates don't know each other in advance
Allow an object to alter its behavior when its internal state changes
→Object behavior depends on state and changes at runtime
→You have many conditional statements (if/else or switch) based on state
Define a family of algorithms, encapsulate each one, and make them interchangeable
→You have multiple ways to solve a problem and need to select at runtime
→You want to avoid conditional statements (if/else or switch) scattered throughout code
Define the skeleton of an algorithm in a base class, deferring some steps to subclasses
→Multiple classes implement similar algorithms with slight variations
→You want to avoid code duplication of common algorithm structure
Represent an operation to be performed on elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates
→You have a complex object structure with many different element types
→You need many different, unrelated operations on the structure
AI Agent & LLM Patterns
18 patternsOne agent transfers full conversational control to a specialized peer — the receiving agent takes over completely, with context, not just a subtask.
→A general agent needs to route to a specialist who will own the entire conversation going forward
→Different topics within one product need different system prompts, tools, or model tiers
Prevent hallucinations and stale answers by retrieving real, relevant documents before the LLM generates a response.
→You need up-to-date information beyond the LLM's training data
→Domain-specific or proprietary knowledge is critical
Break down complex problems into step-by-step reasoning to improve answer quality.
→Problems require multi-step reasoning or mathematical calculations
→Questions involve logic, planning, or causal reasoning
Combine reasoning and action: think through steps and execute tools to solve tasks.
→Tasks require gathering information from external sources
→Actions must be taken based on intermediate reasoning
Give LLMs the ability to call real functions — search the web, run code, query databases, and interact with APIs autonomously.
→Tasks require real-time data the LLM can't know (weather, stock prices, live search)
→You need deterministic, testable execution — e.g. exact database queries, calculations
Coordinate multiple specialized agents working together to solve complex problems.
→Tasks require diverse expertise (research, coding, design, review)
→Different subtasks would benefit from specialized agents
Generate, critique, and refine outputs iteratively to improve quality.
→Quality is more important than speed (creative writing, analysis)
→Complex tasks benefit from revision (code generation, essays)
Maintain context across interactions using short-term and long-term memory systems.
→Multi-turn conversations longer than context window
→Agents need to learn from interactions across sessions
Validate inputs and outputs to ensure safety, quality, and compliance.
→Safety and content moderation are critical
→Regulatory compliance is required (HIPAA, GDPR)
Constrain LLM responses to a schema so downstream code can reliably parse and act on them.
→You need to extract specific fields from unstructured text
→Downstream code must act on LLM output programmatically
Pause agent execution and hand off to a human when confidence is low, stakes are high, or an action is irreversible.
→The agent is about to perform an irreversible action (delete, send, charge, deploy)
→Confidence score falls below a safe threshold
Classify incoming requests and direct them to the best-fit model, agent, or handler.
→You have multiple models with different cost/capability trade-offs
→Different request types benefit from specialized agents or prompts
Separate prompt structure from dynamic content to build reusable, testable, and versionable LLM instructions.
→The same prompt structure is reused with different inputs across the codebase
→You need to version, A/B test, or iterate on prompts without touching application code
Keep what's relevant in the limited context window and evict the rest — without losing coherence.
→Conversations or agentic loops that run longer than a few exchanges
→Document Q&A where the source is larger than the context window
Fan out a task to multiple agents or tools in parallel, then synthesize their results into a single answer.
→Tasks decompose into independent subtasks with no data dependencies between them
→You need information from multiple sources simultaneously (search, databases, APIs)
Gracefully degrade when a model or tool fails — retry, switch providers, or return a safe default.
→Your AI feature must stay available even during provider outages
→You're calling external tools or APIs that can fail
One coordinator plans and delegates; many stateless workers execute — then results flow back up.
→Tasks decompose cleanly into independent, parallelizable subtasks
→Different subtasks require different tools, expertise, or model tiers
Emit LLM tokens to the client as they're generated — don't wait for the full response.
→Any user-facing AI feature where the response takes more than 1–2 seconds
→Chat interfaces, writing assistants, code generation — anywhere the user reads while the model writes
ML System & MLOps Patterns
8 patternsCentralize feature computation, storage, and serving so training and inference always use the exact same data.
→Multiple models sharing overlapping feature sets
→Strict SLA on serving latency (need pre-computed online features)
A versioned catalog of trained ML models with metadata, lineage, and stage transitions from staging to production.
→Multiple models in production (need to track which is deployed)
→Regulatory compliance requires audit trail of model decisions
Automated, reproducible workflows that ingest data, train models, evaluate them, and register successful runs.
→Regular retraining needed (daily/weekly as new data arrives)
→Multiple models being trained independently (consolidate into one pipeline)
Deploy ML models behind a scalable, low-latency API that handles feature lookup, prediction, and response transformation.
→Real-time predictions needed (< 100ms SLA)
→High QPS (queries per second) requiring horizontal scaling
Track datasets like code with immutable versions so any model run can be reproduced months later.
→Models require reproducibility (audit trail, regulatory compliance)
→Frequently retraining with new data versions
Continuously track model performance, input distributions, and prediction drift to catch silent failures in production.
→Models in production serving real users
→Need to detect silent failures (accuracy degradation without labels)
Safely roll out new models by routing a fraction of traffic to the candidate and comparing business metrics against the control.
→Deploying a new model to production with business impact
→Want to prove statistical significance before promoting
Train a base model further on your own data to improve quality, consistency, or efficiency on a specific task.
→You have 50–1000+ high-quality labeled examples for a specific task
→Prompt engineering isn't achieving the quality or consistency you need