Software Design Patterns: 23 GoF Patterns and Practical Examples


Software design patterns are reusable descriptions of how to organize code to address a problem that appears repeatedly during software development. A pattern identifies a context, the forces or constraints involved, and a general arrangement of responsibilities that has proved useful in that context. It is not a finished component that can be copied into every project. It is a design idea that developers adapt to the language, framework, domain, team, and operational environment they are working with.
The distinction matters. A pattern does not prescribe one exact class diagram, method signature, or implementation. The same Strategy pattern might be represented by a family of classes in Java, a set of functions in Python, interfaces and structs in Go, or closures in JavaScript. The essential idea is that an algorithm or policy can be selected independently of the code that uses it. The implementation changes; the design relationship remains recognizable.
The best-known catalog comes from Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, whose 1994 book, Design Patterns: Elements of Reusable Object-Oriented Software, documented 23 object-oriented patterns. These are commonly called the Gang of Four (GoF) patterns. They remain useful because they give developers a shared vocabulary for discussing object creation, composition, and collaboration. They are not the only patterns in software engineering: application architecture, enterprise integration, distributed systems, data processing, and user-interface development all have their own recurring solutions.
A pattern description usually includes several elements:
For example, a notification service may need to send email, text messages, and push notifications. If the business workflow contains a large conditional statement for every provider, adding another channel can require changes in several places. A Strategy design allows each delivery method to implement a common capability while the application chooses the appropriate strategy at runtime. The pattern does not decide which provider is best, how credentials are stored, or how delivery failures are retried. Those are separate design and operational decisions.
Patterns are most valuable when they help a team reason about a recurring problem. They are not a measure of sophistication, and a system with more patterns is not automatically better designed.
A well-chosen pattern can make a codebase easier to understand, extend, test, and discuss. Naming a familiar solution gives developers a concise way to communicate intent. Saying that a component uses an Adapter, for instance, immediately suggests that it translates one interface into another and protects the rest of the application from a dependency's specific API.
Patterns can also help manage change. If an application is likely to support several payment providers, placing provider-specific behavior behind an interface can prevent payment rules from spreading throughout the checkout workflow. If a report can be exported in multiple formats, a suitable creation or Strategy approach can isolate format-specific details. If several objects must react to a business event, an Observer or publish-subscribe approach can reduce direct dependencies between the publisher and its consumers.
Other benefits include lower coupling, clearer responsibilities, improved testability, more consistent design, easier onboarding, and safer evolution. When components depend on stable contracts instead of concrete implementation details, changes can often be localized. When each component has a focused responsibility, teams can reason about behavior without understanding the entire application.
However, patterns do not guarantee scalability, security, performance, or maintainability. Those qualities depend on the complete system, including its data model, infrastructure, deployment process, team practices, and workload. A Decorator cannot compensate for an inefficient database query. A Factory cannot repair unclear domain boundaries. A microservices architecture does not automatically make a product resilient.
Every abstraction also has a cost. Additional interfaces, objects, indirection, configuration, and files can make a simple operation harder to follow. A pattern may be technically correct yet unsuitable if the team does not need the flexibility it introduces. The goal is not to maximize reuse at any cost; it is to make the design easier to change without making the present unnecessarily complicated.
A useful rule is to start with the clearest solution that satisfies current requirements. Introduce a pattern when repeated variation, tangled dependencies, duplicated decisions, or difficult testing reveal a concrete need. This is consistent with refactoring: improving internal structure while preserving externally observable behavior. The Refactoring.Guru design pattern catalog is a useful reference for comparing classic patterns and their trade-offs.
The GoF catalog groups its 23 patterns by their primary intent: creational, structural, and behavioral. The categories are a learning aid, not rigid boundaries. A real application may combine patterns from all three categories to solve one business problem.
Creational patterns deal with how objects are created. They help separate the code that requests an object from the code that knows which concrete implementation to instantiate or how to assemble it. They are useful when creation logic is complex, varies by configuration, or needs to be isolated for testing.
Structural patterns deal with how objects and classes are composed. They help build larger structures while keeping interfaces and dependencies manageable. They are useful when integrating incompatible systems, adding responsibilities, simplifying complex subsystems, or representing hierarchies.
Behavioral patterns deal with communication and responsibility among objects. They help organize algorithms, workflows, state changes, and notifications. They are useful when behavior varies, when several components need to collaborate, or when conditional logic has become difficult to maintain.
The categories should not be treated as a checklist. A developer does not need to use one pattern from each category in every application. The problem determines the appropriate design.
Creational patterns provide alternatives to scattering direct object construction throughout an application. They are especially useful when the concrete type, configuration, or construction sequence can vary.
Factory Method defines a creation operation while allowing a creator or its specialized implementation to decide which concrete product is returned. The calling code works with the product abstraction rather than constructing a specific class directly.
Imagine an application that imports documents in PDF, CSV, and JSON formats. The import workflow should not need to know the details of every parser. A factory method can select a parser based on the file type and return an object that implements a shared parsing interface.
Use this approach when specialized creators genuinely need to vary object creation. For a small application with only one or two simple object types, a straightforward function may be clearer than a hierarchy of factories.
Abstract Factory creates families of related objects without exposing their concrete classes. It is useful when several components must be compatible with one another.
A desktop application might support multiple visual themes, each providing a matching set of buttons, menus, and dialog components. An abstract factory can supply the entire family for the selected theme. The client code works with common interfaces and does not need separate conditionals for each widget.
This pattern is less compelling when the family of products is unlikely to vary or when adding a new product type would force extensive changes to every factory. Consider whether configuration, dependency injection, or simpler factory functions are sufficient.
Builder separates the step-by-step construction of a complex object from its final representation. It is useful when an object has many optional settings, validation rules, or construction stages.
A report may require a date range, data source, filters, grouping rules, output format, and access policy. A builder can make construction explicit and validate that required values are present before producing the report configuration.
Builders should not be introduced merely because a constructor has a few parameters. Named arguments, configuration objects, or data classes may provide a simpler solution.
Prototype creates new objects by copying an existing configured instance. It can be useful when creating a new object from scratch is expensive or when many objects begin with a shared configuration.
A simulation system might keep a configured template for a particular type of entity and clone it when new instances are needed. The important design question is whether the copy should be shallow or deep. A shallow copy may share nested mutable objects with the original, while a deep copy attempts to copy nested values too. Neither is automatically correct; the desired ownership and sharing semantics must be explicit.
Singleton restricts a class to a single instance and provides a shared access point. It is sometimes appropriate for process-wide immutable configuration or a resource whose lifecycle is deliberately managed by the application.
It is also easy to misuse. A global mutable Singleton can hide dependencies, complicate parallel tests, create initialization-order problems, and become a bottleneck. A database connection pool, for example, is usually better managed by the application's dependency-injection or resource-lifecycle system than by an improvised global Singleton. A pool manages multiple connections and concurrency; it is not equivalent to one shared connection.
Before choosing Singleton, ask whether a module-level value, an explicitly constructed service, or a dependency-injection container would be easier to reason about. Prefer explicit ownership over hidden global state.
Structural patterns organize relationships between components. They are particularly helpful when a system needs to integrate external dependencies or compose functionality without creating deep inheritance hierarchies.
Adapter translates one interface into another expected by a client. It is useful when integrating a third-party service, legacy component, or library whose interface does not match the application's own contract.
A payment application may define an internal PaymentGateway interface with operations such as authorize, capture, and refund. A provider adapter translates those operations into the specific request and response formats of each external payment service. The business workflow can then depend on the application's contract rather than on a vendor's SDK.
An Adapter should translate behavior and data carefully, including error handling and idempotency semantics. It should not simply rename methods while allowing provider-specific exceptions and data structures to leak through the boundary.
Bridge separates an abstraction from its implementation so that both can vary independently. It is useful when two dimensions of variation would otherwise create a large class hierarchy.
Imagine a notification feature that supports several notification types and several delivery channels. Combining every notification type with every channel through inheritance can lead to many classes. A bridge-like design keeps the notification concept separate from the delivery mechanism, allowing the two dimensions to evolve independently.
Composite represents part-whole hierarchies as trees and lets clients treat individual objects and groups uniformly. It is useful for file systems, document editors, organization charts, and nested UI components.
A document may contain paragraphs, sections, and nested sections. Each element can expose operations such as render or calculate word count. A section delegates the operation to its children, while a paragraph performs the operation directly. The client can work with a common component abstraction instead of checking every node type.
The design must still define how empty groups, child ownership, and invalid operations behave. Recursive structures can become difficult to maintain if their lifecycle rules are unclear.
Decorator adds responsibilities to an object by wrapping it with another object that implements the same interface. It supports flexible composition without creating a subclass for every possible combination of features.
A data reader might be wrapped with compression, encryption, buffering, or metrics functionality. Each decorator adds one focused behavior and delegates the core operation to the wrapped component. The order of decorators can matter, so it should be documented and tested.
Python's function decorators use similar terminology and can implement related ideas, although the classic object-oriented Decorator pattern and Python's decorator syntax are not identical in every use.
Facade provides a simpler interface to a complex subsystem. It is useful when a client should coordinate several internal components without depending on their details.
An order-placement facade might coordinate inventory checks, pricing, payment authorization, and order persistence. The facade gives the calling application one clear operation while keeping subsystem coordination in a dedicated place.
A facade should simplify access, not become a giant service that owns every business rule. Keep domain decisions in the appropriate domain or application services.
Flyweight reduces memory use by sharing common, reusable state across many similar objects. It is most useful when an application creates a very large number of objects that repeat substantial immutable data.
A text editor, for example, may represent many characters while sharing font and style data instead of storing a full copy of that data for every character. The design distinguishes shared intrinsic state from context-specific extrinsic state.
Flyweight introduces lookup and lifecycle complexity. Measure memory use before adopting it; modern runtimes and data structures may already provide adequate sharing.
Proxy provides a substitute object that controls access to another object. A proxy may implement lazy loading, access checks, caching, logging, or remote communication while presenting a compatible interface.
A virtual proxy can defer loading a large document until it is needed. A protection proxy can check authorization before forwarding a request. A remote proxy can hide the mechanics of communicating with a service over a network.
A proxy is not a substitute for security at the actual trust boundary. Authorization must still be enforced by the system that owns the protected resource.
Behavioral patterns help define how responsibilities and control flow are distributed among collaborating components.
Chain of Responsibility passes a request through a sequence of handlers. Each handler can process the request, reject it, or pass it to the next handler.
Web middleware is a familiar example. A request may pass through authentication, authorization, validation, logging, and routing components. Each middleware handles one concern and can stop processing when a condition fails.
The chain should have clear ordering, termination, and error-propagation rules. A request should not silently skip a required security or validation step.
Command represents a request as an object or callable. This makes it possible to queue, log, retry, schedule, or undo operations when the domain supports those behaviors.
A text editor can represent user actions as commands and keep a history for undo and redo. A background job system can represent a task as a serializable command containing the operation and its validated parameters.
For irreversible operations such as charging a payment or sending an external message, undo is not always a real reversal. The application may need a compensating business operation instead.
Interpreter defines a representation for a small language and an evaluator for its grammar. It can be useful for simple rule languages, search filters, or expressions that users can compose.
It is not generally a reason to build a custom programming language. If the grammar becomes large or security-sensitive, use a well-understood parser and restrict what expressions can do.
Iterator provides a consistent way to traverse a collection without exposing its internal representation. Most mainstream languages provide iterator abstractions directly, so developers often use the language's native iteration protocol rather than implement the classic pattern manually.
Iterators are useful for lazy processing, streaming large datasets, and separating traversal from collection storage. Be clear about whether an iterator can be consumed once, whether it reflects mutations, and how errors or resource cleanup are handled.
Mediator centralizes communication among a group of components so that they do not all need direct references to one another. A user-interface dialog can use a mediator to coordinate fields, buttons, and validation. A workflow coordinator can manage interactions among a bounded set of application components.
The mediator can become a central dependency or a large object with too many responsibilities. Keep its scope limited and avoid turning every interaction into a message routed through a universal event hub.
Memento captures and restores an object's previous state without exposing its internal representation. It can support undo features, checkpoints, and snapshots.
A form editor might save a snapshot before a user makes a complex change. The application can restore that snapshot if the user chooses undo. Consider storage size, sensitive data, version compatibility, and whether a snapshot is safe to retain.
Observer establishes a one-to-many relationship: when a subject changes, registered observers are notified. It is useful for local event notifications, UI updates, and domain events within a bounded application context.
A subscription service might publish a SubscriptionRenewed event that triggers an invoice update, an email notification, and an analytics update. The publisher should not need to know the implementation details of every observer.
In-process Observer and distributed publish-subscribe are related ideas, but they are not identical. Distributed messaging introduces delivery guarantees, retries, duplicate messages, ordering, schema evolution, and operational failure modes that an in-memory observer list does not solve.
State lets an object delegate behavior to an object representing its current state. It is useful when an entity's valid operations change significantly as it moves through a lifecycle.
An order may move from draft to submitted, paid, fulfilled, or cancelled. Instead of scattering state checks throughout the application, the design can centralize allowed transitions and state-specific behavior. For critical workflows, also enforce transition rules in persistent storage or transactional application logic so concurrent requests cannot create invalid states.
Strategy encapsulates interchangeable algorithms behind a common contract. It is useful when the application must choose a policy based on configuration, user choice, or business context.
Examples include shipping-cost calculation, discount rules, routing decisions, file compression, and recommendation methods. Each strategy can be tested independently, while the caller works with the same interface.
A handful of simple functions may be enough in a language that supports first-class functions. The point is to isolate variation, not to create classes for their own sake.
Template Method defines the overall steps of an algorithm while allowing selected steps to vary in subclasses. It is useful when multiple workflows share a stable sequence but differ in specific operations.
A data-import workflow might always validate input, normalize records, persist results, and report completion, while source-specific subclasses implement extraction and normalization. If composition is more natural than inheritance, a pipeline of injected functions may be a better fit.
Visitor represents an operation that can be applied to elements in an object structure while keeping that operation separate from the element classes. It can be useful when the object structure is stable but new operations are added regularly, such as exporting or analyzing an abstract syntax tree.
Visitor can be awkward when the element hierarchy changes frequently, because every visitor may need updates. Consider pattern fit against the expected direction of change.
The GoF catalog focuses mainly on object-oriented design. Application and architectural patterns operate at broader levels: modules, application layers, data access, service boundaries, and workflows. They should be understood as related design tools rather than additional GoF patterns.
Model-View-Controller (MVC) separates application data and domain behavior, presentation, and user input handling. It is common in web frameworks and desktop applications. Model-View-ViewModel (MVVM) and similar approaches organize presentation state differently, especially in applications with rich interactive interfaces.
These patterns help prevent user-interface code from becoming tightly coupled to persistence and business rules. Their names and exact responsibilities vary between frameworks, so follow the framework's actual conventions rather than forcing a textbook diagram onto it.
Repository provides a domain-oriented interface for retrieving and persisting data while hiding storage details. Unit of Work tracks changes made during a business operation and coordinates their persistence, often within a transaction.
These patterns can make application logic easier to test and can keep database-specific details out of domain code. They can also become unnecessary wrappers if they merely duplicate every ORM method. Use them when they provide a meaningful boundary, query abstraction, or transaction scope.
Dependency Injection (DI) supplies a component's dependencies from the outside instead of having the component construct every dependency itself. This makes dependencies visible and allows tests to substitute fakes or mocks.
DI can be implemented manually through constructor parameters, function arguments, or framework containers. A container is not required for every application. Prefer explicit composition at application boundaries and avoid service locators that hide where dependencies come from.
Command Query Responsibility Segregation (CQRS) separates the model used to change state from the model used to read state. It can be helpful when read and write workloads have very different requirements or when complex domain workflows benefit from explicit commands.
Event Sourcing stores state changes as an ordered sequence of events from which current state can be reconstructed. It can support auditability and temporal analysis, but it introduces event versioning, replay, projection, retention, and operational complexity. Neither CQRS nor Event Sourcing should be adopted simply because an application has an API and a database.
Layered architecture organizes responsibilities into levels, often separating presentation, application logic, domain logic, and infrastructure. Hexagonal Architecture (Ports and Adapters) places the domain and application core behind explicit ports, with adapters connecting external technologies. Clean Architecture emphasizes dependency direction: business rules should not depend directly on frameworks, databases, or delivery mechanisms.
These approaches overlap but are not interchangeable labels for one mandatory structure. Their common goal is to protect important business rules from details that change more frequently. They are useful when an application has meaningful domain logic, multiple external interfaces, or a need to test core behavior independently.
For a deeper foundation, see the related guide to Clean Code Principles and the article on Software Architecture and System Design.
Distributed systems introduce failure modes that do not appear in the same way inside a single process. Network calls can time out, messages can be duplicated, services can become unavailable, and data can be temporarily inconsistent. Patterns at this level address communication, resilience, consistency, and scale.
Circuit Breaker prevents an application from repeatedly calling a dependency that is failing. A typical implementation has closed, open, and half-open states. In the closed state, calls proceed normally. After a configured failure threshold, the breaker opens and rejects or short-circuits calls. After a recovery interval, a limited number of trial calls determine whether the dependency has recovered.
A circuit breaker should be paired with timeouts and meaningful fallback behavior. It does not fix the dependency, guarantee recovery, or make every error safe to ignore. Monitor state changes and avoid overly aggressive thresholds that cause unnecessary outages.
Retry can recover from temporary failures, but immediate repeated requests can amplify load on an already struggling service. Exponential backoff increases the delay between attempts, while jitter adds randomness to reduce synchronized retry bursts.
Retry only operations that are safe to repeat or protected by idempotency. Set a maximum attempt count and an overall deadline. A retry policy should respect error types: a temporary network failure may be retryable, while invalid input or an authorization failure usually is not.
Bulkhead isolates resources so that one failing workload cannot consume all capacity. Separate thread pools, queues, connection pools, or concurrency limits can protect critical operations from noisy neighbors.
Timeouts bound how long a caller waits for a dependency. Without deadlines, requests can accumulate and consume memory, threads, or connections while waiting for work that may never finish. Set timeouts across the full request path and ensure downstream timeouts fit within the caller's remaining deadline.
An operation is idempotent when repeating it produces the same intended effect as performing it once. This property is essential when clients retry after uncertain outcomes, such as when a payment request times out after the provider may already have processed it.
An idempotency key lets the server recognize a repeated request and return the original result rather than performing the action again. The key must be scoped, stored safely, associated with a request fingerprint, and retained for an appropriate period. Idempotency does not mean every HTTP method is automatically safe to repeat; the application must define the semantics.
A Saga coordinates a business transaction across multiple services through a sequence of local transactions. If a later step fails, compensating actions attempt to reverse or counteract earlier steps. Sagas can be choreographed through events or orchestrated by a coordinator.
The Transactional Outbox pattern addresses a common dual-write problem: an application updates a database and publishes an event, but one succeeds while the other fails. By writing the business change and an outbox record in the same database transaction, a separate publisher can deliver the event later. Consumers should still be idempotent because messages may be delivered more than once.
For more on message-based integration, see Engineering Software Systems with Robust Message Queues and Explaining Apache Kafka. Microsoft's Azure Architecture Center and its microservices design patterns guide provide useful architecture-level examples and trade-offs.
Patterns are easier to apply well when they are guided by broader design principles. Principles help explain why a pattern is useful and when its trade-offs are justified.
Encapsulation keeps implementation details behind a boundary so that other components depend on a stable contract. Separation of concerns gives distinct responsibilities to components that change for different reasons. High cohesion keeps closely related behavior together, while low coupling limits unnecessary dependencies between components.
The SOLID principles are a widely used set of object-oriented design guidelines:
SOLID is guidance, not a mathematical law. Applying it mechanically can produce excessive interfaces and fragmented classes. The principles should improve clarity and changeability, not become a reason to split every method into another type.
Composition over inheritance is another useful guideline. Inheritance can express a genuine subtype relationship, but deep hierarchies often make behavior difficult to predict. Composition lets a component delegate work to smaller collaborators and combine behavior at runtime. Strategy, Decorator, and Bridge are examples of patterns that make composition practical.
Favor explicit dependencies, small interfaces, clear ownership of mutable state, and predictable error handling. These decisions often matter more than selecting a named pattern. For related API boundaries and interface design, read API Design Fundamentals.
Consider an online store that needs to support more than one payment provider. The checkout workflow must validate an order, calculate the amount, authorize payment, record the result, and communicate the outcome to the customer. The system may later add another provider or change its payment rules.
A tightly coupled implementation might place provider-specific SDK calls inside the checkout function. Conditional branches select a provider, translate request fields, interpret provider-specific errors, and update order state. As more providers are added, the workflow becomes harder to test and each provider change risks affecting unrelated checkout behavior.
A more maintainable design can combine several patterns, each with a distinct purpose:
The application-level workflow can remain focused on business steps. It asks the selected payment strategy to authorize a payment and handles the result according to domain rules. It does not need to know the provider's HTTP endpoint, SDK class names, or raw error payload.
A simplified Python example illustrates the Strategy idea:
from typing import Protocol
class PaymentGateway(Protocol):
def authorize(self, amount: int, currency: str, idempotency_key: str) -> str:
...
class CheckoutService:
def __init__(self, gateway: PaymentGateway):
self.gateway = gateway
def pay(self, amount: int, currency: str, order_id: str) -> str:
if amount <= 0:
raise ValueError("Amount must be positive")
return self.gateway.authorize(
amount=amount,
currency=currency,
idempotency_key=f"order:{order_id}",
)
The Protocol defines the behavior required by the checkout service. A real provider adapter implements that contract and handles provider-specific details. Another adapter can be substituted without rewriting the checkout workflow.
This example is intentionally incomplete: a production payment system also needs authentication, secret management, request validation, provider timeouts, safe retry rules, webhook verification, reconciliation, audit records, and secure handling of payment data. The idempotency key must be enforced by the server and, where supported, passed to the provider. Payment state transitions must be persisted transactionally, and the application must distinguish a declined payment from an unknown outcome.
The lesson is not that every payment workflow must use seven patterns. It is that each pattern should solve a clearly identified responsibility. If there is only one provider and no foreseeable variation, a simple implementation may be sufficient. Add abstractions when business and operational requirements justify them.
Design patterns are not tied to one programming language. Their implementation should reflect the language's strengths and conventions.
In Python, first-class functions, protocols, decorators, generators, context managers, and modules can express many patterns with little boilerplate. A Strategy may be a function or callable object. A context manager can manage resource acquisition and cleanup. A module can provide a simple shared configuration boundary without introducing a custom Singleton class. Python's official documentation is the best place to verify language behavior and standard-library capabilities.
In Java, interfaces, classes, generics, annotations, and dependency-injection frameworks are commonly used to express patterns. The language's type system can make contracts explicit, although developers should avoid creating unnecessary class hierarchies for simple variations.
In C#, interfaces, delegates, events, records, generics, and built-in dependency-injection support provide multiple ways to model patterns. A Strategy can be represented by an interface or delegate; events can support observer-like communication; and middleware pipelines can compose request processing.
In JavaScript and TypeScript, functions, closures, modules, objects, classes, and structural typing provide flexible implementations. TypeScript interfaces can describe contracts without requiring inheritance. Frameworks also provide established composition and dependency patterns, so developers should understand the framework's conventions before adding another abstraction layer.
In Go, interfaces are often satisfied implicitly, and composition is preferred over deep inheritance. Small interfaces and constructor functions can express Adapter, Strategy, and dependency-injection ideas with minimal ceremony. Go's standard library also provides established patterns for context cancellation, resource handling, and concurrent work.
The important question is not whether a language can reproduce a textbook diagram. It is whether the implementation communicates the intended responsibility clearly and fits the language's idioms.
Patterns influence how software is tested and operated, but they do not remove the need for disciplined engineering.
Test a pattern at the level where its behavior matters. Unit tests can verify a Strategy's calculation, an Adapter's translation, or a State object's allowed transitions. Integration tests can verify that a repository works with the actual database or that an adapter communicates correctly with a provider sandbox. Contract tests can verify that independently developed services agree on request and response expectations. End-to-end tests should focus on important user journeys rather than trying to cover every internal branch.
Dependency injection and explicit interfaces make it easier to substitute fakes in unit tests. However, a test that only checks a mock interaction may miss integration errors. Use realistic integration tests for serialization, transaction boundaries, database constraints, authentication, and external service contracts. The Python Testing Guide covers testing practices in more detail.
Security must be part of the design rather than an afterthought. An Adapter should not weaken authentication or trust data simply because it came from a provider. A Proxy that checks access must enforce authorization at the actual resource boundary. Event consumers should validate message schemas and permissions. Secrets should be injected through secure configuration rather than embedded in code or shared global objects. Avoid logging credentials, payment details, tokens, or sensitive personal data.
Operational behavior matters too. A distributed pattern may require metrics, structured logs, tracing, alerting, dead-letter handling, replay procedures, and runbooks. A retry mechanism needs visibility into attempt counts and exhausted retries. An event-driven workflow needs a way to detect messages that are delayed or repeatedly failing. A circuit breaker needs metrics that help operators distinguish a failing dependency from an overly sensitive threshold.
Finally, measure performance before optimizing. Patterns can add indirection, allocation, serialization, network calls, or extra database queries. These costs may be negligible, or they may matter in a high-throughput path. Profiling, load testing, and production telemetry should guide optimization decisions.
One common mistake is treating patterns as mandatory recipes. Developers sometimes introduce factories, interfaces, managers, and abstract base classes before a real variation exists. This creates more code to navigate without making the system easier to change. Start with the problem and choose the smallest design that addresses it.
Another mistake is confusing a pattern's name with a guarantee. Calling a class a Repository does not automatically make it a useful abstraction. Calling a system event-driven does not guarantee reliable event delivery. Calling a service a Singleton does not make its shared state safe under concurrency.
Overusing inheritance is also problematic. A deep hierarchy can make it difficult to understand where behavior originates and which methods are overridden. Prefer composition when components need to combine independent capabilities. Use inheritance when the subtype relationship is genuine and substitutability is preserved.
A further mistake is allowing abstractions to leak. An Adapter that exposes provider-specific exceptions or a Repository that returns database-specific objects may not provide the isolation the design intended. Define the boundary around the needs of the caller and translate details at the edge.
Developers also sometimes ignore failure modes. A retry loop without a deadline can overload a service. An Observer that runs every handler synchronously can make a user request slow or fail because an unrelated notification failed. A distributed Saga without compensation and reconciliation rules can leave business data in an ambiguous state.
Finally, patterns can become outdated as requirements change. A design that once simplified a workflow may later introduce unnecessary indirection. Refactoring includes removing abstractions that no longer earn their cost. Good design is not a museum of patterns; it is a system that remains understandable as it evolves.
Choosing a pattern begins with describing the problem in plain language. Avoid starting with a pattern name and searching for somewhere to use it. Ask what is changing, what must remain stable, which components know too much about one another, and where the current design makes testing or extension difficult.
A practical selection process is:
A useful design review question is: if the requirement changes, which files and components will need to change? A good pattern often narrows the impact of a predictable change. If adding a new provider still requires edits across the entire application, the abstraction may be in the wrong place.
Do not choose a distributed-systems pattern to solve a local object-design problem, or a class-level pattern to solve an organizational boundary problem. Match the scale of the solution to the scale of the pressure.
Developers learning design patterns should combine conceptual study with small implementation exercises. Begin with the three classic categories and learn the intent and trade-offs of a few widely used patterns: Strategy, Factory Method, Adapter, Decorator, Observer, State, and Facade. These patterns appear in many everyday applications and provide a useful foundation.
Next, study the design principles that explain why the patterns work. Learn encapsulation, cohesion, coupling, composition, dependency inversion, and the SOLID principles. Practice identifying responsibilities in an existing codebase before trying to redesign it.
Then implement patterns in a language you already use. Build a small notification service with interchangeable delivery strategies, an Adapter for two different data sources, or a document structure using Composite. Write tests before and after refactoring so you can compare the behavior and the clarity of the design.
After object-oriented patterns, explore application architecture and distributed systems only when your projects need them. Learn Repository, Dependency Injection, CQRS, event-driven messaging, idempotency, circuit breakers, and Sagas as solutions to specific problems—not as a checklist for every application. The guides to Backend Architecture, Microservices Architecture, and API Design Fundamentals provide useful next steps.
Finally, practice explaining why you selected a pattern and why you rejected alternatives. This is an important engineering skill: a strong design decision includes its context, constraints, trade-offs, and evidence.
Software design patterns are reusable descriptions of solutions to recurring software design problems. They explain how responsibilities and relationships can be organized, but they are not complete code templates. Developers adapt them to the language, framework, and requirements of a project.
The 23 classic patterns are grouped into three categories. Creational patterns are Factory Method, Abstract Factory, Builder, Prototype, and Singleton. Structural patterns are Adapter, Bridge, Composite, Decorator, Facade, Flyweight, and Proxy. Behavioral patterns are Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, and Visitor.
Strategy, Factory Method, Adapter, Decorator, Observer, State, and Facade are useful starting points because they address common problems involving variation, integration, responsibilities, and communication. Learn the problem each pattern solves before memorizing its structure.
No. The underlying design ideas can be implemented in many languages, but the code should use the language's natural features. A Strategy may be a class in one language and a function in another. Some patterns are also built into language features or frameworks.
Yes. Their implementations evolve as languages and frameworks change, but recurring design pressures remain: managing dependencies, isolating variation, coordinating behavior, and handling failure. Modern systems also use application and distributed-system patterns beyond the original GoF catalog.
A design pattern usually addresses a focused problem in the organization of objects or components. An architecture pattern describes a broader structure for an application or system, such as layered architecture, hexagonal architecture, event-driven architecture, or microservices. The boundary is not absolute, but the scale and consequences differ.
Not necessarily. Patterns primarily address structure, communication, and changeability. Some may add runtime overhead through extra calls, allocations, or indirection. Measure performance and optimize based on evidence rather than assuming that a pattern improves speed.
Yes. Unnecessary abstractions increase complexity and make code harder to follow. Apply a pattern when it resolves a real design pressure and produces a net improvement in clarity, flexibility, or reliability.
A principle is a general guideline, such as keeping components loosely coupled or depending on abstractions. A pattern is a reusable arrangement of responsibilities that can help apply principles to a recurring problem. Principles guide judgment; patterns provide vocabulary and practical structures.
The following resources provide deeper explanations, catalogs, and practical guidance. They are useful references, not substitutes for evaluating a pattern against the requirements of a particular system.
Software design patterns are most effective when they help developers make a system easier to understand and change. Learn the intent behind each pattern, recognize the problem it addresses, and adapt the solution rather than copying a diagram. The strongest design is not the one with the most abstractions; it is the one that meets real requirements with a clear structure, controlled complexity, and room for sensible evolution.

Full-stack churn prediction platform using React, Django REST Framework, XGBoost, and SHAP. Enables telecom providers to identify at-risk customers, understand churn drivers, and support data-driven retention strategies.

Learn the 23 classic software design patterns, understand creational, structural and behavioral patterns, and apply practical design principles in modern applications.
Read
Learn how to design maintainable, scalable, secure, and reliable software systems. Explore architecture styles, system design trade-offs, data, APIs, resilience, cloud deployment, and a practical design walkthrough.
Read
Understand modern backend architecture, from layered design and domain boundaries to databases, APIs, microservices, scalability, security, resilience, and deployment.
Read