This repository contains Java implementations of the design patterns from the book "Head First Design Patterns" by Eric Freeman and Elisabeth Robson.
Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects.
Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Examples:
- BasicRemote - Home automation remote control
- LambdaRemote - Modern Java implementation with lambdas
Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Examples:
- MenuExample - Custom iterator implementation
- MenuJavaUtilsExample - Using Java's built-in Iterator
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Examples:
- ManualExample - Custom implementation
- JavaBuiltInExample - Using Java's Observable/Observer (deprecated)
Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
Examples:
- FirstTry - Initial implementation with conditionals
- FinalVersion - Proper state pattern with state classes
Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Example:
- StrategyPattern - Duck simulator with different flying and quacking behaviors
Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
Examples:
- CaffeineBeverageExample - Beverage preparation template
- JavaSortExample - Using Java's sorting as a template method
Structural patterns are concerned with how classes and objects are composed to form larger structures.
Converts the interface of a class into another interface that clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Example:
- SimpleAdapter - Adapting Turkey to Duck interface
Composes objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
Examples:
- CompositePattern - Menu component hierarchy
- CompositeIteratorPattern - Composite pattern with iterator
Attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
Examples:
- CoffeeExample - Starbucks beverage ordering system
- CustomIO - Custom I/O decorators
Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
Example:
- FacadePattern - Home theater facade
Provides a surrogate or placeholder for another object to control access to it.
Examples:
- VirtualProxyPattern - Image loading proxy
- RemoteProxyPattern - RMI-based remote proxy
- ProtectionProxy - Access control proxy
Creational patterns are concerned with the process of object creation.
Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
Examples:
- FactoryPatternPizzaStore - Factory Method pattern
- AbstractFactoryPatternPizzaStore - Abstract Factory pattern
Ensures a class has only one instance and provides a global point of access to it.
Examples:
- SimpleSingleton - Basic singleton (not thread-safe)
- Multithreading - Thread-safe implementations
Compound patterns combine two or more patterns into a solution that solves a recurring or general problem.
Model-View-Controller is a compound pattern consisting of the Observer, Strategy, and Composite patterns working together to decouple data, presentation, and user interaction.
Example:
- DJExample - DJ BeatBox and Heart Monitor applications
Demonstrates multiple patterns working together in a single application.
Example:
- DuckSimulatorExample - Combines multiple design patterns
- Java Development Kit (JDK) 8 or higher
- Basic understanding of object-oriented programming
All examples compile into the out/ directory to keep source and compiled files separate. Run from the project root:
javac -d out <PatternName>/<files>.javaTo compile everything at once, use the provided script:
./compile.shRun using -cp out to point Java at the compiled output directory:
java -cp out <PackageName>.<ClassName>For example:
java -cp out StrategyPattern.MiniDuckSimulator
java -cp out FactoryPattern.FactoryPatternPizzaStore.PizzaTestDriveRefer to each pattern's README for full compilation and execution instructions.
These examples demonstrate key object-oriented design principles:
- Encapsulate what varies - Identify the aspects that vary and separate them from what stays the same
- Program to interfaces, not implementations - Depend on abstractions, not concrete classes
- Favor composition over inheritance - Has-a can be better than is-a
- Strive for loosely coupled designs - Objects should interact with minimal knowledge of each other
- Classes should be open for extension but closed for modification - The Open/Closed Principle
- Depend upon abstractions, not concrete classes - The Dependency Inversion Principle
- Talk only to your friends - The Law of Demeter / Principle of Least Knowledge
- Don't call us, we'll call you - The Hollywood Principle (high-level components control low-level)
- A class should have only one reason to change - The Single Responsibility Principle
| Category | Patterns | Focus |
|---|---|---|
| Creational | Factory, Singleton | Object creation mechanisms |
| Structural | Adapter, Composite, Decorator, Facade, Proxy | Object composition and relationships |
| Behavioral | Command, Iterator, Observer, State, Strategy, Template | Communication between objects and responsibility distribution |
| Compound | MVC, Multi-Pattern | Multiple patterns working together |
- Book: "Head First Design Patterns" by Eric Freeman & Elisabeth Robson — Official code repository
- Design Patterns: "Design Patterns: Elements of Reusable Object-Oriented Software" by Gang of Four (GoF)
- Some examples using Java's deprecated APIs (like
java.util.Observable) are included for educational purposes but may not work on newer Java versions - Each pattern directory contains its own README with detailed explanations, benefits, use cases, and instructions
The original design pattern code examples are based on "Head First Design Patterns" (2nd Edition) by Eric Freeman & Elisabeth Robson (O'Reilly Media). All rights to those examples remain with the authors and publisher. Their official code repository is at github.com/bethrobson/Head-First-Design-Patterns.
Original contributions in this repository (build scripts, READMEs, project structure, package organisation) are released under the MIT License — see LICENSE for details.