The Gang of Four book — Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, Helm, Johnson, Vlissides, 1994) — catalogued 23 patterns across three categories. Thirty years later, these patterns remain the shared vocabulary of object-oriented design. Every pattern in this series has a dedicated article with full runnable Java 17 code, a UML diagram, console output, and real JDK usage examples.
💡 How to use this guide: Each pattern link below goes to a standalone article. If you’re new to patterns, start with Singleton (simplest), then Strategy (most practical), then Observer (most commonly needed). If you’re preparing for a design interview, work through all three categories in order. Source code for every pattern is at ankurm.com/git.app/asmhatre/design-patterns.
Creational Patterns
Creational patterns abstract the object creation process. They make systems independent of how objects are created, composed, and represented. Use them when the exact types and dependencies of the objects your code must work with are unknown ahead of time, or when you want to delegate the responsibility for object creation to specialised classes.
| Pattern | Intent | Classic use case |
|---|---|---|
| Factory Method | Subclasses decide which object to create | Framework plug-in points, notification senders |
| Abstract Factory | Create families of related objects without concrete classes | Cross-platform UI toolkits, theme systems |
| Builder | Construct complex objects step by step | SQL query builders, HTTP request builders, Lombok @Builder |
| Prototype | Clone objects instead of creating from scratch | Deep-copy expensive objects, template instances |
| Singleton | Guarantee one instance per JVM | Configuration objects, connection pools, loggers |
Structural Patterns
Structural patterns concern class and object composition. They describe how to assemble objects into larger structures while keeping the structures flexible and efficient. Most structural patterns introduce an indirection layer — a wrapper, bridge, or proxy — that lets two otherwise incompatible interfaces collaborate.
| Pattern | Intent | Classic use case |
|---|---|---|
| Adapter | Make incompatible interfaces work together | Third-party library wrappers, InputStreamReader |
| Bridge | Decouple abstraction from implementation | JDBC drivers, SLF4J logging facade |
| Composite | Treat individual objects and compositions uniformly | File system trees, Swing component hierarchy |
| Decorator | Add behaviour by wrapping, not subclassing | Java I/O streams, servlet filter chains |
| Facade | Simplify a complex subsystem with one interface | SLF4J, JDBC, javax.mail, SDK entry points |
| Flyweight | Share common state among many fine-grained objects | String pool, Integer cache, character rendering |
| Proxy | Control access to an object with a surrogate | Lazy loading, Spring AOP, JDK dynamic proxy |
Behavioral Patterns
Behavioral patterns deal with algorithms and the assignment of responsibilities between objects. They shift focus from structure to communication — how objects interact and distribute work. Most behavioral patterns use interfaces and composition to make the interaction points explicit and swappable.
| Pattern | Intent | Classic use case |
|---|---|---|
| Chain of Responsibility | Pass a request along a handler chain until handled | Servlet filter chains, logging handlers, middleware |
| Command | Encapsulate a request as an object for undo/queuing | Undo/redo, job queues, macro recording |
| Interpreter | Define a grammar and interpret sentences in that language | SQL engines, regex matchers, SpEL, template engines |
| Iterator | Traverse a collection without exposing its internals | Java for-each, cursor-based pagination |
| Mediator | Route communication through a central coordinator | Chat rooms, MVC controllers, event buses |
| Memento | Snapshot and restore object state without breaking encapsulation | Undo history, save/load game state, transactions |
| Observer | Notify dependents automatically when state changes | Event listeners, MVC, reactive streams |
| State | Change behaviour when internal state changes | Traffic lights, vending machines, order workflows |
| Strategy | Swap algorithms at runtime via composition | Sorting, payment methods, Comparator |
| Template Method | Fix an algorithm skeleton; let subclasses fill steps | HttpServlet, AbstractList, data pipelines |
| Visitor | Add operations to a hierarchy without modifying it | AST processing, shape renderers, export formats |
How Patterns Relate
Several patterns look similar on the surface. These distinctions come up often in interviews and code reviews:
Adapter vs. Facade vs. Proxy — Adapter changes the interface of an existing object. Facade creates a new, simpler interface over a subsystem. Proxy preserves the interface but controls access to the real object.
Strategy vs. Template Method vs. State — Strategy swaps the entire algorithm at runtime (composition). Template Method fixes the skeleton and varies the steps (inheritance). State is structurally identical to Strategy but the concrete state can transition itself; the caller doesn’t swap states manually.
Decorator vs. Proxy vs. Composite — Decorator adds behaviour by wrapping (same interface, enhanced). Proxy controls access (same interface, intercepted). Composite is a tree where leaves and branches share an interface so callers treat them uniformly.
Factory Method vs. Abstract Factory vs. Builder — Factory Method delegates object creation to a subclass (one product). Abstract Factory creates families of related products (multiple related products, no concrete types exposed). Builder assembles a single complex product step by step (fine-grained construction control).
Observer vs. Mediator — Observer is a one-to-many notification from a subject to any number of observers; each observer subscribes directly. Mediator is a many-to-many hub: components communicate through the mediator and don’t reference each other at all.
✅ Source Code: Every pattern in this series has a runnable Java 17 project. Clone the repository and compile with the JDK:git clone https://ankurm.com/git.app/asmhatre/design-patterns.gitcd design-patterns/03-behavioral/strategyjavac *.java -d out && java -cp out strategy.Main
Further Reading
- Design Patterns: Elements of Reusable Object-Oriented Software — Gamma, Helm, Johnson, Vlissides (Addison-Wesley, 1994) — the original GoF book
- Head First Design Patterns — Freeman & Freeman — accessible introduction with visual examples
- refactoring.guru/design-patterns — excellent visual pattern reference with Java code samples