Creational, Structural, and Behavioral design patterns
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
The Gang of Four organised their 23 patterns into three categories based on what problem each solves.
Creational patterns deal with how objects are created. They give you control over instantiation — examples are Singleton, Factory, and Builder. In Spring, singleton is the default bean scope, while other scopes are available; the ApplicationContext also has registry and factory responsibilities.
Structural patterns focus on how you compose classes and objects into larger structures. Think Adapter, Decorator, Facade, and Proxy. Spring uses Proxy heavily — when you put at-Transactional on a method, Spring wraps your class in a proxy that handles the transaction boundaries.
Behavioral patterns are about how objects communicate and divide responsibility. Examples include Strategy, Observer, Template Method, and Command. Spring Events use the Observer pattern — you publish an event and multiple listeners can react to it.
The key difference: creational is about creation, structural is about composition, and behavioral is about interaction. The mistake freshers make is mixing these up, like calling Observer a structural pattern because it connects objects. It's behavioral because it defines runtime communication.
In an interview, when you mention a pattern, always state its category to show you understand the bigger picture, not just isolated pattern names.
Creational, Structural, and Behavioral design patterns
Direct Answer
GoF patterns are in three categories: Creational (how objects are created — Singleton, Factory, Builder), Structural (how objects are assembled — Adapter, Decorator, Facade), and Behavioral (how objects interact — Strategy, Observer, Template Method). Each category solves a different dimension of design.
Code Example
// CREATIONAL: Singleton - one shared instance
@Service // Spring creates single instance by default
public class CartService {
private final CartRepository repository;
public CartService(CartRepository repository) {
this.repository = repository;
}
}
// STRUCTURAL: Decorator - adds behavior without changing original
public class LoggingCartService implements CartOperations {
private final CartOperations delegate;
public void addItem(Long cartId, Item item) {
System.out.println("Adding item: " + item);
delegate.addItem(cartId, item); // wraps original behavior
}
}
// BEHAVIORAL: Strategy - swaps algorithms at runtime
interface DiscountStrategy {
double applyDiscount(double price);
}
record PercentageDiscount(int percent) implements DiscountStrategy {
public double applyDiscount(double price) {
return price * (1 - percent / 100.0);
}
}
Common Mistakes & Pitfalls
- Misclassifying patterns: Freshers often think Observer is structural because it 'connects' objects, but it's behavioral because it defines how objects communicate at runtime.
- Confusing similar patterns: Mixing up Adapter (converts one interface to another) with Decorator (adds responsibilities to an object) because both wrap objects.
- Only memorising names: Listing pattern names without understanding which category or what problem each solves — interviewers will ask 'why would you use Factory instead of Builder' to test real understanding, not just recall.
Check your understanding
Answer these without looking back at the article.
- Which design patterns does Spring Boot use internally?
- What is the difference between Factory Method and Abstract Factory?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.