Definition
- Design pattern – a named, reusable solution to a recurring problem in object-oriented design; a blueprint, not code you copy.
- Shared vocabulary – saying "use a Factory here" gives every engineer instant understanding.
- Gang of Four (GoF) – the book that classified 23 classic patterns into three categories.
- Creational – how objects are created: Factory, Abstract Factory, Builder, Prototype, Singleton.
- Structural – how objects are composed: Adapter, Decorator, Facade, Proxy, Composite, Bridge, Flyweight.
- Behavioral – how objects communicate: Strategy, Observer, Template Method, Command, Iterator, State, Visitor, Mediator, Memento.
Think of the National Bank of India. When Rajesh opens a new account, the branch follows a fixed, proven procedure: that is a creational pattern. When the bank connects its old mainframe to a new mobile app using an adapter box, that wiring is a structural pattern. When a transfer happens and the system automatically tells the audit desk, the SMS desk, and the fraud desk, that notification flow is a behavioral pattern. Nobody reinvents these procedures; they reuse known, trusted recipes.
Understanding the Concepts
What is a design pattern?
A design pattern captures the experience of many senior engineers who solved the same problem before you. It gives a shared vocabulary: when Pankaj says "use a Factory here," every engineer instantly knows what he means.
A pattern is not a library or a code snippet. It is a proven blueprint describing how classes and objects should collaborate to solve a specific kind of problem.
What are the three GoF categories?
The Gang of Four book classifies 23 patterns into three categories based on what problem they address:
- Creational – how objects are created.
- Structural – how objects are composed into larger structures.
- Behavioral – how objects communicate and share responsibilities.
What are the creational patterns?
Creational patterns hide the details of object construction so code does not depend on concrete classes.
package com.bank.service;
import java.math.BigDecimal;
// Factory Method: pick the right settlement handler
public interface SettlementStrategy {
void settle(String tradeId, BigDecimal amount);
}
public class EquitySettlement implements SettlementStrategy {
public void settle(String tradeId, BigDecimal amount) {
System.out.println("Settled equity " + tradeId + ": " + amount);
}
}
public class SettlementFactory {
public static SettlementStrategy create(String type) {
return switch (type) {
case "EQUITY" -> new EquitySettlement();
default -> throw new IllegalArgumentException("Unknown: " + type);
};
}
}
// Builder: step-by-step construction
Trade t = Trade.builder()
.tradeId("TRD-1")
.amount(new BigDecimal("50000"))
.build();
Other creational patterns: Abstract Factory (matching families), Prototype (clone existing), Singleton (one instance).
What are the structural patterns?
Structural patterns compose objects into bigger structures while keeping them flexible.
package com.bank.adapter;
// Adapter: make incompatible interfaces work together
public interface LedgerPort {
void postEntry(String account, java.math.BigDecimal amount);
}
public class LegacyLedger {
public void writeRecord(String acc, double amt) {
System.out.println("Legacy: " + acc + " " + amt);
}
}
public class LegacyLedgerAdapter implements LedgerPort {
private final LegacyLedger legacy;
public LegacyLedgerAdapter(LegacyLedger legacy) {
this.legacy = legacy;
}
public void postEntry(String account, java.math.BigDecimal amount) {
legacy.writeRecord(account, amount.doubleValue());
}
}
// Decorator: add behaviour at runtime by wrapping
// BufferedReader wraps FileReader – layers behaviour
import java.io.*;
BufferedReader br = new BufferedReader(new FileReader("ledger.txt"));
Other structural patterns: Facade (simple front door), Proxy (stand-in, like Spring @Transactional), Composite, Bridge, Flyweight.
What are the behavioral patterns?
Behavioral patterns define how objects interact.
package com.bank.service;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
public class BehavioralExamples {
// Strategy: Comparator is a pluggable algorithm
public void sortByBalance(List<BigDecimal> balances) {
balances.sort(Comparator.naturalOrder());
}
// Observer: Spring ApplicationEvent
// When a settlement completes, audit, SMS, and dashboard
// all observe independently
}
// Template Method: skeleton with overridable steps
public abstract class LoanProcessor {
public final void process(String applicationId) {
validate(applicationId);
calculateEmi(applicationId);
disburse(applicationId);
}
protected abstract void validate(String id);
protected abstract void calculateEmi(String id);
private void disburse(String id) {
System.out.println("Disbursed loan: " + id);
}
}
Other behavioral patterns: Command, Iterator, State, Visitor, Mediator, Memento.
How do all three categories show up in a banking system?
At National Bank of India, Pankaj's settlement system uses all three:
package com.bank.demo;
import java.math.BigDecimal;
public class PatternCategoriesDemo {
public static void main(String[] args) {
// CREATIONAL: Factory picks the right handler
SettlementStrategy handler = SettlementFactory.create("EQUITY");
// STRUCTURAL: Adapter wraps legacy ledger
LedgerPort ledger = new LegacyLedgerAdapter(new LegacyLedger());
// BEHAVIORAL: Strategy (the handler) processes the trade
handler.settle("TRD-001", new BigDecimal("500000"));
ledger.postEntry("ACC-001", new BigDecimal("500000"));
}
}
Settled equity TRD-001: 500000
Legacy: ACC-001 500000.0
How do patterns simplify in modern Java?
Many patterns collapse to a lambda, method reference, or map lookup:
// Strategy as lambda
Comparator<BigDecimal> byAmount = Comparator.naturalOrder();
// Factory as Map<String, Supplier>
Map<String, Supplier<SettlementStrategy>> factory = Map.of(
"EQUITY", EquitySettlement::new
);
// Observer as consumer list
List<Consumer<String>> listeners = List.of(
msg -> System.out.println("Audit: " + msg),
msg -> System.out.println("SMS: " + msg)
);
listeners.forEach(l -> l.accept("Trade settled"));
What are common mistakes with design patterns?
- Over-applying patterns where a plain method would do: adds indirection for no benefit.
- Using a pattern because you know it, not because the problem matches its intent.
- Treating patterns as goals rather than tools.
What should a senior engineer know?
- Patterns are tools, not trophies. The skill is recognising when a problem genuinely matches a pattern's intent.
- In modern Java, many patterns collapse to lambdas and maps.
- The real value of patterns is the shared vocabulary: everyone understands "Factory" or "Observer" without diagrams.
Visual Overview
GoF Pattern Categories
──────────────────────
┌───────────────────────────────────────────────────┐
│ CREATIONAL │
│ How objects are created │
│ Factory │ Abstract Factory │ Builder │ Prototype │
│ Singleton │
└───────────────────────────────────────────────────┘
┌───────────────────────────────────────────────────┐
│ STRUCTURAL │
│ How objects are composed │
│ Adapter │ Decorator │ Facade │ Proxy │ Composite │
│ Bridge │ Flyweight │
└───────────────────────────────────────────────────┘
┌───────────────────────────────────────────────────┐
│ BEHAVIORAL │
│ How objects communicate │
│ Strategy │ Observer │ Template │ Command │ State │
│ Iterator │ Visitor │ Mediator │ Memento │
└───────────────────────────────────────────────────┘
Banking Example:
Trade arrives ──▶ Factory (creational) picks handler
──▶ Adapter (structural) wraps legacy ledger
──▶ Observer (behavioral) notifies audit + SMS
Key Takeaway
A design pattern is a named, reusable solution to a recurring object-oriented design problem. It is not a library or code to copy; it is a proven blueprint that describes how classes and objects should collaborate. The real value is a shared vocabulary: when Pankaj tells Rajesh to use a Factory or an Observer, Rajesh immediately understands the structure.
The Gang of Four grouped 23 patterns into three categories. Creational patterns are about how objects are created, hiding construction details so code does not depend on concrete classes: Factory, Abstract Factory, Builder, Prototype, Singleton. Structural patterns are about how objects are assembled into larger structures while staying flexible: Adapter, Decorator, Facade, Proxy, Composite, Bridge, Flyweight. Behavioral patterns are about how objects communicate and divide responsibilities: Strategy, Observer, Template Method, Command, Iterator, State, Visitor, Mediator, Memento.
At National Bank of India, all three categories show up naturally. When a trade arrives, a Factory creates the correct settlement handler. When the bank integrates a legacy ledger, an Adapter wraps its old interface. When settlement completes, an Observer notifies audit, SMS, and the dashboard independently.
The senior insight is that patterns are tools, not trophies. Over-applying them adds indirection. The skill is recognising when a problem genuinely matches a pattern's intent, and knowing that in modern Java many patterns collapse to a lambda, a method reference, or a map lookup.
Remember one sentence: design patterns are proven recipes grouped into creational, structural, and behavioral, and the skill is knowing when a problem genuinely matches a pattern, not forcing patterns onto every problem.
---