Structural vs behavioral patterns
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
Structural patterns and behavioral patterns solve different kinds of problems.
Structural patterns are about composition — how you organise and assemble classes and objects into larger structures. They focus on relationships.
For example, the Decorator pattern wraps an object to add responsibilities without changing its code. You might wrap a CartService with a LoggingCartService that logs every method call. The Proxy pattern controls access to an object — Spring uses this for at-Transactional, wrapping your service class in a proxy that manages database transactions.
Adapter converts one interface to another so incompatible classes can work together. These are all about structure and composition. Behavioral patterns are about communication and responsibility.
They define how objects interact and who does what. The Observer pattern creates a one-to-many relationship where one object notifies multiple dependents. Spring Events use this — when you publish an OrderPlacedEvent, multiple at-EventListener methods can react independently.
Strategy pattern encapsulates algorithms to make them interchangeable at runtime. You might have a DiscountStrategy interface with different implementations for percentage discounts, fixed discounts, or seasonal promotions. Command pattern encapsulates requests as objects, useful for undo operations or job queues.
The key difference: structural is about how things are built and connected, behavioral is about how they talk and cooperate. Both structural and behavioral patterns can involve compile-time types and runtime objects. The distinction is their intent: composition versus communication and responsibility.
The mistake freshers make is calling Observer a structural pattern because it 'connects' objects. It's behavioral because it defines a communication protocol, not a composition structure. In Spring Boot, Proxy is structural because it's about wrapping and composition, while Events are behavioral because they're about runtime communication between decoupled components.
Structural vs behavioral patterns
Direct Answer
Structural patterns organise how classes and objects are composed (e.g., Decorator, Adapter, Facade); behavioral patterns define how objects communicate and assign responsibilities (e.g., Observer, Strategy, Command).
Code Example
// STRUCTURAL (Decorator): compose objects to add behavior by wrapping
interface CartOps { void addItem(String item); }
class RealCart implements CartOps { public void addItem(String i){ /* save */ } }
class LoggingCart implements CartOps { // wraps another CartOps
private final CartOps inner;
LoggingCart(CartOps c){ this.inner = c; }
public void addItem(String i){ System.out.println("log " + i); inner.addItem(i); }
}
// BEHAVIORAL (Strategy): swap interchangeable algorithms at runtime
interface Discount { double apply(double price); }
class PercentOff implements Discount { public double apply(double p){ return p * 0.9; } }
// Structural = how objects are assembled; Behavioral = how they decide/communicate.
Common Mistakes & Pitfalls
- Calling Observer structural: Freshers think Observer is structural because it 'connects' objects, but it's behavioral because it defines runtime communication protocols, not compile-time composition.
- Confusing Decorator and Strategy: Both seem to 'wrap' behavior, but Decorator adds responsibilities to an object (structural composition), while Strategy swaps algorithms (behavioral interaction).
- Not giving Spring examples: Discussing patterns abstractly without mentioning that Spring uses Proxy (structural) for at-Transactional and Observer (behavioral) for ApplicationEvent listeners — interviewers want to see you connect theory to practice.
Check your understanding
Answer these without looking back at the article.
- Where does the Singleton pattern fit — is it structural, behavioral, or creational?
- Can a design pattern span multiple categories?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.