Skip to content
StackPractices

design

Practical resources about design for software engineers.

152 results

Patterns for Maintainable Code

Design patterns are not cookbook recipes — they are a shared vocabulary for discussing solutions to recurring problems. Knowing when to apply (and when to avoid) a pattern is a hallmark of senior engineering.

Browse our catalog of creational, structural, and behavioral patterns. Each entry includes a real-world scenario, UML diagrams, implementation in multiple languages, and a discussion of trade-offs. From Singleton to Outbox, find the right tool for your context.

beginner

Bridge Incompatible Interfaces with the Adapter Pattern

How to integrate legacy APIs, third-party libraries, and incompatible interfaces using object adapters, class adapters, and facade adapters in Java, TypeScript, and Python.

advanced

Scale Read and Write Workloads with CQRS

How to separate read and write models using Command Query Responsibility Segregation for optimized queries, event sourcing, and independent scaling of read and write paths.

advanced

Model Complex Business Domains with Domain-Driven Design

How to structure code around business concepts using bounded contexts, aggregates, entities, value objects, and domain events to manage complexity in large applications.

beginner

Create Objects Flexibly with the Factory Pattern

How to use factory methods, abstract factories, and dependency injection containers to decouple object creation from usage and improve testability.

intermediate

Build Maintainable Applications with Hexagonal Architecture

How to structure applications using ports and adapters to isolate business logic from frameworks, databases, and external services for testability and flexibility.

beginner

Implement Reactive Systems with the Observer Pattern

How to build event-driven, reactive systems using the observer pattern with pub/sub, event emitters, and reactive streams in JavaScript, Java, and Python.

beginner

Ensure a Single Instance with the Singleton Pattern

How to guarantee exactly one instance of a class exists in an application using lazy initialization, thread-safe creation, and registry-based singletons.

beginner

Swap Algorithms at Runtime with the Strategy Pattern

How to encapsulate interchangeable algorithms and behaviors using the strategy pattern with dependency injection, function pointers, and lambda strategies in Java, TypeScript, and Python.

intermediate

Pipes and Filters Pattern

Chain processing steps with independent filters connected by pipes. A pattern for data transformation pipelines where each step is reusable and composable.

intermediate

Abstract Factory for Cross-Platform UI Component Families

Create families of related objects without specifying concrete classes, enabling platform-specific implementations that share a common interface

intermediate

Abstract Factory Pattern

Create families of related objects without specifying concrete classes. A creational design pattern for consistent object families.

beginner

Active Record Pattern

Wrap a database table or view in a class where an instance is tied to a single row, and the class provides methods for CRUD operations directly on the object.

beginner

Adapter Pattern

Convert the interface of a class into another interface clients expect. A structural design pattern for interface compatibility.

beginner

Adapter Pattern for Integrating External REST APIs

Use the Adapter pattern to normalize responses from external REST APIs into a consistent internal model without leaking third-party formats into your domain

advanced

Aggregate Pattern

Encapsulate a cluster of domain objects treated as a single unit for data changes. An Aggregate Root controls access to its internal entities and value objects.

intermediate

Ambassador Pattern for Resilient Remote Service Access

Add a local ambassador that handles retries, circuit breaking, and monitoring when calling remote services, keeping the client simple and the service logic pure

intermediate

Back-Pressure Pattern

Prevent upstream systems from overwhelming downstream consumers by propagating flow-control signals backward through the pipeline, ensuring stable throughput under load.

intermediate

Backend for Frontend (BFF) Pattern

Create dedicated backend services tailored to the specific needs of each frontend client type, aggregating downstream APIs and optimizing data shapes per platform.

advanced

Blackboard Pattern

A shared knowledge space where independent specialized modules collaborate to solve complex problems by contributing partial solutions.

intermediate

Bridge Pattern

Decouple an abstraction from its implementation so both can vary independently. A structural design pattern for platform independence.

intermediate

Bridge Pattern for Decoupling UI Components from Themes

Separate an abstraction from its implementation so both can vary independently using the Bridge pattern for pluggable UI themes and rendering engines

intermediate

Builder Pattern

