This repository contains the complete code examples for Head First Design Patterns (2nd Edition, 2020). It is an educational codebase implementing all 23 Gang of Four design patterns using Java 8+ with practical, real-world examples.
Primary Purpose: Learning and teaching software design patterns through executable examples.
- Language: Java 8+
- Build Tool: Maven 3+ (with Maven Wrapper included)
- IDE Support: Eclipse, IntelliJ IDEA
- Dependencies: Minimal (javax.servlet-api 4.0.0)
- Project Type: Multi-pattern educational examples (not a production application)
/
├── src/ # All source code
│ └── headfirst/designpatterns/ # Root package for all patterns
├── pom.xml # Maven build configuration
├── mvnw, mvnw.cmd # Maven wrapper scripts
├── README.md # User-facing documentation
└── CLAUDE.md # This file - AI assistant guide
Base Package: headfirst.designpatterns
Pattern Directory Structure:
src/headfirst/designpatterns/
├── PATTERN_NAME/ # One directory per pattern
│ ├── Core interfaces/classes # Pattern abstractions
│ ├── Concrete classes # Implementations
│ ├── *TestDrive.java # Executable demo (main method)
│ ├── *Simulator.java # Simulation demo (main method)
│ └── VARIANT_NAME/ # Pattern variations/evolutions
│ └── ... # Variant-specific code
| Category | Patterns | Key Directories |
|---|---|---|
| Creational | Factory Method, Abstract Factory, Singleton, Builder, Prototype | factory/, singleton/, builder/, prototype/ |
| Structural | Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy | adapter/, bridge/, composite/, decorator/, facade/, flyweight/, proxy/ |
| Behavioral | Observer, Strategy, Command, State, Template Method, Iterator | observer/, strategy/, command/, state/, templatemethod/, iterator/ |
| Special | Combined patterns, Pattern combinations | combined/, combining/ |
Patterns with Multiple Variants:
- singleton/: 7 variants (classic, threadsafe, dcl, enumS, stat, subclass, chocolate)
- command/: 9 variants including Lambda versions (dinerLambda, remoteWL, simpleremoteWL)
- observer/: 4 variants (weather, weatherobservable, simple, swing)
- factory/: 4 variants (pizzas, pizzafm, pizzaaf, challenge)
- decorator/: 4 variants (starbuzz, starbuzzWithSizes, pizza, io)
Special Combined Patterns:
- combined/djview/: Full MVC application with Observer, Adapter, Strategy patterns + JSP servlets
- combining/: Demonstrates multiple patterns working together (71 files)
headfirst.designpatterns.PATTERN_NAME
headfirst.designpatterns.PATTERN_NAME.VARIANT_NAME
Executable Demo Classes (contain main() methods):
*TestDrive.java- Most common (e.g.,DuckTestDrive.java,PizzaTestDrive.java)*Simulator.java- Simulation examples (e.g.,MiniDuckSimulator.java)*Director.java- Builder pattern directors*Client.java- Client code demonstrations*Cafe.java,*Diner.java- Scenario-based examples
Pattern Components:
- Interfaces: Often named after pattern role (e.g.,
Observer.java,Subject.java,Command.java) - Abstract classes: Base implementations (e.g.,
Beverage.java,CondimentDecorator.java) - Concrete classes: Descriptive names (e.g.,
MallardDuck.java,Espresso.java,Mocha.java)
- One pattern per top-level directory under
src/headfirst/designpatterns/ - Variants in subdirectories showing pattern evolution or alternative implementations
- Self-contained examples - each variant can be run independently
- Minimal cross-pattern dependencies - patterns are isolated for learning
# Install dependencies
./mvnw install
# Clean build
./mvnw clean install
# Compile only
./mvnw compile# Generate Eclipse project files
./mvnw eclipse:eclipse
# Generate IntelliJ IDEA project files
./mvnw idea:ideaDirect Java Execution:
# After compilation, run any TestDrive class
java -cp target/classes headfirst.designpatterns.strategy.MiniDuckSimulator
java -cp target/classes headfirst.designpatterns.observer.weather.WeatherStationMaven Execution:
# Use exec plugin (if configured)
./mvnw exec:java -Dexec.mainClass="headfirst.designpatterns.PATTERN.TestDrive"Finding all executable examples:
# Search for classes with main methods
grep -r "public static void main" src/Locating a specific pattern:
# Pattern directories are at src/headfirst/designpatterns/PATTERN_NAME/
ls src/headfirst/designpatterns/-
Understand the Educational Context
- This is a learning repository, not production code
- Examples prioritize clarity and teaching over optimization
- Code duplication across variants is intentional for pedagogical reasons
- Comments and verbose naming are features, not bugs
-
Respect Pattern Isolation
- Each pattern directory is self-contained
- Avoid creating cross-pattern dependencies unless in
combining/orcombined/ - Changes to shared classes (e.g.,
ducks/) affect multiple patterns
-
Maintain Consistency with Existing Code
- Follow established naming conventions (
*TestDrive,*Simulator) - Keep the simple, educational style of existing code
- Preserve comments that explain pattern concepts
- Match indentation and formatting of surrounding code
- Follow established naming conventions (
-
When Adding New Pattern Examples
- Create subdirectory under appropriate pattern (e.g.,
strategy/newvariant/) - Include at least one executable class with
main()method - Add clear comments explaining the pattern demonstration
- Follow package naming:
headfirst.designpatterns.PATTERN.VARIANT
- Create subdirectory under appropriate pattern (e.g.,
-
When Modifying Existing Patterns
- Read the original implementation first to understand the teaching goal
- Preserve the pedagogical value of the example
- If improving code, ensure it remains beginner-friendly
- Test the executable examples after changes
-
Testing Approach
- This repository uses manual testing via
main()methods - No unit testing framework is configured (educational focus)
- Verify changes by running the relevant
*TestDriveclasses - Ensure output demonstrates the pattern correctly
- This repository uses manual testing via
-
Documentation Standards
- Inline comments should explain why (pattern concepts), not what (obvious code)
- Class-level JavaDoc should describe the pattern role
- Keep explanations simple and accessible to beginners
- Reference the Head First Design Patterns book when relevant
Java Version: Java 8 features are acceptable (lambdas, streams)
- Some patterns have explicit lambda variants (e.g.,
command/dinerLambda/) - Default to classic Java for pattern demonstrations unless showing modern alternatives
Formatting:
- Indentation: Tabs or spaces matching existing code in that directory
- Braces: Opening brace on same line (
if (condition) {) - Line length: No strict limit, prioritize readability
Naming:
- Classes: PascalCase
- Methods: camelCase
- Constants: UPPER_SNAKE_CASE
- Packages: lowercase, no underscores
- Don't over-engineer examples - Keep them simple and focused
- Don't add production-grade features - No logging frameworks, complex error handling, etc.
- Don't remove pattern "smells" - Some patterns intentionally show problems before solutions
- Don't consolidate duplicate code - Variants are meant to be standalone
- Don't add external dependencies - Keep the project lightweight
Singleton Pattern (singleton/):
- Multiple variants show evolution from flawed to correct implementations
- Thread-safety examples are teaching tools, not production recommendations
- Enum singleton (
enumS/) is the recommended modern approach
Factory Pattern (factory/):
pizzafm/= Factory Method patternpizzaaf/= Abstract Factory pattern- Don't mix these distinct pattern variants
Observer Pattern (observer/):
weather/= Custom implementation (push model)weatherobservable/= Java's built-in Observable (deprecated in Java 9+)- Keep both for comparison purposes
Command Pattern (command/):
- Largest pattern directory (94 files, 9 variants)
- Lambda variants demonstrate modern Java alternatives
- Each variant is a complete, self-contained example
Combined/Combining (combined/, combining/):
- Shows real-world usage of multiple patterns together
- More complex than individual pattern examples
combined/djview/includes web components (JSP, servlets)
Pattern locations (all under /home/user/Head-First-Design-Patterns/src/headfirst/designpatterns/):
adapter/ - Adapter pattern (ducks, iterenum)
bridge/ - Bridge pattern (remote controls)
builder/ - Builder pattern (pizza, house, vacation)
collections/ - Collection patterns (iterator)
combined/ - Multi-pattern MVC application (djview)
combining/ - Pattern combination examples
command/ - Command pattern (9 variants)
composite/ - Composite pattern (menus)
decorator/ - Decorator pattern (starbuzz, pizza, io)
ducks/ - Shared duck classes
facade/ - Facade pattern (home theater)
factory/ - Factory patterns (pizzafm, pizzaaf)
flyweight/ - Flyweight pattern
iterator/ - Iterator pattern (5 variants)
iterenum/ - Iterator/Enumeration adapter
observer/ - Observer pattern (4 variants)
prototype/ - Prototype pattern
proxy/ - Proxy pattern (gumball, virtual, dynamic)
singleton/ - Singleton pattern (7 variants)
state/ - State pattern (gumball machine)
strategy/ - Strategy pattern (ducks)
templatemethod/ - Template Method pattern (6 variants)
- Main branch:
master - Feature branches:
claude/claude-md-*(for AI assistant work)
- Keep commits focused on single patterns or concepts
- Use clear commit messages: "Add Builder pattern house example" or "Fix Singleton double-checked locking"
- Reference pattern names in commit messages for clarity
# Compile entire project
./mvnw clean compile
# Run affected TestDrive classes manually
java -cp target/classes headfirst.designpatterns.PATTERN.TestDriveSource Directory: src/ (non-standard, typically would be src/main/java/)
Target Java: 1.8 (source and target)
Dependencies: javax.servlet-api 4.0.0 (only for combined/djview web examples)
Important: The source directory configuration is non-standard. Most patterns don't use servlets - the dependency is only for the combined/djview/ examples.
- Book Website: http://wickedlysmart.com/head-first-design-patterns/
- Pattern Reference: Gang of Four Design Patterns
- Java Version: Java 8+ required (for lambda examples)
# List all patterns
ls src/headfirst/designpatterns/
# Find all executable examples
grep -r "public static void main" src/ | cut -d: -f1
# Search for specific pattern usage
grep -r "implements Observer" src/
# Check Java version
java -version
# Verify Maven setup
./mvnw --version
# Full clean build
./mvnw clean install- Total Java Files: ~677 files
- Largest Pattern: Command (94 files)
- Most Variants: Command (9 variants), Singleton (7 variants)
- Pattern Count: 23 design patterns covered
- Always compile and test changes by running
./mvnw compileand executing relevant TestDrive classes - Read existing implementations in a pattern directory before making changes
- Preserve educational value - clarity trumps cleverness in this codebase
- Match existing style - consistency is important for learners
- Document pattern concepts - explain the "why" behind design decisions
- Keep variants independent - don't create shared dependencies between pattern variants
- Respect the book's approach - this code follows the Head First teaching methodology
Last Updated: 2025-11-13 Repository: Head-First-Design-Patterns (2nd Edition) Maintainer: Educational resource for design patterns