Learn the three major categories of Gang of Four (GoF) Design Patterns — Creational, Structural, and Behavioral. Understand what problems each category solves with practical Java examples and real-world framework use cases.
In the previous article, we explored why Design Patterns exist, what the Gang of Four (GoF) introduced, and how patterns help solve recurring software engineering problems.
But many developers make the same mistake when learning design patterns:
They try to memorize all 23 patterns individually.
That usually leads to confusion.
A better approach is understanding the three major design pattern categories first.
Every GoF pattern belongs to one of these families:
Design Patterns
│
├── Creational
├── Structural
└── Behavioral
Each category answers a different software design question.
Understanding these categories makes the remaining pattern journey significantly easier.
Why Were Pattern Categories Created?
Software problems generally fall into three broad areas:
- Object Creation Problems
- Object Organization Problems
- Object Interaction Problems
GoF patterns classify solutions accordingly.
Think of them as answering these core architecture questions:
How do we CREATE objects?
→ Creational Patterns
How do we ORGANIZE objects?
→ Structural Patterns
How do objects COMMUNICATE?
→ Behavioral Patterns
Let’s understand each category using practical Java examples.
1. Creational Patterns — Managing Object Creation
Creational patterns focus on:
How objects should be created.
Object creation sounds simple initially.
After all, Java gives us:
new User();
Problem solved?
Not always.
In enterprise applications, object creation can become surprisingly complicated.
The Object Creation Problem
Suppose you’re building a payment gateway system.
Naive implementation:
public class PaymentService {
public void process(String mode) {
PaymentGateway gateway = null;
if(mode.equals("STRIPE"))
gateway = new StripeGateway();
else if(mode.equals("PAYPAL"))
gateway = new PaypalGateway();
gateway.pay();
}
}
Looks acceptable.
Until requirements evolve.
Now business wants:
- Razorpay
- Apple Pay
- Google Pay
- Country-specific providers
Your service gradually turns into:
if
else if
else if
else if
else if
Problems appear:
- Tight coupling
- Difficult testing
- Repeated modification
- Growing conditional logic
This is where Creational Patterns help.
Common Creational Patterns
Singleton Pattern
Ensures only one instance exists.
Example:
Runtime.getRuntime()
Common use cases:
- configuration managers
- cache managers
- logging services
Factory Method Pattern
Delegates object creation.
Example:
PaymentFactory.getGateway("STRIPE");
Instead of spreading new everywhere, creation becomes centralized.
Builder Pattern
Used for constructing complex objects.
Example:
User user =
User.builder()
.name("Rahul")
.email("rahul@test.com")
.age(30)
.build();
Widely used in:
- Lombok
- Spring configuration
- DTO creation
Abstract Factory Pattern
Creates related object families.
Example:
Windows UI Factory
├── Button
├── Checkbox
└── Menu
Mac UI Factory
├── Button
├── Checkbox
└── Menu
Prototype Pattern
Creates new objects by cloning existing ones.
Useful when creation cost is expensive.
Mental Model for Creational Patterns
Think of them as:
Problem:
Creating objects cleanly.
Solution:
Use abstraction around construction.
2. Structural Patterns — Organizing Objects
Structural patterns focus on:
How objects and classes are composed together.
As applications grow, relationships become messy.
Without proper organization:
- dependencies explode
- interfaces become incompatible
- code duplication increases
Structural patterns solve these composition problems.
Structural Problem Example
Suppose your application uses a legacy payment provider.
Legacy API:
class LegacyGateway {
public void makePayment() {
System.out.println("Payment Done");
}
}
Your new application expects:
interface PaymentGateway {
void pay();
}
Interfaces don’t match.
Now what?
Rewrite the legacy system?
Usually impossible.
Adapter Pattern Solution
Adapter converts incompatible interfaces.
public class GatewayAdapter
implements PaymentGateway {
private LegacyGateway gateway =
new LegacyGateway();
public void pay() {
gateway.makePayment();
}
}
Now old and new systems integrate cleanly.
Common Structural Patterns
Adapter Pattern
Interface conversion.
Think:
Power Plug Converter
Decorator Pattern
Adds behavior dynamically.
Java example:
BufferedReader
Wrapping streams with additional functionality.
Facade Pattern
Provides simplified interface over complexity.
Example:
JdbcTemplate
Complex JDBC operations hidden behind simpler APIs.
Proxy Pattern
Controls access to objects.
Common uses:
- lazy loading
- caching
- security
- remote access
Hibernate lazy loading heavily uses Proxy.
Composite Pattern
Represents hierarchical tree structures.
Example:
Folder
├── File
├── File
└── Folder
Mental Model for Structural Patterns
Think of them as:
Problem:
Objects organized poorly.
Solution:
Improve relationships and composition.
3. Behavioral Patterns — Managing Communication and Behavior
Behavioral patterns focus on:
How objects interact and behave.
This category is often the most visible in enterprise systems.
Behavioral Problem Example
Suppose you’re implementing payment processing.
Naive solution:
public class PaymentProcessor {
public void process(String type) {
if(type.equals("CARD"))
processCard();
else if(type.equals("UPI"))
processUPI();
else if(type.equals("WALLET"))
processWallet();
}
}
Again:
Conditionals start multiplying.
Behavior becomes rigid.
Adding new payment methods requires changing existing code.
Strategy Pattern Solution
Strategy enables runtime behavior selection.
Common contract:
public interface PaymentStrategy {
void pay();
}
Implementation 1:
public class CardPayment
implements PaymentStrategy {
public void pay() {
System.out.println("Card Payment");
}
}
Implementation 2:
public class UpiPayment
implements PaymentStrategy {
public void pay() {
System.out.println("UPI Payment");
}
}
Usage:
PaymentStrategy strategy =
new UpiPayment();
strategy.pay();
Benefits:
- runtime flexibility
- reduced conditionals
- cleaner extensions
- easier testing
Common Behavioral Patterns
Strategy Pattern
Dynamic algorithm selection.
Observer Pattern
Publisher–subscriber communication.
Example:
Stock Price Updated
↓
Notify Subscribers
Spring events follow similar ideas.
State Pattern
Behavior changes based on internal state.
Example:
Order Created
Order Paid
Order Delivered
Command Pattern
Encapsulates operations as objects.
Common uses:
- job execution
- queues
- undo operations
Chain of Responsibility
Creates processing pipelines.
Example:
Authentication
↓
Authorization
↓
Validation
↓
Business Processing
Spring Security uses similar concepts.
Mental Model for Behavioral Patterns
Think:
Problem:
Objects behaving poorly together.
Solution:
Improve communication and behavior delegation.
Quick Comparison of Pattern Categories
| Category | Focus | Main Question |
|---|---|---|
| Creational | Object Creation | How do we create objects? |
| Structural | Object Composition | How do we organize objects? |
| Behavioral | Communication & Logic | How do objects interact? |
Real Framework Examples
Patterns are not theoretical concepts.
Modern frameworks use them everywhere.
| Framework | Pattern |
|---|---|
| Spring Container | Singleton / Factory |
| Hibernate | Proxy |
| Java Collections | Iterator |
| SLF4J | Facade |
| Spring Security | Chain of Responsibility |
Most developers already use design patterns daily without realizing it.
Final Thoughts
You do not need to memorize all 23 GoF patterns immediately.
Start by remembering the three categories.
Ask yourself:
Is my problem about creating objects?
→ Creational
Is my problem about organizing classes?
→ Structural
Is my problem about communication or behavior?
→ Behavioral
Once this mental model becomes clear, individual patterns become dramatically easier to understand.