A comprehensive multi-module Maven project for learning design patterns through hands-on exercises based on realistic software engineering scenarios.
This project helps developers master the 22 classic design patterns from the Gang of Four catalog through:
- Isolation exercises: Focused, minimal examples that teach the core intent of each pattern
- Real-world simulations: Production-like coding challenges that mirror senior-level engineering tasks
All patterns and terminology follow Refactoring.Guru as the source of truth.
design-patterns-training/
βββ pom.xml (parent)
βββ README.md (this file)
β
βββ Creational Patterns (5 modules)
β βββ factory-method-pattern/
β βββ abstract-factory-pattern/
β βββ builder-pattern/
β βββ prototype-pattern/
β βββ singleton-pattern/
β
βββ Structural Patterns (7 modules)
β βββ adapter-pattern/
β βββ bridge-pattern/
β βββ composite-pattern/
β βββ decorator-pattern/
β βββ facade-pattern/
β βββ flyweight-pattern/
β βββ proxy-pattern/
β
βββ Behavioral Patterns (10 modules)
βββ chain-of-responsibility-pattern/
βββ command-pattern/
βββ iterator-pattern/
βββ mediator-pattern/
βββ memento-pattern/
βββ observer-pattern/
βββ state-pattern/
βββ strategy-pattern/
βββ template-method-pattern/
βββ visitor-pattern/
- Java 21 or later
- Maven 3.8+ (or use the included Maven wrapper)
./mvnw clean installcd strategy-pattern
../mvnw testcd strategy-pattern
../mvnw test -Dtest=IsolationExerciseTest
../mvnw test -Dtest=SimulationExerciseTestEach pattern module contains exactly two exercises:
Goal: Understand and practice the pattern in a focused, minimal context.
Characteristics:
- Small and self-contained
- Centered on the core intent of the pattern
- More guided with clear TODOs
- Perfect for learning what the pattern does
Example domains: Simple calculators, basic payment selection, trivial formatters
Goal: Apply the pattern to a realistic business/domain problem.
Characteristics:
- Simulates senior-level coding tasks
- Feels like production code with real constraints
- Includes ambiguity, tradeoffs, and extensibility concerns
- May combine multiple patterns where appropriate
- Tests separation of concerns, maintainability, and clean architecture
Example domains: Pricing engines, notification systems, workflow orchestration, fraud detection, approval pipelines
Each module follows this consistent structure:
<pattern-name>-pattern/
βββ pom.xml
βββ README.md # Pattern overview, when to use, when not to use
βββ docs/
β βββ exercise-1-pattern-in-isolation.md # Detailed exercise 1 instructions
β βββ exercise-2-real-world-simulation.md # Detailed exercise 2 instructions
β βββ solution-notes.md # High-level solution guidance (no code)
βββ src/
βββ main/java/com/patterns/<pattern>/
β βββ isolation/ # Exercise 1 starter code with TODOs
β βββ simulation/ # Exercise 2 starter code with TODOs
βββ test/java/com/patterns/<pattern>/
βββ isolation/ # Exercise 1 tests (make them pass!)
βββ simulation/ # Exercise 2 tests (make them pass!)
Start with the most commonly used and easiest to grasp:
- Strategy - Different algorithms, same interface
- Observer - Event notification system
- Factory Method - Object creation with subclasses
- Decorator - Add behavior dynamically
- Template Method - Algorithm skeleton with customizable steps
- Adapter - Make incompatible interfaces work together
- Singleton - Single instance control
- Command - Encapsulate requests as objects
Continue with more sophisticated patterns:
- Builder - Construct complex objects step by step
- Facade - Simplified interface to complex subsystem
- Proxy - Control access to objects
- State - Object behavior changes with internal state
- Chain of Responsibility - Pass requests along a chain
- Composite - Tree structures of objects
- Iterator - Sequential access without exposing representation
Tackle the more specialized patterns:
- Abstract Factory - Families of related objects
- Bridge - Separate abstraction from implementation
- Mediator - Centralize complex communications
- Memento - Capture and restore object state
- Prototype - Clone objects
- Flyweight - Share state among many objects
- Visitor - Operations on object structures
All exercises follow a Test-Driven Development approach:
- Read the exercise instructions in
docs/ - Review the failing tests in
src/test/ - Implement the solution in
src/main/ - Run tests until they all pass:
mvn test - Refactor and improve your code
- Check
docs/solution-notes.mdfor high-level guidance (if stuck)
Passing all tests means your solution meets the acceptance criteria!
- Do the isolation exercise first - Build confidence with the pattern mechanics
- Then tackle the simulation - Apply your knowledge to realistic scenarios
- Don't skip the README - Each pattern's README explains:
- What problem it solves
- When to use it
- When NOT to use it
- Make the tests pass - Tests are your acceptance criteria
- Read hints sparingly - Struggle a bit before looking at hints
- Study solution notes only after - Try your own solution first
- Use your brain, not AI assistants - The learning happens when you struggle and solve problems yourself. Resist the temptation to ask AI tools for solutions. Think, experiment, fail, and learn.
- Understand that implementations are intentionally simplified - Some exercises present "school-like" implementations to focus on pattern mechanics (e.g., State pattern). In production systems, you'll encounter more complex scenarios requiring advanced techniques.
- Experiment beyond the exercises - Once you pass the tests, challenge yourself:
- Create new packages within the module (e.g.,
isolation-advanced/orsimulation-complex/) - Implement variations that handle edge cases, concurrency, or performance constraints
- Combine patterns to solve more sophisticated problems
- Refactor your solution to support different complexity levels
- Create new packages within the module (e.g.,
Focus on object creation mechanisms, increasing flexibility and reuse.
- Factory Method, Abstract Factory, Builder, Prototype, Singleton
Explain how to assemble objects and classes into larger structures while keeping them flexible and efficient.
- Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy
Concerned with algorithms and the assignment of responsibilities between objects.
- Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor
- Java 21 - Latest LTS with modern language features
- Maven - Project management and build tool
- JUnit 5 - Testing framework
- AssertJ - Fluent assertions for readable tests
- Mockito - Mocking framework (when needed)
This project is provided for educational purposes.
Happy Learning! π
Start with any pattern that interests you, or follow the recommended study order above.