Construct complex objects step by step. A creational design pattern for readable, configurable object construction.

beginner

Builder Pattern for Complex Configuration Objects

Use the Builder pattern to construct complex configuration objects with optional parameters and sensible defaults without telescoping constructors

intermediate

Business Delegate Pattern

Reduce coupling between presentation and business tiers by introducing an intermediary that handles lookup, creation, and invocation of business services.

beginner

Cache-Aside Pattern

Load data into the cache on demand from the backing store. A caching pattern that gives the application full control over what and when to cache.

intermediate

Cache Invalidation Pattern

Strategies for keeping cached data fresh: TTL expiration, explicit invalidation, write-through, and event-driven cache eviction.

advanced

Cache Stampede Prevention Pattern

Prevent thundering herd cache misses with locks, single-flight, and early refresh strategies to protect the database from concurrent reloads.

intermediate

Chain of Responsibility for Request Processing Middleware

Pass requests along a chain of handlers where each handler decides whether to process the request or pass it to the next handler in the pipeline

intermediate

Chain of Responsibility Pattern

Pass requests along a chain of handlers until one handles it. A behavioral design pattern for decoupling senders and receivers.

intermediate

Circuit Breaker Pattern

Prevent cascading failures by stopping requests to failing services. An architectural pattern for resilient distributed systems.

intermediate

Claim Check Pattern

Store large payloads in external storage and pass only a lightweight reference token through the message bus, reducing broker load and preventing message size limits from being exceeded.

intermediate

Command Pattern

Encapsulate a request as an object, letting you parameterize clients with queues, logs, and undoable operations. A behavioral design pattern.

intermediate

Command Pattern with Undo/Redo in TypeScript

Implement the Command pattern to encapsulate requests as objects, enabling undo/redo operations, request queuing, and operation logging

advanced

Compensating Transaction Pattern

Undo the effects of a completed transaction by executing a counter-operation, enabling eventual consistency in long-running business processes across distributed services.

intermediate

Composite Entity Pattern

Map a coarse-grained entity to multiple database tables by composing dependent objects, reducing the number of fine-grained remote calls in EJB and distributed systems.

intermediate

Composite Pattern

Compose objects into tree structures to represent part-whole hierarchies. A structural design pattern for treating individual objects and compositions uniformly.

intermediate

Composite Pattern for UI Component Trees in React

Use the Composite pattern to compose objects into tree structures, letting clients treat individual objects and compositions uniformly in UI component hierarchies

beginner

Content Delivery Network (CDN) Pattern

Distribute static and live content through geographically dispersed edge servers to reduce latency, improve availability, and offload origin infrastructure.

intermediate

Context Object Pattern

Encapsulate state and services needed by multiple components into a single context object, reducing method signature bloat and decoupling code from specific environment details.

advanced

CQRS Pattern

Separate read and write operations into different models, optimizing each for their specific workload. A data pattern for scalable systems.

beginner

Data Access Object (DAO) Pattern

Abstract and encapsulate all access to a data source by exposing a clean interface while hiding persistence details from business logic.

intermediate

Data Mapper Pattern

Separate in-memory domain objects from the database by delegating persistence to a dedicated mapper layer, keeping models framework-agnostic.

intermediate

Database per Service Pattern

Give each microservice its own private database to ensure loose coupling, independent deployment, and technology heterogeneity across the application portfolio.

intermediate

Decorator Pattern

Add new functionality to objects dynamically by wrapping them. A structural design pattern for flexible behavior extension.

intermediate

Decorator Pattern for HTTP Request Pipelines

Use the Decorator pattern to compose cross-cutting concerns like logging, metrics, and retries into HTTP request pipelines without modifying core logic

intermediate

Dependency Injection Pattern

Supply dependencies from outside rather than creating them internally. An architectural pattern for decoupled, testable code.

intermediate

Dependency Injection Container in TypeScript

Build a lightweight DI container that resolves class dependencies automatically, enabling testable, loosely-coupled applications without frameworks like Angular or InversifyJS

intermediate

Distributed Lock Pattern

