Common GoF Design Patterns in Spring
Boot Microservices
This document summarizes common Gang of Four (GoF) design patterns used in Spring
Boot microservices projects. These patterns are categorized into Creational, Structural, and
Behavioral types.
1. Creational Patterns
✔️Singleton
Spring beans are singleton by default. Example: @Service creates a singleton instance.
✔️Factory Method
Spring @Bean methods or factory classes provide controlled object creation.
✔️Builder
Used to construct complex objects, often via Lombok's @Builder.
✔️Prototype
Stateful or thread-scoped beans via @Scope('prototype').
2. Structural Patterns
✔️Adapter
Used to integrate with external systems by mapping incompatible interfaces.
✔️Decorator
Adds behavior to beans dynamically using Spring AOP or wrappers.
✔️Proxy
Spring AOP uses proxies for cross-cutting concerns like transactions.
✔️Facade
Controllers often act as facades to simplify interactions with services.
3. Behavioral Patterns
✔️Observer
Spring Events allow publishing and handling events asynchronously.
✔️Strategy
Multiple implementations of an interface injected at runtime.
✔️Template Method
JdbcTemplate and RestTemplate define steps and allow hooks to override.
✔️Command
Used in decoupling commands from execution logic, e.g., message handling.
✔️Chain of Responsibility
Implemented with filter chains or Spring interceptor chains.
Summary Table
Pattern Spring Boot Usage Example
Singleton Default Spring bean scope
Factory Method @Bean method, factory classes
Builder Lombok @Builder, API client DSLs
Adapter External API integration
Proxy @Transactional, Spring Security, AOP
Decorator Validation, logging wrappers
Strategy Injecting implementations by name
Template Method RestTemplate, JdbcTemplate
Observer Spring Events, @EventListener
Chain of Responsibility Filter chains, interceptors