A collection of design pattern implementations in Java for learning and reference.
-
Singleton - Ensures a class has only one instance
DatabaseConnection: Double-checked locking implementationAppConfig: Enum-based singleton (recommended approach)
-
Factory Method - Creates objects without specifying exact class
NotificationFactory: Creates Email, SMS, or Push notifications
-
Abstract Factory - Creates families of related objects
UIFactory: Creates themed UI components (Dark/Light themes)
-
Builder - Constructs complex objects step by step
HttpRequest.Builder: Fluent API for building HTTP requests
-
Observer - Notifies multiple objects about state changes
EventManager: Pub/sub event system with logging and alerting
-
Strategy - Defines a family of interchangeable algorithms
Sorter: Swappable sorting strategies (Bubble Sort, Quick Sort)
-
Template Method - Defines algorithm skeleton in base class
DataParser: CSV and JSON parsing with shared validation logic
-
Chain of Responsibility - Passes requests along a handler chain
RequestHandler: Authentication, authorization, and logging chain
-
Command - Encapsulates requests as objects
CommandHistory: Text editor with undo support
-
Decorator - Adds behavior to objects dynamically
DataSourceDecorator: Encryption and compression wrappers
-
Proxy - Controls access to another object
ImageProxy: Lazy-loading image proxy
-
Adapter - Makes incompatible interfaces work together
-
State - Allows object to change behavior when internal state changes
Order: Order lifecycle transitions (New -> Paid -> Shipped -> Delivered)VlcPlayerAdapter: Adapts VLC player to MediaPlayer interface
src/
├── main/java/com/huweiming/patterns/
│ ├── singleton/
│ ├── factory/
│ ├── abstractfactory/
│ ├── builder/
│ ├── observer/
│ ├── strategy/
│ ├── templatemethod/
│ ├── chain/
│ ├── command/
│ ├── decorator/
│ ├── proxy/
│ └── adapter/
└── test/java/com/huweiming/patterns/
└── (mirrors main structure)
# Compile
mvn compile
# Run tests
mvn test
# Package
mvn package- Java 17+
- Maven 3.8+