1. Overview
Software design raises recurring questions: how to create objects flexibly, organize complex classes, and manage communication between components. Design patterns offer proven answers to these recurring challenges.
In this lesson, we’ll explore what design patterns are, why they exist, and how they help us write better software. We’ll look at the cost of solving common problems from scratch and how patterns create a shared vocabulary. We’ll also explore the three categories that organize the best-known patterns.
2. What Are Design Patterns?
In software, “design” refers to the structural decisions we make about how code is organized: which classes exist, how they relate to each other, and how responsibilities are distributed among them. These decisions connect the syntax of a language and the high-level architecture of a system. Design patterns operate at exactly this level.
2.1. Defining Patterns
Experienced developers rarely start from a blank slate when they face a familiar problem. Over the years, the software community has identified recurring solutions to common design problems. Developers have documented, refined, and named these solutions. That’s essentially what a design pattern is, a reusable template for solving a particular category of problem in software design:
It helps to be clear about what patterns are not:
- A pattern isn’t a library we can import or a framework we can plug into a project.
- It isn’t a finished piece of code we copy and paste.
- It isn’t a rule to follow blindly. Patterns should only be applied when they are useful or necessary.
Instead, a pattern describes a general approach, a blueprint for structuring classes and objects in a way that has been shown to work well across many different contexts. The actual implementation in any language is an adaptation of the pattern, varying depending on the project and its specific requirements.
3. The Problem Patterns Solve: Reinventing the Wheel
3.1. The Cost of Ad-Hoc Solutions
When a developer encounters a design problem for the first time, the natural response is to build something that addresses the immediate issue. The result may solve today’s problem but lack the flexibility to accommodate change. When requirements evolve, that initial design may resist modification. The developer then has to revise and evaluate the structure repeatedly to make it flexible and maintainable. This iteration cost is the primary price of ad-hoc solutions:
Beyond the structural cost, ad-hoc solutions also create secondary problems at the team level. When each developer invents their own approach to the same category of problem, the codebase fills with inconsistent designs. New team members struggle to learn the project because there’s no predictable structure. Code reviews take longer because reviewers need to evaluate each custom solution on its own merits rather than recognizing a familiar approach.
3.2. How Patterns Help
Patterns address this directly. They’re solution blueprints drawn from recurring designs in real systems. When a team adopts patterns, they get a thought-through starting point rather than a blank page.
Pattern descriptions explain their applicability, consequences, and trade-offs, so we can assess whether they fit our problem.
Design patterns appear in established frameworks. For example, Spring uses factories to manage object creation, singleton scope to share a bean instance within a container, and template methods to provide workflows with customizable steps.
Spring’s singleton scope differs from the GoF Singleton pattern, which restricts instances of a class.
Learning patterns gives us names and structure for techniques that are already part of the ecosystem.
When they fit the problem, patterns can make code easier to understand, maintain, and extend. One of the main reasons is that patterns communicate intent. Recognizing a familiar pattern helps a developer understand both what the code does and why it’s structured that way.
The contrast between ad-hoc and pattern-based approaches is most visible over time. An ad-hoc solution can accumulate complexity as requirements change. A suitable pattern provides a starting structure designed to accommodate particular kinds of change:
Patterns influence both how we write code and how we handle change.
4. Shared Vocabulary: Communicating with Patterns
Beyond the structural benefits, patterns improve communication. Consider the difference between these two statements in a code review:
“We should define an interface for a family of algorithms and implement each one in its own class. The client should be able to switch between them at runtime.”
Compare that with:
“Let’s use the Strategy pattern here.”
The second statement summarizes the first more briefly, provided the team shares an understanding of Strategy. We still need to discuss the details of its application. Therefore, pattern names act as a compressed language for design concepts. A single term suggests expectations about class structure, object roles, and interactions.
The vocabulary benefit extends beyond a single team. We can use pattern names in several kinds of technical communication:
- Discussions about alternative designs during interviews
- Documentation and contributor guides that explain a project design
- Blog posts and conference talks that describe design choices
Knowing this vocabulary is a professional asset that pays off in conversations at every level, from pair programming to system design discussions.
Now that we understand the two core benefits of patterns (preventing reinvention and enabling communication), we need a way to organize and classify them. That framework already exists, and it comes from the Gang of Four catalog.
5. The Gang of Four and Pattern Categories
In 1994, four authors published a book that cataloged recurring object-oriented designs. Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, collectively known as the “Gang of Four,” wrote Design Patterns: Elements of Reusable Object-Oriented Software.
The book cataloged 23 patterns, falling into three categories based on what aspect of software design each pattern addresses.
These three categories map to three fundamental concerns in any object-oriented system:
- How objects are created
- How they’re composed into larger structures
- How they communicate with each other
Let’s look at each one.
5.1. Creational Patterns
Creational patterns deal with how objects are created. The core idea is to abstract the instantiation process so that the system doesn’t depend on exactly how its objects are created, composed, or represented.
Imagine a service that needs a database connection. If every component creates its own connection, each repeats the creation steps: choosing the implementation, configuring parameters, and managing initialization. When the database changes, that logic must be updated in every affected component.
The Factory Method pattern separates the creation decision from the code using the object. Subclasses implement the creation method to choose the connection implementation, while callers use a common connection interface. Changing that creation logic can leave callers unchanged when the interface and behavior they rely on remain compatible.
This separation of creation from use is the principle behind all creational patterns. In a growing application, the way we create objects tends to become tangled with business logic. Creational patterns separate these two concerns. This can reduce the effect of creation changes on callers.
This course covers several creational patterns, including Singleton, Factory Method, and Builder.
5.2. Structural Patterns
Structural patterns address how classes and objects are combined to form larger structures. These patterns help us design relationships between entities so that when one part of the system changes, the rest can stay the same.
Take integrating a third-party logging library whose interface doesn’t match what our application expects. The Adapter pattern gives us a principled way to bridge this incompatibility without changing either side. Without it, we’d either modify the library (usually not possible), rewrite parts of our application to match it, or create tangled workarounds.
These patterns are particularly valuable when integrating existing components or adapting interfaces that weren’t originally designed to work together. We’ll explore Adapter, Decorator, Facade, and Proxy in detail throughout this course.
5.3. Behavioral Patterns
Behavioral patterns focus on how objects communicate and distribute responsibility. They define the protocols for interaction between objects and help us manage complex flows of control.
Think of a notification system where an email sender, an SMS handler, and a push notification service must react whenever a new user registers. If the registration class calls all three components directly, adding another notification channel requires changing that class. The Observer pattern reduces this coupling. The registration class notifies subscribers through a common interface rather than depending on each concrete notification handler.
This example shows that when a system grows, the web of interactions between objects can grow too complex to manage efficiently. Behavioral patterns provide tested approaches for organizing these interactions.
The course covers Strategy, Observer, Template Method, and Command.
5.4. Categorization Criteria
Each pattern category addresses a different design challenge. Recognizing which category a problem falls into is the first step toward identifying the right pattern. Object-creation problems suggest creational patterns. Composition problems suggest structural patterns, while interaction problems suggest behavioral patterns:
While each category has a primary focus, issues involving structure, behavior, and creation are often related. A behavioral pattern may influence how objects are composed or instantiated, but its primary intent is to define communication and responsibility. Classification follows the primary effects, focus, and intent, not side effects.
6. Conclusion
Design patterns are proven solutions to the recurring problems that every developer is likely to encounter. Patterns prevent us from reinventing the wheel by providing tested blueprints, and they give teams a shared vocabulary that makes communication more efficient.
The Gang of Four organized the most widely known patterns into three categories. Creational patterns handle object creation, structural patterns manage composition, and behavioral patterns govern communication between objects. These three categories define a roadmap for the rest of this course.