Coordinate mutually exclusive access to shared resources across distributed nodes using a consensus-based lock service, preventing race conditions in scaled-out systems.

intermediate

Domain Event Pattern

Capture and publish major occurrences within a domain model to decouple side effects from core business logic and enable reactive workflows.

intermediate

Eager Loading Pattern

Load related data in a single query rather than multiple round-trips, preventing the N+1 problem and improving read performance.

advanced

Entity-Component-System (ECS) Pattern

Compose entities from pure data components and process them with systems, enabling high-performance and flexible game object architecture without deep inheritance.

intermediate

Event Bus Pattern

Decouple components by routing events through a central bus. A behavioral pattern for loosely coupled communication between modules.

intermediate

Event-Carried State Transfer Pattern

Replicate state changes across services by publishing events that carry the full updated entity state, enabling consumers to maintain their own local copies without querying the source.

advanced

Event Sourcing Pattern

Store the state of an application as a sequence of events rather than storing only the current state. An architectural pattern for audit-friendly systems.

beginner

Facade Pattern

Provide a simplified interface to a complex subsystem. A structural pattern that hides implementation details behind a clean API.

beginner

Factory Pattern

Create objects without specifying the exact class to instantiate. A creational design pattern for flexible object creation.

intermediate

Flyweight Pattern

Share objects to support large numbers of fine-grained objects efficiently. A structural design pattern for memory optimization.

intermediate

Flyweight Pattern for Efficient Large-Scale Object Sharing

Use the Flyweight pattern to minimize memory usage by sharing as much data as possible between similar objects, essential for rendering large datasets

intermediate

Front Controller Pattern

Route all incoming requests through a single handler that dispatches to the appropriate page command, centralizing request processing and security.

intermediate

Gatekeeper Pattern

Place a validation and security boundary at the edge of a system to inspect, sanitize, and authenticate all incoming requests before they reach internal services.

intermediate

Graceful Degradation Pattern

Degrade functionality instead of failing when dependencies are unavailable. Serve partial results, cached data, or fallback features to keep users running.

intermediate

Idempotent Consumer Pattern

Process messages from a queue exactly once regardless of duplicates by using idempotent operations, unique identifiers, and deduplication strategies at the consumer level.

intermediate

Identity Map Pattern

Ensure each object is loaded only once per transaction by caching instances by their primary key, preventing duplicate in-memory representations of the same database row.

intermediate

Inbox Pattern

Use a dedicated inbox table or queue to record incoming events or requests, ensuring reliable delivery, deduplication, and idempotent processing even when downstream systems fail.

intermediate

Intercepting Filter Pattern

Compose cross-cutting concerns into a chain of pluggable filters that intercept requests and responses, enabling reusable preprocessing and postprocessing logic.

advanced

Interpreter Pattern

Define a representation for a language's grammar along with an interpreter that uses the representation to interpret sentences. A behavioral design pattern for mini-languages.

advanced

Interpreter Pattern for Domain-Specific Expression Languages

Build a language interpreter that evaluates expressions and rules by representing grammar as composable objects, useful for formulas, queries, and business rules

beginner

Iterator Pattern

Provide a way to access elements of a collection sequentially without exposing its underlying representation. A behavioral design pattern for traversal.

intermediate

Iterator Pattern for Custom Collection Traversal in

Provide a way to access elements of an aggregate object sequentially without exposing its underlying representation using the Iterator pattern

beginner

Manager Pattern

Encapsulate lifecycle, coordination, and access control for a set of related objects through a dedicated manager class that centralizes operations and enforces invariants.

beginner

Marker Interface Pattern

Use empty interfaces as metadata tags to signal properties or capabilities at compile time and runtime, enabling type-safe checks without modifying class behavior.

intermediate

Materialized View Pattern

Precompute and store expensive query results in a read-optimized cache to avoid repeated costly aggregation or joins across large datasets.

intermediate

Mediator Pattern

Define an object that encapsulates how a set of objects interact. A behavioral design pattern for reducing chaotic dependencies.

intermediate

Mediator Pattern for Loose Component Coupling in

