
Head First Design Patterns
by Eric Freeman, Elisabeth Robson
2nd Edition
Publisher: O'Reilly Media
Book Details
| Print ISBN | 9781492078005 |
| eText ISBN | 9781492077954 |
| Publisher | O'Reilly Media |
| Publishing Year | 2021 |
| Edition | 2nd Edition |
| Language | English |
| Pages | 632 |
Head First Design Patterns, 2nd Edition, is a textbook by Eric Freeman and Elisabeth Robson, published by O'Reilly Media, that teaches essential software design patterns and their underlying object-oriented principles. The text demonstrates when to use specific patterns, explains why they matter, and guides readers in applying them to software designs.
Coverage spans foundational design concepts and real-world scenarios, examining patterns such as Observer, Decorator, Factory, and Singleton. The book also explores compound patterns, architectural principles, and strategies for managing dependencies and collections.
Presented in a visually rich format aligned with how the brain processes information, the textbook supports practice through simplified diagrams, lean code examples, and interactive exercises.
Table of Contents
Chapter Intro: How to use this Book
- • Who is this book for?
- • Who should probably back away from this book?
- • We know what you’re thinking.
- • And we know what your brain is thinking.
- • Metacognition: thinking about thinking
- • Here’s what WE did:
- • Here’s what YOU can do to bend your brain into submission
- • Read Me
- • We use simple UML-like diagrams.
- • We don’t cover every single Design Pattern ever created.
- • The activities are NOT optional.
- • We use the word “composition” in the general OO sense, which is more flexible than the strict UML use of “composition.”
- • The redundancy is intentional and important.
- • The code examples are as lean as possible.
- • The Brain Power exercises don’t have answers.
- • Tech Reviewers
- • Tech Reviewers, 2nd Edition
- • Acknowledgments
- • From the first edition
- • From the second edition
- • Very Special Thanks
Chapter 1: Welcome to Design Patterns: Intro to Design Patterns
- • It started with a simple SimUDuck app
- • But now we need the ducks to FLY
- • But something went horribly wrong...
- • Joe thinks about inheritance...
- • How about an interface?
- • What would you do if you were Joe?
- • The one constant in software development
- • Zeroing in on the problem...
- • Separating what changes from what stays the same
- • Designing the Duck Behaviors
- • Implementing the Duck Behaviors
- • there are no Dumb Questions
- • Integrating the Duck Behaviors
- • More integration...
- • Testing the Duck code
- • Setting behavior dynamically
- • The Big Picture on encapsulated behaviors
- • HAS-A can be better than IS-A
- • Speaking of Design Patterns...
- • Design Puzzle
- • Overheard at the local diner...
- • Overheard in the next cubicle...
- • The power of a shared pattern vocabulary
- • How do I use Design Patterns?
- • Tools for your Design Toolbox
- • Design Patterns Crossword
- • Design Puzzle Solution
- • Design Patterns Crossword Solution
Chapter 2: Keeping your Objects in the Know: The Observer Pattern
- • The Weather Monitoring application overview
- • Unpacking the WeatherData class
- • Our Goal
- • Stretch Goal
- • Taking a first, misguided implementation of the Weather Station
- • What’s wrong with our implementation anyway?
- • Meet the Observer Pattern
- • Publishers + Subscribers = Observer Pattern
- • A day in the life of the Observer Pattern
- • Five-minute drama: a subject for observation
- • Two weeks later...
- • The Observer Pattern defined
- • The Observer Pattern: the Class Diagram
- • there are no Dumb Questions
- • The Power of Loose Coupling
- • Cubicle conversation
- • Designing the Weather Station
- • Implementing the Weather Station
- • Implementing the Subject interface in WeatherData
- • Now, let’s build those display elements
- • Power up the Weather Station
- • Looking for the Observer Pattern in the Wild
- • The Swing library
- • A little life-changing application
- • Coding the life-changing application
- • Meanwhile, back at Weather-O-Rama
- • For the Subject to send notifications...
- • For an Observer to receive notifications...
- • Code Magnets
- • Test Drive the new code
- • Tools for your Design Toolbox
- • Design Principle Challenge
- • Design Patterns Crossword
- • Design Principle Challenge Solution
- • Code Magnets Solution
- • Design Patterns Crossword Solution
Chapter 3: Decorating Objects: The Decorator Pattern
- • Welcome to Starbuzz Coffee
- • The Open-Closed Principle
- • there are no Dumb Questions
- • Meet the Decorator Pattern
- • Constructing a drink order with Decorators
- • Okay, here’s what we know about Decorators, so far...
- • The Decorator Pattern defined
- • Decorating our Beverages
- • Cubicle Conversation
- • New barista training
- • Writing the Starbuzz code
- • Coding beverages
- • Coding condiments
- • Serving some coffees
- • Real-World Decorators: Java I/O
- • Decorating the java.io classes
- • Writing your own Java I/O Decorator
- • Test out your new Java I/O Decorator
- • Tools for your Design Toolbox
Chapter 4: Baking with OO Goodness: The Factory Pattern
- • Identifying the aspects that vary
- • But the pressure is on to add more pizza types
- • Encapsulating object creation
- • Building a simple pizza factory
- • there are no Dumb Questions
- • Reworking the PizzaStore class
- • The Simple Factory defined
- • Franchising the pizza store
- • We’ve seen one approach...
- • But you’d like a little more quality control...
- • A framework for the pizza store
- • Allowing the subclasses to decide
- • Let’s make a Pizza Store
- • Declaring a factory method
- • Let’s see how it works: ordering pizzas with the pizza factory method
- • So how do they order?
- • Let’s check out how these pizzas are really made to order...
- • We’re just missing one thing: Pizzas!
- • Our Pizza Store isn’t going to be very popular without some pizzas, so let’s implement them
- • Now we just need some concrete subclasses...how about defining New York and Chicago-style cheese pizzas?
- • You’ve waited long enough. Time for some pizzas!
- • It’s finally time to meet the Factory Method Pattern
- • The Creator classes
- • The Product classes
- • View Creators and Products in Parallel
- • Design Puzzle
- • Factory Method Pattern defined
- • Looking at object dependencies
- • The Dependency Inversion Principle
- • Applying the Principle
- • Inverting your thinking...
- • A few guidelines to help you follow the Principle...
- • Meanwhile, back at the Pizza Store...
- • Ensuring consistency in your ingredients
- • Families of ingredients...
- • Building the ingredient factories
- • Building the New York ingredient factory
- • Reworking the pizzas...
- • Reworking the pizzas, continued...
- • Revisiting our pizza stores
- • What have we done?
- • More pizza for Ethan and Joel...
- • Ethan and Joel can’t get enough Objectville Pizza! What they don’t know is that now their orders are making use of the new ingredient factories. So now when they order...
- • From here things change, because we are using an ingredient factory
- • Abstract Factory Pattern defined
- • Factory Method and Abstract Factory compared
- • Tools for your Design Toolbox
- • Design Patterns Crossword
- • Design Puzzle Solution
- • Design Patterns Crossword Solution
Chapter 5: One-of-a-Kind Objects: The Singleton Pattern
- • The Little Singleton
- • A small Socratic exercise in the style of The Little Lisper
- • Dissecting the classic Singleton Pattern implementation
- • The Chocolate Factory
- • Singleton Pattern defined
- • Hershey, PA, we have a problem...
- • BE the JVM
- • Dealing with multithreading
- • Can we improve multithreading?
- • 1. Do nothing if the performance of getInstance() isn’t critical to your application.
- • 2. Move to an eagerly created instance rather than a lazily created one.
- • 3. Use “double-checked locking” to reduce the use of synchronization in getInstance().
- • Meanwhile, back at the Chocolate Factory...
- • Congratulations!
- • there are no Dumb Questions
- • Tools for your Design Toolbox
- • Design Patterns Crossword
- • Design Patterns Crossword Solution
Chapter 6: Encapsulating Invocation: The Command Pattern
- • Home Automation or Bust, Inc.
- • Free hardware! Let’s check out the Remote Control...
- • Taking a look at the vendor classes
- • Cubicle Conversation
- • Meanwhile, back at the Diner..., or, A brief introduction to the Command Pattern
- • Let’s study the interaction in a little more detail...
- • The Objectville Diner roles and responsibilities
- • From the Diner to the Command Pattern
- • Our first command object
- • Using the command object
- • Creating a simple test to use the Remote Control
- • The Command Pattern defined
- • The Command Pattern defined: the class diagram
- • Assigning Commands to slots
- • Implementing the Remote Control
- • Implementing the Commands
- • Putting the Remote Control through its paces
- • Now, let’s check out the execution of our remote control test...
- • Time to write that documentation...
- • What are we doing?
- • Time to QA that Undo button!
- • Using state to implement Undo
- • Adding Undo to the Ceiling Fan commands
- • Get ready to test the ceiling fan
- • Testing the ceiling fan...
- • Every remote needs a Party Mode!
- • Using a macro command
- • there are no Dumb Questions
- • More uses of the Command Pattern: queuing requests
- • More uses of the Command Pattern: logging requests
- • Command Pattern in the Real World
- • Tools for your Design Toolbox
- • Design Patterns Crossword
Chapter 7: Being Adaptive: The Adapter and Facade Patterns
- • Adapters all around us
- • Object-oriented adapters
- • If it walks like a duck and quacks like a duck, then it must might be a duck turkey wrapped with a duck adapter...
- • Test drive the adapter
- • The Adapter Pattern explained
- • Here’s how the Client uses the Adapter
- • there are no Dumb Questions
- • Adapter Pattern defined
- • Object and class adapters
- • Duck Magnets
- • Duck Magnets Answer
- • Real-world adapters
- • Enumerators
- • Iterators
- • Using Enumerators with code that expects Iterators
- • Adapting an Enumeration to an Iterator
- • Designing the Adapter
- • Dealing with the remove() method
- • Writing the EnumerationIterator adapter
- • And now for something different...
- • Home Sweet Home Theater
- • Watching a movie (the hard way)
- • Lights, Camera, Facade!
- • Constructing your home theater facade
- • Implementing the simplified interface
- • Time to watch a movie (the easy way)
- • Facade Pattern defined
- • The Principle of Least Knowledge
- • How NOT to Win Friends and Influence Objects
- • Keeping your method calls in bounds...
- • The Facade Pattern and the Principle of Least Knowledge
- • Tools for your Design Toolbox
- • Design Patterns Crossword
- • Design Patterns Crossword Solution
Chapter 8: Encapsulating Algorithms: The Template Method Pattern
- • It’s time for some more caffeine
- • Whipping up some coffee and tea classes (in Java)
- • And now the Tea...
- • Let’s abstract that Coffee and Tea
- • Taking the design further...
- • Abstracting prepareRecipe()
- • What have we done?
- • Meet the Template Method
- • Let’s make some tea...
- • What did the Template Method get us?
- • Template Method Pattern defined
- • Hooked on Template Method...
- • Using the hook
- • Let’s run the Test Drive
- • And let’s give it a run...
- • there are no Dumb Questions
- • The Hollywood Principle
- • The Hollywood Principle and Template Method
- • Template Methods in the Wild
- • Sorting with Template Method
- • We’ve got some ducks to sort...
- • What is compareTo()?
- • Comparing Ducks and Ducks
- • Let’s sort some Ducks
- • Let the sorting commence!
- • The making of the sorting duck machine
- • Swingin’ with Frames
- • Custom Lists with AbstractList
- • Design Patterns Crossword
- • Tools for your Design Toolbox
- • Design Patterns Crossword Solution
Chapter 9: Well-Managed Collections: The Iterator and Composite Patterns
- • Breaking News: Objectville Diner and Objectville Pancake House Merge
- • Check out the Menu Items
- • Lou and Mel’s Menu implementations
- • What’s the problem with having two different menu representations?
- • The Java-Enabled Waitress Specification
- • Implementing the spec: our first attempt
- • What now?
- • Can we encapsulate the iteration?
- • Meet the Iterator Pattern
- • Adding an Iterator to DinerMenu
- • Reworking the DinerMenu with Iterator
- • Fixing up the Waitress code
- • Testing our code
- • Here’s the test run...
- • What have we done so far?
- • Reviewing our current design...
- • Making some improvements...
- • there are no Dumb Questions
- • Cleaning things up with java.util.Iterator
- • We are almost there...
- • What does this get us?
- • Iterator Pattern defined
- • The Iterator Pattern Structure
- • The Single Responsibility Principle
- • Meet Java’s Iterable interface
- • Java’s enhanced for loop
- • Not so fast; Arrays are not Iterables
- • Taking a look at the Café Menu
- • Reworking the Café Menu code
- • Adding the Cafe Menu to the Waitress
- • Breakfast, lunch, AND dinner
- • Here’s the test run; check out the new dinner menu from the Café!
- • What did we do?
- • We decoupled the Waitress....
- • ...and we made the Waitress more extensible
- • But there’s more!
- • Iterators and Collections
- • Code Magnets
- • Is the Waitress ready for prime time?
- • Just when we thought it was safe...
- • What do we need?
- • The Composite Pattern defined
- • Designing Menus with Composite
- • Implementing MenuComponent
- • Implementing the MenuItem
- • Implementing the Composite Menu
- • Fixing the print() method
- • Getting ready for a test drive...
- • Now for the test drive...
- • Design Patterns Crossword
- • Tools for your Design Toolbox
- • Code Magnets Solution
- • Design Patterns Crossword Solution
Chapter 10: The State of Things: The State Pattern
- • Java Breakers
- • Cubicle Conversation
- • State machines 101
- • Writing the code
- • In-house testing
- • You knew it was coming...a change request!
- • Design Puzzle
- • The messy STATE of things...
- • The new design
- • Defining the State interfaces and classes
- • Implementing our State classes
- • Reworking the Gumball Machine
- • Now, let’s look at the complete GumballMachine class...
- • Implementing more states
- • Let’s take a look at what we’ve done so far...
- • The State Pattern defined
- • there are no Dumb Questions
- • We still need to finish the Gumball 1 in 10 game
- • Finishing the game
- • Demo for the CEO of Mighty Gumball, Inc.
- • Sanity check...
- • We almost forgot!
- • Tools for your Design Toolbox
- • Design Puzzle Solution
Chapter 11: Controlling Object Access: The Proxy Pattern
- • Coding the Monitor
- • Testing the Monitor
- • The role of the ‘remote proxy’
- • Adding a remote proxy to the Gumball Machine monitoring code
- • Remote methods 101
- • Walking through the design
- • How the method call happens
- • Java RMI, the Big Picture
- • Making the Remote service
- • Step one: make a Remote interface
- • Step two: make a Remote implementation
- • Step four: start the service
- • there are no Dumb Questions
- • Complete code for the server side
- • Complete code for the client side
- • Back to our GumballMachine remote proxy
- • Getting the GumballMachine ready to be a remote service
- • Registering with the RMI registry...
- • Now for the GumballMonitor client...
- • Writing the Monitor test drive
- • Another demo for the CEO of Mighty Gumball...
- • And now let’s put the monitor in the hands of the CEO. Hopefully, this time he’ll love it:
- • The Proxy Pattern defined
- • Get ready for the Virtual Proxy
- • Remote Proxy
- • Virtual Proxy
- • Displaying Album covers
- • Designing the Album Cover Virtual Proxy
- • How ImageProxy is going to work:
- • Writing the Image Proxy
- • Testing the Album Cover Viewer
- • Things to try...
- • What did we do?
- • Using the Java API’s Proxy to create a protection proxy
- • Geeky Matchmaking in Objectville
- • The Person implementation
- • Five-minute drama: protecting subjects
- • Big Picture: creating a Dynamic Proxy for the Person
- • Step one: creating Invocation Handlers
- • Creating Invocation Handlers, continued...
- • Step two: creating the Proxy class and instantiating the Proxy object
- • Testing the matchmaking service
- • Running the code...
- • The Proxy Zoo
- • Design Patterns Crossword
- • Tools for your Design Toolbox
- • Design Patterns Crossword Solution
- • The code for the Album Cover Viewer
Chapter 12: Patterns of Patterns: Compound Patterns
- • Working together
- • Duck reunion
- • there are no Dumb Questions
- • What did we do?
- • A duck’s-eye view: the class diagram
- • The King of Compound Patterns
- • If Elvis were a compound pattern, his name would be Model-View-Controller, and he’d be singing a little song like this...
- • Meet Model-View-Controller
- • A closer look...
- • Understanding MVC as a set of Patterns
- • Observer
- • Strategy
- • Composite
- • Using MVC to control the beat...
- • Meet the Java DJ View
- • The controller is in the middle...
- • Let’s not forget about the model underneath it all...
- • Putting the pieces together
- • Building the pieces
- • Let’s check out the BeatModelInterface before looking at the implementation:
- • Now let’s have a look at the concrete BeatModel class
- • The View
- • Implementing the View
- • Now for the Controller
- • And here’s the implementation of the controller:
- • Putting it all together...
- • And now for a test run...
- • Things to try
- • Exploring Strategy
- • Adapting the Model
- • Now we’re ready for a HeartController
- • And that’s it! Now it’s time for some test code...
- • Tools for your Design Toolbox
- • Exercise Solutions
- • The Beat Model
- • The Controller
- • The Heart Model
- • The Heart Adapter
Chapter 13: Patterns in the Real World: Better Living with Patterns
- • Design Pattern defined
- • Looking more closely at the Design Pattern definition
- • there are no Dumb Questions
- • So you wanna be a Design Patterns writer
- • Organizing Design Patterns
- • Pattern Categories
- • Thinking in Patterns
- • Keep it simple (KISS)
- • Design Patterns aren’t a magic bullet; in fact, they’re not even a bullet!
- • You know you need a pattern when...
- • Refactoring time is Patterns time!
- • Take out what you don’t really need. Don’t be afraid to remove a Design Pattern from your design.
- • If you don’t need it now, don’t do it now.
- • Your Mind on Patterns
- • Don’t forget the power of the shared vocabulary
- • Cruisin’ Objectville with the Gang of Four
- • Your journey has just begun...
- • The definitive Design Patterns text
- • The definitive Patterns texts
- • Other Design Patterns resources
- • The Patterns Zoo
- • Annihilating evil with Anti-Patterns
- • Tools for your Design Toolbox
- • Leaving Objectville...
- • Boy, it’s been great having you in Objectville.
Chapter A: Leftover Patterns
- • Bridge
- • Your dilemma
- • Why use the Bridge Pattern?
- • Builder
- • You need a flexible design
- • Why use the Builder Pattern?
- • Chain of Responsibility
- • A scenario
- • Your task
- • How to use the Chain of Responsibility Pattern
- • Flyweight
- • Your big client’s dilemma
- • Why use the Flyweight Pattern?
- • Interpreter
- • Now what?
- • How to implement an interpreter
- • Mediator
- • HouseOfTheFuture’s dilemma
- • Mediator in action...
- • Memento
- • The Memento at work
- • Prototype
- • Prototype to the rescue
- • Visitor
- • The Visitor drops by
Customer Reviews
0.0
0 reviews
No reviews yet. Be the first to review this book!
Write a Review
Reviewed by GradeFocus Editorial Team





