| Pattern | Type | Spring Boot Usage | Typical Example |
|---|---|---|---|
| Singleton | Creational | @Component, @Service, @Repository beans | Spring creates one bean instance per application context by default. |
| Factory Method | Creational | ApplicationContext, BeanFactory, @Bean | Create objects dynamically without exposing creation logic. |
| Builder | Creational | DTOs, response objects, Lombok @Builder | UserResponse.builder().id(1).name("John").build() |
| Prototype | Creational | @Scope("prototype") | Create a new bean instance whenever it is requested from the container. |
| Adapter | Structural | Spring Security, legacy/external integrations | Convert an external API model into an internal domain model. |
| Decorator | Structural | Logging, validation, wrappers, servlet filters | Add behavior without changing the original object. |
| Proxy | Structural | @Transactional, @Cacheable, Spring AOP | Spring creates a proxy to apply transactions, caching, security, etc. |
| Facade | Structural | Service layer, API gateway | Expose a simple interface over multiple backend services. |
| Template Method | Behavioral | JdbcTemplate, RestTemplate, TransactionTemplate | Define a common workflow while allowing specific steps to vary. |
| Strategy | Behavioral | Payment, authentication, notification services | Select an algorithm/implementation dynamically at runtime. |
| Observer | Behavioral | ApplicationEventPublisher, @EventListener | Publish an event and notify subscribers. |
| Command | Behavioral | CQRS, background jobs, task queues | Encapsulate an action as an object. |
| Chain of Responsibility | Behavioral | Servlet filters, Spring Security filter chain | Pass a request through multiple processing steps. |
| Iterator | Behavioral | Java Collections, Streams, JPA paging | Traverse a collection sequentially without exposing its internals. |
| Interpreter | Behavioral | Expression/rule engines, DSL parsing | Interpret expressions or business rules dynamically. |
| Pattern Category | Design Pattern | Description | Spring Boot Implementation |
|---|---|---|---|
| Decomposition | Business Capability | Split services by business domain/capability. | Separate projects: order-service, user-service. |
| Subdomain / Bounded Context | Define clear DDD business boundaries. | Inventory, Catalog, Payment, Shipping contexts. | |
| Integration | API Gateway | Single entry point for clients. | Spring Cloud Gateway. |
| Aggregator | Combine multiple service responses. | Composite REST service / gateway aggregation. | |
| BFF | Separate API optimized for each client. | Web BFF and Mobile BFF. | |
| Database | Database per Service | Independent data ownership. | Spring Data JPA with separate DB/schema. |
| Shared Database | Multiple services share schema; generally creates coupling. | Legacy systems / migration scenarios. | |
| Reliability | Circuit Breaker | Prevent cascading failures. | Resilience4j CircuitBreaker. |
| Retry | Retry transient failures. | Resilience4j Retry / Spring Retry. | |
| Timeout | Limit remote-call waiting time. | WebClient/HTTP client timeouts. | |
| Bulkhead | Isolate resources. | Resilience4j Bulkhead. | |
| Observability | Log Aggregation | Centralized logging. | ELK/OpenSearch-style stacks. |
| Distributed Tracing | Track one request across services. | Micrometer Tracing + OpenTelemetry. | |
| Metrics | Monitor application behavior. | Micrometer + Prometheus + Grafana. | |
| Cross-Cutting | External Configuration | Centralized/environment-specific configuration. | Spring Cloud Config / environment config. |
| Service Discovery | Locate service instances dynamically. | Eureka, Consul, or platform-native discovery. | |
| Centralized Security | Centralized authentication and authorization. | Spring Security + OAuth2/OIDC + gateway/IdP. | |
| Data Consistency | Saga | Distributed business transaction using local transactions and compensation. | Kafka/RabbitMQ + orchestration/choreography. |
| Event Sourcing | Persist state changes as events. | Event store/Kafka/Axon-style implementation. | |
| CQRS | Separate command and query responsibilities. | Separate read/write models or services. | |
| Messaging | Message Broker | Asynchronous communication. | Kafka/RabbitMQ. |
| Publish-Subscribe | Broadcast events to independent consumers. | @KafkaListener and Kafka topics. |
| Principle | Definition | Objective | Spring Boot Example | Related Pattern / Concept |
|---|---|---|---|---|
| SRP | One reason to change. | Single responsibility. | Controller → Service → Repository | Layered Architecture |
| OCP | Open for extension, closed for modification. | Add behavior without repeatedly modifying stable code. | Strategy implementations as Spring beans. | Strategy |
| LSP | Subtypes must be safely substitutable for their abstraction. | Correct inheritance/polymorphism. | Well-designed interfaces and implementations. | Polymorphism |
| ISP | Clients should not depend on methods they do not use. | Small, focused interfaces. | Separate interfaces by capability. | Interface Segregation |
| DIP | High-level modules should depend on abstractions, not concrete implementations. | Loose coupling. | Constructor injection of interfaces. | Dependency Injection |
| Type | Simple Meaning | Who Decides the Next Step? | Best Fit |
|---|---|---|---|
| Choreography | No central coordinator. Services react to events. | Each participating service. | Smaller/simple event-driven workflows. |
| Orchestration | A central Saga Orchestrator controls the workflow. | Orchestrator. | Complex workflows requiring centralized visibility/control. |
| Factor | Description | Spring Boot Implementation / Best Practice |
|---|---|---|
| 1. Codebase | One codebase tracked in version control, with many deploys. | Git repository per microservice or clearly separated application module. |
| 2. Dependencies | Explicitly declare and isolate dependencies. | pom.xml, build.gradle, Maven/Gradle dependency management. |
| 3. Config | Store configuration outside the codebase. | Environment variables, application.yml, Spring configuration mechanisms, external config/secrets. |
| 4. Backing Services | Treat databases, caches, queues and external services as attached resources. | PostgreSQL/MySQL, Redis, Kafka/RabbitMQ and external APIs configured through environment/configuration. |
| 5. Build, Release, Run | Strictly separate build, release and runtime stages. | CI/CD pipeline, Maven/Gradle build, immutable artifact/container image, deployment configuration. |
| 6. Processes | Execute the application as stateless processes. | Stateless REST APIs; persist state in databases, Redis or other external stores. |
| 7. Port Binding | Export services through port binding. | Embedded server such as Tomcat/Netty and configurable server.port. |
| 8. Concurrency | Scale out through the process model. | Multiple containers/pods, Kubernetes replicas and HPA. |
| 9. Disposability | Fast startup and graceful shutdown. | Spring Boot graceful shutdown, readiness/liveness probes, short startup time. |
| 10. Dev/Prod Parity | Keep development, staging and production as similar as practical. | Container images, infrastructure-as-code, profiles/configuration and automated CI/CD. |
| 11. Logs | Treat logs as event streams. | SLF4J/Logback, structured logs, stdout/stderr and centralized log collection. |
| 12. Admin Processes | Run administrative/management tasks as one-off processes. | Database migrations, batch jobs, cleanup tasks and operational scripts. |
Cohesion is how strongly the components of a single module/class are related to each other.
Coupling is about how dependent one module/class is on another.
| Aspect | Cohesion | Coupling |
|---|---|---|
| Definition | Degree to which elements inside a module belong together Measures how closely related the responsibilities of a single class are. | Degree of interdependence between modules Measures how dependent classes are on each other. |
| Goal | High cohesion is desirable | Low coupling is desirable |
| Focus | Within a class/module | Between classes/modules |
| Improves | Maintainability, readability, testability | Flexibility, reusability, modularity |
| Good Example | A class that performs a single, well-defined task | A class that depends on interfaces, not implementations |
| Bad Example | A class that handles DB, UI, and business logic | A class that directly instantiates and uses other classes |
| Panel | Purpose | Interpretation |
|---|---|---|
| Latency | Response Time | Spike indicates performance degradation |
| Throughput | Requests per second | Drop indicates load handling issue |
| Error Rate | Failed Requests | Increase indicates failures |
| CPU | Resource Consumption | High CPU means overload |
| Memory | Heap Usage | Continuous increase may indicate leak |
| Pod Health | Restart Count | Frequent restarts indicate instability |
| Metric | Query | Purpose |
|---|---|---|
| Throughput | sum(increase(http_server_requests_seconds_count[1m])) | Total Requests |
| Latency | rate(http_server_requests_seconds_sum[5m]) | Response Time |
| CPU | process_cpu_usage | Application CPU Usage |
| Memory | jvm_memory_used_bytes | Heap Usage |
| DB Pool Usage | hikaricp_connections_active / hikaricp_connections_max | Connection Pool Saturation |