O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Head First Design Patterns

Book Description

This edition of Head First Design Patterns—now updated for Java 8—shows you the tried-and-true, road-tested patterns used by developers to create functional, elegant, reusable, and flexible software. By the time you finish this book, you’ll be able to take advantage of the best design practices and experiences of those who have fought the beast of software design and triumphed.

Table of Contents

  1. Dedication
  2. Praise for Head First Design Patterns
  3. More Praise for Head First Design Patterns
  4. Praise for other books by Eric Freeman and Elisabeth Robson
  5. Authors of Head First Design Patterns
  6. Creators of the Head First series (and co-conspirators on this book)
  7. How to Use This Book: Intro
    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
    8. Tech Reviewers
    9. Acknowledgments
    10. Even more people
  8. 1. Intro to Design Patterns: Welcome 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. Integrating the Duck Behavior
    13. More integration...
    14. Testing the Duck code
    15. Setting behavior dynamically
    16. The Big Picture on encapsulated behaviors
    17. HAS-A can be better than IS-A
    18. Speaking of Design Patterns...
    19. Overheard at the local diner...
    20. Overheard in the next cubicle...
    21. The power of a shared pattern vocabulary
    22. How do I use Design Patterns?
    23. Tools for your Design Toolbox
  9. 2. The Observer Pattern: Keeping your Objects in the know
    1. The Weather Monitoring application overview
    2. Unpacking the WeatherData class
    3. What do we know so far?
    4. Taking a first, misguided SWAG at the Weather Station
    5. What’s wrong with our implementation?
    6. Meet the Observer Pattern
    7. Publishers + Subscribers = Observer Pattern
    8. A day in the life of the Observer Pattern
    9. Five-minute drama: a subject for observation
    10. Two weeks later...
    11. The Observer Pattern defined
    12. The Observer Pattern defined: the class diagram
    13. The power of Loose Coupling
    14. Cubicle conversation
    15. Designing the Weather Station
    16. Implementing the Weather Station
    17. Implementing the Subject interface in WeatherData
    18. Now, let’s build those display elements
    19. Power up the Weather Station
    20. Using Java’s built-in Observer Pattern
    21. How Java’s built-in Observer Pattern works
    22. Reworking the Weather Station with the built-in support
    23. Running the new code
    24. The dark side of java.util.Observable
    25. Other places you’ll find the Observer Pattern in the JDK
    26. And the code...
      1. The updated code, using lambda expressions
    27. Tools for your Design Toolbox
  10. 3. The Decorator Pattern: Decorating Objects
    1. Welcome to Starbuzz Coffee
    2. The Open-Closed Principle
    3. Meet the Decorator Pattern
    4. Constructing a drink order with Decorators
      1. Okay, here’s what we know so far...
    5. The Decorator Pattern defined
    6. Decorating our Beverages
    7. Cubicle Conversation
    8. New barista training
    9. Writing the Starbuzz code
    10. Coding beverages
    11. Coding condiments
    12. Serving some coffees
    13. Real World Decorators: Java I/O
    14. Decorating the java.io classes
    15. Writing your own Java I/O Decorator
    16. Test out your new Java I/O Decorator
      1. Give it a spin
    17. Tools for your Design Toolbox
  11. 4. The Factory Pattern: Baking with OO Goodness
    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. Reworking the PizzaStore class
    6. The Simple Factory defined
    7. Franchising the pizza store
      1. We’ve seen one approach...
      2. But you’d like a little more quality control...
    8. A framework for the pizza store
    9. Allowing the subclasses to decide
    10. Let’s make a PizzaStore
    11. 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...
    12. We’re just missing one thing: PIZZA!
      1. Our PizzaStore 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?
    13. You’ve waited long enough. Time for some pizzas!
    14. It’s finally time to meet the Factory Method Pattern
      1. The Creator classes
      2. The Product classes
    15. Another perspective: parallel class hierarchies
    16. Factory Method Pattern defined
    17. A very dependent PizzaStore
    18. Looking at object dependencies
    19. The Dependency Inversion Principle
    20. Applying the Principle
    21. Inverting your thinking...
    22. A few guidelines to help you follow the Principle...
    23. Meanwhile, back at the PizzaStore...
      1. Ensuring consistency in your ingredients
    24. Families of ingredients...
    25. Building the ingredient factories
    26. Building the New York ingredient factory
    27. Reworking the pizzas...
    28. Reworking the pizzas, continued...
    29. Revisiting our pizza stores
    30. What have we done?
    31. More pizza for Ethan and Joel...
      1. From here things change, because we are using an ingredient factory
    32. Abstract Factory Pattern defined
    33. Factory Method and Abstract Factory compared
    34. Tools for your Design Toolbox
    35. A very dependent PizzaStore
  12. 5. The Singleton Pattern: One of a Kind Objects
    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. Houston, Hershey, PA we have a problem...
    6. Dealing with multithreading
    7. 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().
    8. Meanwhile, back at the Chocolate Factory...
    9. Congratulations!
    10. Tools for your Design Toolbox
  13. 6. The Command Pattern: Encapsulating Invocation
    1. Free hardware! Let’s check out the Remote Control...
    2. Taking a look at the vendor classes
    3. Cubicle Conversation
    4. Meanwhile, back at the Diner..., or, A brief introduction to the Command Pattern
    5. Let’s study the interaction in a little more detail...
    6. The Objectville Diner roles and responsibilities
    7. From the Diner to the Command Pattern
    8. Our first command object
    9. Using the command object
    10. Creating a simple test to use the Remote Control
    11. The Command Pattern defined
    12. The Command Pattern defined: the class diagram
    13. Assigning Commands to slots
    14. Implementing the Remote Control
    15. Implementing the Commands
    16. Putting the Remote Control through its paces
      1. Now, let’s check out the execution of our remote control test...
    17. Time to write that documentation...
    18. What are we doing?
    19. Time to QA that Undo button!
    20. Using state to implement Undo
    21. Adding Undo to the CeilingFan commands
    22. Get ready to test the ceiling fan
    23. Testing the ceiling fan...
    24. Every remote needs a Party Mode!
    25. Using a macro command
    26. The Command Pattern means lots of command classes
      1. Do we really need all these command classes?
    27. Simplifying the Remote Control with lambda expressions
    28. Simplifying even more with method references
      1. What if we need to do more than one thing in our lambda expression?
    29. Test the remote control with lambda expressions
      1. Check out the results of all those lambda expression commands...
    30. More uses of the Command Pattern: queuing requests
    31. More uses of the Command Pattern: logging requests
    32. Tools for your Design Toolbox
  14. 7. The Adapter and Facade Patterns: Being Adaptive
    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. Adapter Pattern defined
    7. Object and class adapters
    8. Real-world adapters
      1. Old-world Enumerators
      2. New-world Iterators
      3. And today...
    9. Adapting an Enumeration to an Iterator
      1. Designing the Adapter
      2. Dealing with the remove() method
      3. Writing the EnumerationIterator adapter
    10. And now for something different...
    11. Home Sweet Home Theater
    12. Watching a movie (the hard way)
    13. Lights, Camera, Facade!
    14. Constructing your home theater facade
    15. Implementing the simplified interface
    16. Time to watch a movie (the easy way)
    17. Facade Pattern defined
    18. The Principle of Least Knowledge
    19. How NOT to Win Friends and Influence Objects
      1. Keeping your method calls in bounds...
    20. The Facade and the Principle of Least Knowledge
    21. Tools for your Design Toolbox
  15. 8. The Template Method Pattern: Encapsulating Algorithms
    1. It’s time for some more caffeine
    2. Whipping up some coffee and tea classes (in Java)
    3. And now the Tea...
    4. Sir, may I abstract your Coffee, 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
    15. The Hollywood Principle
    16. The Hollywood Principle and Template Method
    17. Template Methods in the Wild
    18. Sorting with Template Method
    19. We’ve got some ducks to sort...
    20. What is compareTo()?
    21. Comparing Ducks and Ducks
    22. Let’s sort some Ducks
    23. The making of the sorting duck machine
    24. Swingin’ with Frames
    25. Applets
    26. Tools for your Design Toolbox
  16. 9. The Iterator and Composite Patterns: Well-Managed Collections
    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. What now?
    6. Can we encapsulate the iteration?
    7. Meet the Iterator Pattern
    8. Adding an Iterator to DinerMenu
    9. Reworking the Diner Menu with Iterator
    10. Fixing up the Waitress code
    11. Testing our code
      1. Here’s the test run...
    12. What have we done so far?
    13. What we have so far...
    14. Making some improvements...
    15. Cleaning things up with java.util.Iterator
    16. We are almost there...
    17. What does this get us?
    18. Iterator Pattern defined
    19. Single Responsibility
    20. Taking a look at the Café Menu
    21. Reworking the Café Menu code
    22. Adding the Café Menu to the Waitress
    23. Breakfast, lunch AND dinner
      1. Here’s the test run; check out the new dinner menu from the Café!
    24. What did we do?
    25. We decoupled the Waitress....
    26. ... and we made the Waitress more extensible
    27. But there’s more!
    28. Iterators and Collections
    29. Is the Waitress ready for prime time?
    30. Just when we thought it was safe...
    31. What do we need?
    32. The Composite Pattern defined
    33. Designing Menus with Composite
    34. Implementing the Menu Component
    35. Implementing the Menu Item
    36. Implementing the Composite Menu
      1. Fixing the print() method
    37. Getting ready for a test drive...
    38. Now for the test drive...
    39. Getting ready for a test drive...
    40. Flashback to Iterator
    41. The Composite Iterator
    42. The Null Iterator
    43. Give me the vegetarian menu
    44. The magic of Iterator & Composite together...
    45. Tools for your Design Toolbox
  17. 10. The State Pattern: The State of Things
    1. Jawva 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. The messy STATE of things...
    8. The new design
    9. Defining the State interfaces and classes
    10. Implementing our State classes
    11. Reworking the Gumball Machine
    12. Now, let’s look at the complete GumballMachine class...
    13. Implementing more states
    14. Let’s take a look at what we’ve done so far...
    15. The State Pattern defined
    16. We still need to finish the Gumball 1 in 10 game
    17. Finishing the game
    18. Demo for the CEO of Mighty Gumball, Inc.
    19. Sanity check...
    20. We almost forgot!
    21. Tools for your Design Toolbox
  18. 11. The Proxy Pattern: Controlling Object Access
    1. Coding the Monitor
    2. Testing the Monitor
    3. The role of the ‘remote proxy’
    4. Adding a remote proxy to the Gumball Machine monitoring code
    5. Remote methods 101
    6. Java RMI, the Big Picture
    7. How does the client get the stub object?
    8. Back to our GumballMachine remote proxy
    9. Getting the GumballMachine ready to be a remote service
    10. Registering with the RMI registry...
    11. Now for the GumballMonitor client...
    12. Writing the Monitor test drive
    13. 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
    14. The Proxy Pattern defined
    15. Get ready for Virtual Proxy
      1. Remote Proxy
      2. Virtual Proxy
    16. Displaying CD covers
    17. Designing the CD cover Virtual Proxy
      1. How ImageProxy is going to work
    18. Writing the Image Proxy
    19. Testing the CD Cover Viewer
      1. Things to try...
    20. What did we do?
    21. Using the Java API’s Proxy to create a protection proxy
    22. Matchmaking in Objectville
    23. The PersonBean implementation
    24. Five-minute drama: protecting subjects
    25. Big Picture: creating a Dynamic Proxy for the PersonBean
    26. Step one: creating Invocation Handlers
    27. Creating Invocation Handlers continued...
    28. Step two: creating the Proxy class and instantiating the Proxy object
    29. Testing the matchmaking service
    30. Running the code...
    31. The Proxy Zoo
    32. Tools for your Design Toolbox
    33. The code for the CD Cover Viewer
  19. 12. Compound Patterns: Patterns of Patterns
    1. Working together
    2. Duck reunion
    3. What did we do?
    4. A duck’s eye view: the class diagram
    5. 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...
    6. Meet the Model-View-Controller
    7. A closer look...
    8. Looking at MVC through patterns-colored glasses
      1. Observer
      2. Strategy
      3. Composite
    9. 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...
    10. Putting the pieces together
    11. Building the pieces
      1. Let’s check out the BeatModelInterface before looking at the implementation
    12. Now let’s have a look at the concrete BeatModel class
    13. The View
    14. Implementing the View
    15. Implementing the View, continued...
    16. Now for the Controller
      1. And here’s the implementation of the controller
    17. Putting it all together...
      1. And now for a test run...
      2. Things to do
    18. Exploring Strategy
    19. Adapting the Model
    20. Now we’re ready for a HeartController
      1. And that’s it! Now it’s time for some test code...
    21. And now for a test run...
      1. Things to do
    22. MVC and the Web
    23. Model 2: DJ’ing from a cell phone
      1. The plan
    24. Step one: the model
    25. Step two: the controller servlet
    26. Now we need a view...
    27. Putting Model 2 to the test...
      1. Things to do
    28. Design Patterns and Model 2
      1. Model 2 is an adaptation of MVC to the Web
    29. Observer
      1. Strategy
      2. Composite
    30. Tools for your Design Toolbox
    31. Exercise Solutions
  20. 13. Better Living with Patterns: Patterns in the Real World
    1. Design Pattern defined
    2. Looking more closely at the Design Pattern definition
    3. So you wanna be a Design Patterns writer
    4. Organizing Design Patterns
    5. Pattern Categories
    6. 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.
    7. Your Mind on Patterns
    8. Don’t forget the power of the shared vocabulary
    9. Cruisin’ Objectville with the Gang of Four
    10. Your journey has just begun...
    11. The Patterns Zoo
    12. Annihilating evil with Anti-Patterns
    13. Tools for your Design Toolbox
    14. Leaving Objectville...
    15. Boy, it’s been great having you in Objectville.
  21. A. Leftover Patterns
    1. Bridge
    2. Why use the Bridge Pattern?
    3. Builder
    4. Why use the Builder Pattern?
    5. Chain of Responsibility
    6. How to use the Chain of Responsibility Pattern
    7. Flyweight
    8. Why use the Flyweight Pattern?
    9. Interpreter
    10. How to implement an interpreter
    11. Mediator
    12. Mediator in action...
    13. Memento
    14. The Memento at work
    15. Prototype
    16. Prototype to the rescue
    17. Visitor
    18. The Visitor drops by
  22. B.  
  23. C. Mighty Gumball
  24. Index
  25. About the Authors
  26. Colophon
  27. Copyright