Reduce chaotic dependencies between UI components by introducing a mediator that centralizes communication, preventing explicit references between peers

intermediate

Memento Pattern

Capture and restore an object's internal state without violating encapsulation. A behavioral design pattern for undo/redo.

intermediate

Memento Pattern for State Snapshot and Restoration

Capture and externalize an object's internal state without violating encapsulation, enabling undo, serialization, and state rollback in applications

beginner

Mixin Pattern

Add reusable behavior to classes without inheritance by composing methods from shared objects into a target class.

intermediate

Model-View-Presenter (MVP) Pattern

Separate presentation logic from the view by introducing a presenter that intermediates between the model and a passive view, enabling testable UI code.

intermediate

Model-View-ViewModel (MVVM) Pattern

Bind UI components declaratively to a ViewModel that exposes data and commands, enabling automatic synchronization between view and state.

beginner

Module Pattern

Encapsulate private state and behavior inside a self-contained unit with a public API. A structural pattern for organizing code into reusable, scope-safe modules.

intermediate

Multiton Pattern

Manage a map of named singleton instances, providing controlled access to a finite set of shared objects identified by keys.

beginner

MVC Pattern in Modern Frontend Applications

Apply the Model-View-Controller pattern to React and Vue applications to separate data, UI, and interaction logic for maintainable component architecture

beginner

Null Object Pattern

Use a default object instead of null references to eliminate null checks and simplify client code. A behavioral pattern for safer defaults.

intermediate

Object Pool Pattern

Reuse expensive objects instead of creating and destroying them repeatedly. A creational pattern for managing scarce resources efficiently.

beginner

Observer Pattern

Define a subscription mechanism to notify multiple objects about events. A behavioral design pattern for event-driven communication.

advanced

Outbox Pattern

Reliably publish domain events by persisting them in an outbox table within the same database transaction as the business operation.

beginner

Page Controller Pattern

Use a dedicated controller object for each logical page in a web application, handling the request and populating the view for that specific page.

beginner

Partial Class Pattern

Split a single class definition across multiple source files to separate auto-generated code from hand-written code, or to organize large classes by concern.

intermediate

Plugin Pattern

Enable third-party extensions by defining extension points in a host system that loads and executes live external modules.

intermediate

Priority Queue Pattern

Process tasks based on priority rather than arrival order, ensuring high-priority work gets resources before lower-priority tasks even if it arrived later.

intermediate

Prototype Pattern

Create new objects by copying existing ones. A creational design pattern for cloning and object duplication.

intermediate

Prototype Pattern for Object Cloning and Configuration

Create new objects by copying existing ones, allowing pre-configured templates and avoiding subclass explosion when object creation is expensive

intermediate

Proxy Pattern

Provide a surrogate or placeholder for another object to control access to it. A structural design pattern for access control, lazy loading, and logging.

intermediate

Proxy Pattern for API Response Caching

How to implement a caching proxy that intercepts API calls and stores responses to reduce latency and avoid redundant network requests

intermediate

Queue-Based Load Leveling Pattern

Introduce a queue between task producers and consumers to smooth out traffic spikes, decouple components, and prevent downstream services from being overwhelmed by burst workloads.

intermediate

Read-Through Cache Pattern

A transparent cache layer that intercepts read requests, fetches from the data source on miss, and populates the cache automatically.

advanced

Refresh-Ahead Cache Pattern

Proactively refresh cache entries before they expire to eliminate cache misses on hot keys and maintain consistent read latency.

intermediate

Registry Pattern

Centralize access to shared services and objects via a lookup table. A structural pattern that decouples consumers from concrete implementations.

intermediate

Repository Pattern with TypeScript Generics

Implement a type-safe repository pattern in TypeScript that decouples data access logic from domain services using generics and interfaces

intermediate

Retry Pattern

Retry an operation that has failed with transient errors, using configurable strategies like fixed delay, exponential backoff, or circuit breaker integration.

intermediate

Role Pattern

Assign dynamic roles to objects at runtime instead of hard-coding behavior in class hierarchies, enabling flexible identity changes without inheritance bloat.

advanced

Saga Pattern

