A collection of practical Java design pattern implementations focused on writing clean, maintainable, scalable, and loosely coupled backend applications.
This repository contains code examples and practical use cases for commonly used Creational, Behavioral, and Structural Design Patterns.
| Pattern | Category | Purpose |
|---|---|---|
| ποΈ Builder Pattern | Creational | Build complex objects step-by-step |
| π Chain of Responsibility | Behavioral | Pass requests through a chain of handlers |
| π Mapper Pattern | Structural / Application | Convert objects between different layers/models |
| π― Strategy Pattern | Behavioral | Select an algorithm/behavior dynamically |
System-Design/src/main/java/com/
|
+---builder_pattern/
| Main.java
| PaymentRequest.java
|
+---chain_of_responsibility/
| | ChainConfiguration.java
| | Main.java
| | NotificationRequest.java
| | NotificationService.java
| |
| \---handler/
| AbstractNotificationHandler.java
| EmailHandler.java
| NotificationHandler.java
| PushHandler.java
| SmsHandler.java
|
+---mapper_pattern/
| Main.java
| RequestMapper.java
| TransactionDto.java
|
+---strategy_pattern/
| | Main.java
| | PaymentRequest.java
| | TransactionDto.java
| |
| +---request_builder/
| | NeftRequestBuilder.java
| | RequestBuilder.java
| | UpiRequestBuilder.java
| |
| \---request_factory/
| RequestFactory.java
|
\---system_design
SystemDesignApplication.java
The Builder Pattern is a creational design pattern used to construct complex objects step-by-step.
It is particularly useful when a class contains:
- Many fields
- Optional parameters
- Multiple combinations of parameters
- Complex object creation logic
- Improves code readability
- Avoids large constructors
- Supports optional parameters
- Creates immutable objects
- Makes object creation easier to understand
DTO creation
Configuration objects
Request objects
Complex domain objects
Immutable objects
The Chain of Responsibility Pattern passes a request through a sequence of handlers.
Each handler can:
- Process the request
- Reject the request
- Pass it to the next handler
Chain of Responsibility is useful for:
- Request validation
- Authentication
- Authorization
- Logging
- API filters
- Payment validation
- Transaction processing
- Exception handling
This pattern can also be seen conceptually in:
HTTP Request
β
Security Filter
β
Authentication Filter
β
Authorization
β
Controller
β
Service
The Mapper Pattern is used to convert one object representation into another.
A common backend use case is converting:
Entity β DTO
For example:
Database
β
UserEntity
β
UserMapper
β
UserDTO
β
API Response
For larger applications, a mapping framework such as MapStruct can reduce repetitive mapping code.
- Separates DTOs from entities
- Reduces boilerplate
- Improves maintainability
- Prevents exposing database entities directly
- Makes API contracts cleaner
Entity β DTO
DTO β Entity
Request β Domain Object
Domain Object β Response
External API Model β Internal Model
The Strategy Pattern allows you to define multiple implementations of an algorithm and select the required implementation dynamically.
Spring Boot makes this pattern particularly useful when multiple implementations of the same interface exist.
- Payment methods
- Notification channels
- Authentication mechanisms
- Pricing strategies
- Discount calculations
- File processing
- Validation strategies
- API version-specific behavior
| Pattern | Category | Main Problem Solved |
|---|---|---|
| Builder | Creational | Complex object creation |
| Chain of Responsibility | Behavioral | Sequential request processing |
| Mapper | Structural / Application | Object transformation |
| Strategy | Behavioral | Dynamic behavior selection |
Need to create a complex object?
β
βΌ
Builder Pattern
Need multiple processing steps?
β
βΌ
Chain of Responsibility
Need Entity β DTO conversion?
β
βΌ
Mapper Pattern
Need different implementations
of the same behavior?
β
βΌ
Strategy Pattern
These patterns are particularly useful when developing Java/Spring Boot microservices.
They help with:
- β»οΈ Code reusability
- π§Ή Clean code
- π Loose coupling
- π Scalability
- π§ͺ Testability
- π§ Maintainability
- π Extensibility
- π§© Separation of concerns
The examples in this repository are intended for learning and practical backend development.
The implementations can be extended and adapted for real-world applications such as:
Banking Systems
E-Commerce Platforms
Payment Systems
Notification Systems
Video Streaming Platforms
Enterprise Microservices
Vansh Gala
β If you find this repository useful, consider giving it a star!