Skip to content

Latest commit

 

History

History
229 lines (133 loc) · 9.03 KB

File metadata and controls

229 lines (133 loc) · 9.03 KB

Head First Design Patterns - Java Examples

This repository contains Java implementations of the design patterns from the book "Head First Design Patterns" by Eric Freeman and Elisabeth Robson.

Design Patterns Catalog

Behavioral Patterns

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:

Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Examples:

Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Examples:

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:

Structural Patterns

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:

Composes objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

Examples:

Attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Examples:

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:

Provides a surrogate or placeholder for another object to control access to it.

Examples:

Creational Patterns

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:

Ensures a class has only one instance and provides a global point of access to it.

Examples:

Compound Patterns

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:

Quick Start

Prerequisites

  • Java Development Kit (JDK) 8 or higher
  • Basic understanding of object-oriented programming

Compilation

All examples compile into the out/ directory to keep source and compiled files separate. Run from the project root:

javac -d out <PatternName>/<files>.java

To compile everything at once, use the provided script:

./compile.sh

Running Examples

Run 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.PizzaTestDrive

Refer to each pattern's README for full compilation and execution instructions.

Design Principles

These examples demonstrate key object-oriented design principles:

  1. Encapsulate what varies - Identify the aspects that vary and separate them from what stays the same
  2. Program to interfaces, not implementations - Depend on abstractions, not concrete classes
  3. Favor composition over inheritance - Has-a can be better than is-a
  4. Strive for loosely coupled designs - Objects should interact with minimal knowledge of each other
  5. Classes should be open for extension but closed for modification - The Open/Closed Principle
  6. Depend upon abstractions, not concrete classes - The Dependency Inversion Principle
  7. Talk only to your friends - The Law of Demeter / Principle of Least Knowledge
  8. Don't call us, we'll call you - The Hollywood Principle (high-level components control low-level)
  9. A class should have only one reason to change - The Single Responsibility Principle

Pattern Categories Overview

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

Additional Resources

  • 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)

Notes

  • 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

License

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.