Manage distributed transactions across multiple services by chaining local transactions with compensating actions for rollbacks. A microservices pattern.

advanced

Scheduler Agent Supervisor Pattern

Coordinate resilient job scheduling by separating scheduling logic from execution agents and adding a supervisor that monitors, restarts, and manages agent lifecycle.

intermediate

Sequential Convoy Pattern

Preserve message ordering for related messages in a distributed system by grouping them into ordered sequences and processing them one at a time through a single consumer.

advanced

Serverless Event Sourcing Pattern

Store function state as an append-only event log so workflows can be replayed, audited, and recovered without a persistent database.

intermediate

Serverless Fanout Pattern

Broadcast a single event to multiple independent consumers via SNS, EventBridge, or SQS so each consumer processes the event without coupling.

advanced

Serverless Function Composition Pattern

Chain serverless functions via Step Functions or orchestration layers to build multi-step workflows with retries, branching, and state management.

advanced

Serverless Throttling Pattern

Handle backpressure in serverless by using SQS, token buckets, and concurrency limits to protect downstream services from burst traffic.

intermediate

Serverless Warm Pool Pattern

Keep Lambda functions warm by sending periodic ping events to reduce cold start latency for latency-sensitive workloads.

advanced

Sharding Pattern

Split a large dataset into smaller partitions (shards) distributed across multiple servers to improve growth, performance, and availability beyond single-node limits.

beginner

Singleton Pattern

Ensure a class has only one instance and provide global access to it. A creational design pattern for controlled object creation.

intermediate

SOLID Principles in TypeScript with Practical Examples

Apply the five SOLID principles to TypeScript code to improve maintainability, testability, and reduce coupling in object-oriented designs

intermediate

Specification Pattern

Encapsulate business rules for selecting objects as reusable, composable predicate objects that can be combined with logical operators.

intermediate

State Pattern

Allow an object to alter its behavior when its internal state changes. A behavioral design pattern for finite state machines.

beginner

Static Content Hosting Pattern

Deploy static files to a dedicated content delivery network or object storage to offload origin servers, reduce latency, and improve availability for assets like images, CSS, and JavaScript.

beginner

Strategy Pattern

Define a family of algorithms, encapsulate each one, and make them interchangeable. A behavioral design pattern for flexible behavior selection.

beginner

Template Method Pattern

Define the skeleton of an algorithm in a base class, letting subclasses override specific steps without changing the algorithm's structure. A behavioral design pattern.

intermediate

Throttling Pattern

Limit the rate at which a system processes requests or consumes resources to prevent overload, ensure fair usage, and maintain predictable performance under varying load.

beginner

Timeout Pattern

Prevent operations from hanging indefinitely by enforcing a maximum execution time. A resilience pattern for predictable response times.

intermediate

Twin Pattern

Provide an alternative to multiple inheritance by linking two separate classes through mutual references, allowing them to delegate methods to each other as needed.

advanced

Two-Level Cache Pattern

Combine an L1 in-memory cache with an L2 distributed cache to reduce latency for hot keys while maintaining cache consistency across instances.

intermediate

Type Object Pattern

Define game object types as runtime data rather than hard-coding them as classes, enabling designers to create new entity variants without recompiling the codebase.

intermediate

Unit of Work Pattern

Track changes to in-memory objects during a business transaction and commit all updates atomically to the database, ensuring consistency.

intermediate

Value Object Pattern

Model domain concepts by value rather than identity. An immutable object defined by its attributes, not by a unique ID.

advanced

Visitor Pattern

Represent an operation to be performed on elements of an object structure without changing the classes of the elements. A behavioral design pattern.

advanced

Visitor Pattern for Extensible Operations on Object

Separate algorithms from the objects they operate on, allowing new operations to be added without modifying existing element classes

advanced

Write-Behind Cache Pattern

Write to cache synchronously and persist to the database asynchronously for high-throughput write workloads with eventual consistency.

intermediate

Write-Through Cache Pattern

Synchronously write to both cache and backing store so the cache always has the latest data without TTL-based invalidation.

intermediate

GraphQL Connection Pagination Pattern

