Head First Design Patterns, 2nd Edition

Book description

What will you learn from this book?

You know you don't want to reinvent the wheel, so you look to Design Patterns: the lessons learned by those who've faced the same software design problems. With Design Patterns, you get to take advantage of the best practices and experience of others so you can spend your time on something more challenging. Something more fun. This book shows you the patterns that matter, when to use them and why, how to apply them to your own designs, and the object-oriented design principles on which they're based. Join hundreds of thousands of developers who've improved their object-oriented design skills through Head First Design Patterns.

What's so special about this book?

If you've read a Head First book, you know what to expect: a visually rich format designed for the way your brain works. With Head First Design Patterns, 2E you'll learn design principles and patterns in a way that won't put you to sleep, so you can get out there to solve software design problems and speak the language of patterns with others on your team.

Publisher resources

View/Submit Errata

Table of contents

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

Product information

  • Title: Head First Design Patterns, 2nd Edition
  • Author(s): Eric Freeman, Elisabeth Robson
  • Release date: December 2020
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492078005