Design Patterns
Design patterns are reusable solutions to recurring object-oriented design problems. The Gang of Four (GoF) patterns — creational, structural, and behavioral — appear everywhere in Java frameworks and are a staple of senior-level interviews. Spring itself is an application of Factory, Proxy, Template Method, and Observer patterns.
What You'll Find Here
Creational Patterns
| Note | Description |
|---|---|
| Singleton | Guarantee one instance per JVM; thread-safe idioms; Enum singleton. |
| Builder | Fluent step-by-step construction of complex objects; Effective Java idiom; Lombok @Builder. |
| Factory Method | Let subclasses decide which class to instantiate; static factories; Spring @Bean. |
| Abstract Factory | Create families of related objects; Spring @Profile as Abstract Factory. |
| Prototype | Clone existing objects; copy constructors vs Cloneable; Spring prototype scope. |
Structural Patterns
| Note | Description |
|---|---|
| Decorator | Wrap objects to add behaviour dynamically; Java I/O streams; Spring Security filters. |
| Adapter | Bridge incompatible interfaces; object adapter vs class adapter; Spring legacy integration. |
| Facade | Simplified interface over a complex subsystem; JdbcTemplate; RestTemplate. |
| Composite | Part-whole tree hierarchy; file system; recursive operations. |
| Proxy | Control access to an object; JDK dynamic proxy; CGLIB; Spring AOP. |
Behavioral Patterns
| Note | Description |
|---|---|
| Strategy | Interchangeable algorithms; replace if/else chains; Spring DI strategy registry. |
| Observer | Subject notifies observers on state change; Spring @EventListener; @TransactionalEventListener. |
| Command | Encapsulate a request as an object; undo/redo; Spring Batch steps. |
| Template Method | Algorithm skeleton; override steps in subclasses; JdbcTemplate; Spring Security filters. |
| Chain of Responsibility | Pass request along a handler chain; Spring Security FilterChainProxy. |
| State | Behaviour changes with internal state; FSM; Spring Statemachine. |
Demos
| Demo | Covers |
|---|---|
| Singleton Demo | Eager, Holder, Enum, thread-safety test, Spring @Component. |
| Proxy Demo | Manual logging/caching proxy, JDK dynamic proxy, Spring @Cacheable. |
| Strategy Demo | Class-based, lambda, Spring @Qualifier, Strategy Registry. |
| Observer Demo | Manual, Spring events, @Async, @TransactionalEventListener, conditional SpEL. |
| Builder Demo | Effective Java Builder, Lombok @Builder, Director, Spring @ConfigurationProperties. |
| Factory Method Demo | Inheritance-based, static factory, lambda factory, Spring @Bean. |
| Adapter Demo | Object adapter, legacy bridge, two-way adapter, Spring @Profile. |
Learning Path
- Creational Patterns — start with Singleton (thread-safe idioms), Builder (used everywhere with
@Builder), and Factory. - Structural Patterns — Decorator (Java I/O streams) and Proxy (Spring AOP) are the most Java-relevant.
- Behavioral Patterns — Strategy (replaces
if/elsechains), Observer (Spring events), and Template Method (Spring'sJdbcTemplate) are must-knows.
Related Domains
- Object-Oriented Programming — design patterns are OOP in action; solid OOP knowledge is a prerequisite.
- Spring Framework — Spring's internals implement Factory, Proxy, Template Method, and Observer.
- System Design — design patterns at the class level support the architectural patterns at the system level.