What Is a Design Pattern? (With an Example)
A design pattern is a proven way to structure code so it’s easier to reuse, understand, and maintain. In object-oriented software, design patterns typically describe roles (e.g., “subject,” “observer”), interactions between objects, and trade-offs—rather than prescribing a single implementation. The canonical “Gang of Four” (GoF) book organizes patterns into creational, structural, and behavioral categories, emphasizing how patterns help manage recurring design challenges such as object creation, composition, and communication between components.
At a high level, design patterns are useful because they:
- provide shared vocabulary for common design issues,
- capture experience (what works and why),
- promote maintainable structure over ad-hoc solutions,
- allow teams to reason about code architecture at the right level of abstraction.
Key terms in this section:
- Gang of Four
- creational pattern
- structural pattern
- behavioral pattern
Footnotes
-
Design pattern - Wikipedia - Overview of design patterns, GoF categories, and typical intent/structure. ↩ ↩2
Design Patterns (Overview)
Example: The Observer Pattern
A classic Observer design pattern example is an event-driven system where a “subject” changes state and “observers” automatically receive updates. For example, consider:
- A
WeatherStation(subject) that tracks temperature. - Multiple
Displaycomponents (observers) like “Current Temperature Display” and “Statistics Display.” When the weather station updates its temperature, it notifies all registered displays, without the station needing to know how each display renders. This separation improves modularity and supports adding/removing displays at runtime.
This aligns with the GoF’s framing of patterns as reusable templates for organizing responsibilities and interactions to solve a recurring problem: keeping dependent components loosely coupled while still synchronizing behavior.
Footnotes
-
Design pattern - Wikipedia - Overview of design patterns, GoF categories, and typical intent/structure. ↩
Observer Pattern: How it Works
- 1Step 1
Create a subject that stores state (e.g., temperature) and maintains a list of observers to notify.
- 2Step 2
Specify an update method (e.g.,
onTemperatureChanged(newValue)) that all observers implement. - 3Step 3
Allow observers to subscribe to the subject (e.g.,
addObserver(...)). - 4Step 4
When the subject’s internal state changes, it triggers a notification event.
- 5Step 5
Iterate over observers and call their update method with the new state.
- 6Step 6
The subject publishes changes; observers react—without the subject knowing observer-specific behavior.
What the Observer Pattern “buys” you (Use / Benefits)
- Loose coupling: The subject does not depend on concrete observer classes; it depends on the observer interface.
- Extensibility: You can add new observers without modifying the subject’s core logic (you only implement the observer interface).
- Centralized change propagation: All observers stay consistent because updates are pushed automatically.
In design-pattern terms, the pattern clarifies roles and responsibilities: subjects manage state changes and notifications; observers manage reaction logic. This is exactly what a design pattern is meant to capture: a reusable structure that addresses a recurring design issue.
Footnotes
-
Design pattern - Wikipedia - Overview of design patterns, GoF categories, and typical intent/structure. ↩
Rule of Thumb for Choosing Patterns
"If you repeatedly face the same kind of coupling/communication problem (e.g., “many things react to one change”), start by searching for a pattern that matches that concern (often behavioral patterns like Observer)."
Design Patterns Are Not Copy-Paste Recipes
"Patterns describe intent and structure; forcing a pattern when the underlying problem doesn’t match can add complexity. Use patterns when they reflect your real constraints (runtime variability, coupling, creation needs, etc.)."
Common Workflow for Learning and Applying Design Patterns
Recognize the recurring problem
Step 1Identify the repeated design issue (e.g., “multiple components must react to changes”)."
Match to a pattern category
Step 2Use creational/structural/behavioral classification to narrow down candidates."
Map roles to your code
Step 3Assign Subject/Observer (or other roles) to your domain objects."
Implement with trade-offs in mind
Step 4Consider performance, coupling, and maintainability impacts of the chosen structure."
Refactor when needed
Step 5If the pattern increases complexity, reassess whether the problem truly matches the pattern."
Design Patterns vs Other Concepts (Quick Comparison)
Patterns focus on reusable design structure, not a single algorithm or full architecture.
Common Confusions
Design Patterns Quick Self-Test
Knowledge Check
What best describes a design pattern?