A design pattern in object-oriented programming is a named, reusable solution to a recurring problem in software design—a template for how classes and objects should collaborate that can be adapted to many codebases rather than copied as finished code. The pattern describes the roles, responsibilities, and relationships among participating objects, along with the trade-offs of applying it; it is not a library, a finished implementation, or a language feature.
The concept was popularized by the 1994 book Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides—often called the "Gang of Four" (GoF). That book catalogued 23 patterns and gave the field a shared vocabulary. A pattern name lets developers compress a design discussion: saying "we used an Observer here" communicates a whole structure of relationships in two words. The essential value of patterns is this combination of reusable structure and precise shared terminology.
What a Pattern Is—and Is Not
A design pattern sits between a general principle and a concrete implementation. It is more specific than advice like "favor composition over inheritance," which is a design principle with no fixed structure. It is more general than a particular class or function you could paste into a file. The pattern specifies what roles objects play and how they interact, while leaving the when and how much to the developer.
Several things are commonly confused with patterns:
An algorithm is a step-by-step procedure for a computation, such as quicksort. A pattern is a structural arrangement of collaborating objects, not a sequence of operations. A framework is working code you extend; a pattern is a design idea you implement yourself. A framework may use many patterns internally. A data structure is a concrete way of organizing data, like a hash table. Patterns like Iterator and Composite concern how objects expose or compose structures, not the structures themselves. An idiom is a language-specific convention, such as RAII in C++ or Python's context managers. Idioms depend on a language's syntax and features; patterns are meant to be broadly language-independent.
A subtle point: the same pattern can look very different across languages. The Strategy pattern, for instance, is often implemented with separate classes and interfaces in Java, but in a language with first-class functions it may collapse into passing a function as an argument—so the "pattern" becomes almost invisible. This does not mean the pattern is absent; the underlying design decision (varying an algorithm independently of its context) is still being made. It means pattern implementations are shaped by the language's expressive power.
Why Patterns Matter
Patterns matter for three practical reasons. First, they give teams a shared vocabulary that shortens design conversations and documentation. Second, they encode accumulated design experience: each pattern emerged because many developers independently rediscovered a solution to the same problem, and the catalog records the trade-offs. Third, they aid communication across codebases: a new developer who recognizes a Factory or an Adapter can orient quickly.
Patterns also carry a warning label. Misapplied, they produce over-engineering—elaborate class hierarchies and indirection where a simple function would do. A pattern is justified when it addresses a real, present or clearly anticipated variation; applying it "just in case" often adds complexity without benefit. The GoF book itself discusses consequences for each pattern, including the cost of added indirection.
The Three Classic Categories
The GoF book groups its 23 patterns by purpose. This taxonomy is a teaching convenience, not a rigid law—some patterns blur boundaries—but it remains a useful map.
Creational Patterns
Creational patterns concern how objects are instantiated, decoupling client code from the concrete classes it creates.
Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes. A UI toolkit might expose a factory that produces matched sets of buttons and checkboxes for a given platform. Builder separates the construction of a complex object from its representation, allowing the same construction process to produce different results step by step. Factory Method defines an interface for creating an object but lets subclasses decide which class to instantiate. Prototype creates new objects by cloning an existing instance, useful when construction is expensive or when the set of objects is determined at runtime. Singleton ensures a class has only one instance and provides a global point of access. It is the most debated pattern: global state creates hidden dependencies and complicates testing, so many practitioners restrict it to genuinely singular resources and prefer dependency injection otherwise.
Structural Patterns
Structural patterns concern how classes and objects are composed into larger structures.
Adapter converts one interface into another that clients expect, letting otherwise incompatible classes work together—like a power plug adapter. Bridge decouples an abstraction from its implementation so the two can vary independently, for example a shape abstraction paired with separate rendering implementations. Composite composes objects into tree structures to represent part-whole hierarchies, letting clients treat individual objects and groups uniformly—file systems and UI widget trees are common examples. Decorator attaches additional responsibilities to an object dynamically by wrapping it, offering a flexible alternative to subclassing for extending behavior. Facade provides a simplified interface to a complex subsystem. Flyweight shares fine-grained objects to support large numbers efficiently, separating intrinsic shared state from extrinsic context. Proxy provides a surrogate or placeholder for another object to control access—used for lazy loading, access control, or remote communication.
Behavioral Patterns
Behavioral patterns concern how objects communicate and distribute responsibility.
Chain of Responsibility passes a request along a chain of handlers until one handles it. Command encapsulates a request as an object, enabling parameterization, queuing, logging, and undo. Interpreter defines a representation for a language's grammar and an interpreter for it—most practical for small domain-specific languages. Iterator provides sequential access to elements of an aggregate without exposing its underlying representation. Many modern languages build this into the language (for example, for...of), but the pattern describes the underlying design. Mediator centralizes complex communication between objects, reducing direct dependencies among them. Memento captures and externalizes an object's state so it can be restored later, supporting undo without violating encapsulation. Observer defines a one-to-many dependency so that when one object changes state, all dependents are notified—the basis of event systems and reactive architectures. State lets an object alter its behavior when its internal state changes, appearing to change class. Strategy defines a family of algorithms, encapsulates each, and makes them interchangeable, letting the algorithm vary independently of clients. Template Method defines the skeleton of an algorithm in a base class, deferring some steps to subclasses. Visitor represents an operation to be performed on elements of an object structure, letting you add operations without modifying the element classes—at the cost of making it harder to add new element types.
A Concrete Illustration: Strategy vs. Inheritance
Suppose an application computes shipping costs with several algorithms: flat rate, weight-based, and distance-based. A naive approach subclasses a Shipment class for each algorithm. This works initially but hard-wires the algorithm into the class hierarchy: adding a new algorithm means adding a subclass, and combining an algorithm with other varying features multiplies subclasses.
The Strategy pattern instead extracts each algorithm into its own object implementing a common interface, and the Shipment class holds a reference to one. Clients can swap algorithms at runtime, and new algorithms can be added without touching Shipment. This shows the pattern's essence: encapsulate what varies. The related principle—"program to an interface, not an implementation"—is not itself a pattern but the design idea the pattern serves.
A counterexample clarifies the boundary: if there is exactly one algorithm and no credible expectation of another, applying Strategy adds an interface and an extra object for no benefit. The pattern's value is probabilistic and contextual, not absolute.
Patterns, Principles, and Architectures
Patterns are distinct from design principles and architectural patterns, though they are often discussed together.
| Concept | Scope | Example | Nature |
|---|---|---|---|
| Design principle | Guiding rule | Single Responsibility Principle | Advice, no fixed structure |
| Design pattern | Class/object collaboration | Observer | Named structure, adaptable |
| Architectural pattern | System-level organization | Model-View-Controller | Broad structural organization |
The five SOLID principles—Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion—are design principles, not patterns. They tell you what good design tends toward; patterns offer concrete structural answers. Confusion arises because many patterns embody principles: Strategy implements the Open/Closed principle for algorithms, and Dependency Inversion is often realized through interfaces that patterns like Factory and Adapter employ.
Architectural patterns operate at a higher level, describing how major subsystems interact. MVC, layered architecture, and microservices are architectural patterns. Some patterns, like Observer, appear inside both design and architectural contexts—an MVC framework typically uses Observer to link model and view—but that overlap does not make them the same kind of thing.
Common Misconceptions
"Patterns are rules you must follow." They are catalogued solutions, not mandates. The GoF book presents each with consequences and trade-offs; using one when its problem is absent is a misuse.
"More patterns equal better design." Pattern density is not a quality metric. Mature code often uses few patterns because the language and framework absorb them. Recognizing when not to introduce a pattern is part of design skill.
"Patterns are obsolete now that languages have features." Some patterns are partly subsumed by language features—functions replace many Strategy classes, iterators are built into loops, decorators exist as language syntax in Python and TypeScript. The design intent persists even when the ceremony disappears. Other patterns, like Observer, remain structurally meaningful in event-driven systems.
"Each pattern maps to exactly one real-world analogy." Analogies—the plug adapter for Adapter—aid memory but mislead if taken literally. The Adapter pattern also concerns object interfaces, not physical compatibility, and carries subtleties about whether it adapts by inheritance or composition.
Where Patterns Stop Applying
Patterns operate within object-oriented design and assume objects with interfaces and polymorphism. In purely functional code, the idiom changes: many GoF patterns concern distributing responsibility among objects, while functional programming reaches for composition, higher-order functions, and immutable data. The underlying problems—varying algorithms, notifying dependents, building complex objects—still exist, but the solutions frequently stop being recognizable as GoF patterns.
Patterns also do not guarantee good design. A design can use every named pattern and still be brittle, hard to test, or over-coupled. Conversely, excellent designs can use none of the classic names. Patterns are a vocabulary and a library of experience, not a proof of quality.
In recent decades, the community has added other pattern catalogs—enterprise application patterns, concurrency patterns, and reactive patterns—showing that the idea of cataloguing recurring solutions extends beyond the original 23. When someone refers to "the design patterns," they usually mean the GoF set, but the broader concept is not limited to that list. It is worth confirming which catalog is meant in a given discussion, since the answer changes with the scope: the GoF patterns define the classic object-oriented set, while "design pattern" in the general sense is an open-ended category.
In summary, a design pattern in object-oriented programming is a named, reusable design template that captures how objects collaborate to solve a recurring problem, distinguished from algorithms, frameworks, data structures, principles, and architectural patterns by its scope and purpose. Its value lies in shared vocabulary, accumulated experience, and adaptable structure—provided it is applied when the problem actually exists, with attention to language features and to the cost of added indirection.