Implement Relay-style cursor-based pagination with edges, nodes, and pageInfo for stable GraphQL list queries.

advanced

GraphQL Federated Entity Pattern

Share entity types across federated GraphQL services so the gateway can resolve fields from multiple subgraphs transparently.

intermediate

GraphQL Mutation Validation Pattern

Centralize input validation for GraphQL mutations using custom validators, schema directives, and structured error responses.

beginner

Feature Specification Template

A template for writing clear, actionable feature specifications that align engineering, product, and design before development begins.

beginner

User Story and Acceptance Criteria Template

A user story template that connects user needs to implementation with clear acceptance criteria, definition of done, and INVEST principles.

advanced

Clean Architecture

A practical guide to Uncle Bob's Clean Architecture: organize code into layers so that frameworks, UI, and databases are details, not dependencies.

advanced

CQRS — Command Query Responsibility Segregation

A complete guide to CQRS: separate read and write models to optimize performance, scalability, and team autonomy in complex domains.

advanced

Domain-Driven Design (DDD) — A Practical Guide

Learn DDD fundamentals: bounded contexts, entities, value objects, aggregates, and how to model complex business domains in code.

advanced

Event Sourcing — State as a Sequence of Events

A detailed analysis into Event Sourcing: persist state changes as events, reconstruct aggregates from history, and build audit trails by design.

advanced

Hexagonal Architecture — Ports, Adapters, and Testability

A complete guide to Hexagonal Architecture (Ports and Adapters): structure applications so domain logic is isolated from frameworks, databases, and external services.

beginner

Layered Architecture — N-Tier Explained

A practical guide to Layered (N-Tier) Architecture: separating presentation, business logic, and data layers with clear responsibilities and dependency rules.

intermediate

Modular Monolith — A Pragmatic Architecture

A practical guide to Modular Monoliths: combine the simplicity of monoliths with the modularity of microservices through clear bounded contexts and strict module boundaries.

intermediate

Onion Architecture — Dependency Inversion in Practice

A practical guide to Onion Architecture: organizing code around the domain model, enforcing dependency direction inward, and isolating infrastructure from business logic.

advanced

Software Architecture Guide

A guide to designing software architecture: monoliths vs microservices, layered architecture, data flow, and technology selection criteria.

intermediate

Vertical Slice Architecture: Feature-First Organization

A practical guide to Vertical Slice Architecture: organizing code by feature instead of technical concern, reducing cross-layer navigation and improving cohesion.

intermediate

Database Normalization — 1NF to 5NF Explained

A visual guide to database normalization: learn 1NF through 5NF with practical examples, when to apply each form, and how to balance normalization with performance.

beginner

Clean Code Principles: Writing Maintainable Software

A practical guide to clean code: meaningful names, short functions, DRY, SOLID foundations, and habits that make codebases easier to read and maintain.

beginner

What Works in Code Review — For Authors and Reviewers

A practical guide to useful code reviews: how to write reviewable code, give constructive feedback, and keep reviews fast and focused.

intermediate

Practical Design Patterns Guide

A guide to selecting and applying the right design pattern for common software engineering problems.

intermediate

SOLID Principles Explained with Examples

Learn the five SOLID principles with practical code examples: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion.

intermediate

Complete Guide to CSS Grid and Flexbox

Master modern CSS layout with Grid and Flexbox. Covers grid templates, areas, subgrid, responsive layouts, flexbox alignment, wrapping, gap, container queries, and when to use Grid vs Flexbox with practical examples and patterns.

intermediate

Complete Guide to Mobile Responsive Design

Build responsive layouts that work on every device. Covers CSS Grid, Flexbox, container queries, fluid typography, mobile-first breakpoints, and responsive images.

intermediate

Threat Modeling — A Practical Guide for Development Teams

A step-by-step guide to threat modeling: STRIDE, attack trees, data flow diagrams, and integrating security design review into your development process.

beginner

Test-Driven Development (TDD) — A Practical Workflow

Learn TDD step by step: write a failing test, make it pass, refactor. Red-Green-Refactor with real examples in Python, JavaScript, and Java.