Skip to main content

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

NoteDescription
SingletonGuarantee one instance per JVM; thread-safe idioms; Enum singleton.
BuilderFluent step-by-step construction of complex objects; Effective Java idiom; Lombok @Builder.
Factory MethodLet subclasses decide which class to instantiate; static factories; Spring @Bean.
Abstract FactoryCreate families of related objects; Spring @Profile as Abstract Factory.
PrototypeClone existing objects; copy constructors vs Cloneable; Spring prototype scope.

Structural Patterns

NoteDescription
DecoratorWrap objects to add behaviour dynamically; Java I/O streams; Spring Security filters.
AdapterBridge incompatible interfaces; object adapter vs class adapter; Spring legacy integration.
FacadeSimplified interface over a complex subsystem; JdbcTemplate; RestTemplate.
CompositePart-whole tree hierarchy; file system; recursive operations.
ProxyControl access to an object; JDK dynamic proxy; CGLIB; Spring AOP.

Behavioral Patterns

NoteDescription
StrategyInterchangeable algorithms; replace if/else chains; Spring DI strategy registry.
ObserverSubject notifies observers on state change; Spring @EventListener; @TransactionalEventListener.
CommandEncapsulate a request as an object; undo/redo; Spring Batch steps.
Template MethodAlgorithm skeleton; override steps in subclasses; JdbcTemplate; Spring Security filters.
Chain of ResponsibilityPass request along a handler chain; Spring Security FilterChainProxy.
StateBehaviour changes with internal state; FSM; Spring Statemachine.

Demos

DemoCovers
Singleton DemoEager, Holder, Enum, thread-safety test, Spring @Component.
Proxy DemoManual logging/caching proxy, JDK dynamic proxy, Spring @Cacheable.
Strategy DemoClass-based, lambda, Spring @Qualifier, Strategy Registry.
Observer DemoManual, Spring events, @Async, @TransactionalEventListener, conditional SpEL.
Builder DemoEffective Java Builder, Lombok @Builder, Director, Spring @ConfigurationProperties.
Factory Method DemoInheritance-based, static factory, lambda factory, Spring @Bean.
Adapter DemoObject adapter, legacy bridge, two-way adapter, Spring @Profile.

Learning Path

  1. Creational Patterns — start with Singleton (thread-safe idioms), Builder (used everywhere with @Builder), and Factory.
  2. Structural Patterns — Decorator (Java I/O streams) and Proxy (Spring AOP) are the most Java-relevant.
  3. Behavioral Patterns — Strategy (replaces if/else chains), Observer (Spring events), and Template Method (Spring's JdbcTemplate) are must-knows.
  • 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.