Eric Freeman recently ended nearly a decade as a media company executive, having held the position of CTO of Disney Online & Disney.com at The Walt Disney Company. Eric is now devoting his time to WickedlySmart.com and lives with his wife and young daughter in Austin, TX. He holds a Ph.D. in Computer Science from Yale University.
Elisabeth Robson is co-founder of Wickedly Smart, an education company devoted to helping customers gain mastery in web technologies. She's co-author of four bestselling books, Head First Design Patterns, Head First HTML and CSS, Head First HTML5 Programming, and Head First JavaScript Programming.
Bert Bates is a 20-year software developer, a Java instructor, and a co-developer of Sun's upcoming EJB exam (Sun Certified Business Component Developer). His background features a long stint in artificial intelligence, with clients like the Weather Channel, A&E Network, Rockwell, and Timken.
Kathy Sierra has been interested in learning theory since her days as a game developer (Virgin, MGM, Amblin'). More recently, she's been a master trainer for Sun Microsystems, teaching Sun's Java instructors how to teach the latest technologies to customers, and a lead developer of several Sun certification exams. Along with her partner Bert Bates, Kathy created the Head First series. She's also the original founder of the Software Development/Jolt Productivity Award-winning javaranch.com, the largest (and friendliest) all-volunteer Java community.
; Praise for Head First Design Patterns; More Praise for Head First Design Patterns; Praise for other books by Eric Freeman and Elisabeth Robson; Authors of Head First Design Patterns; Creators of the Head First series (and co-conspirators on this book); How to Use This Book: Intro; Who is this book for?; 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; Tech Reviewers; Acknowledgments; Even more peopleThe large number of acknowledgments is because we’re testing the theory that everyone mentioned in a book acknowledgment will buy at least one copy, probably more, what with relatives and everything. If you’d like to be in the acknowledgment of our next book, and you have a large family, write to us.; Chapter 1: Intro to Design Patterns: Welcome to Design Patterns; 1.1 It started with a simple SimUDuck app; 1.2 But now we need the ducks to FLY; 1.3 But something went horribly wrong...; 1.4 Joe thinks about inheritance...; 1.5 How about an interface?; 1.6 What would you do if you were Joe?; 1.7 The one constant in software development; 1.8 Zeroing in on the problem...; 1.9 Separating what changes from what stays the same; 1.10 Designing the Duck Behaviors; 1.11 Implementing the Duck Behaviors; 1.12 Integrating the Duck Behavior; 1.13 More integration...; 1.14 Testing the Duck code; 1.15 Setting behavior dynamically; 1.16 The Big Picture on encapsulated behaviors; 1.17 HAS-A can be better than IS-A; 1.18 Speaking of Design Patterns...; 1.19 Overheard at the local diner...; 1.20 Overheard in the next cubicle...; 1.21 The power of a shared pattern vocabulary; 1.22 How do I use Design Patterns?; 1.23 Tools for your Design Toolbox; Chapter 2: The Observer Pattern: Keeping your Objects in the know; 2.1 The Weather Monitoring application overview; 2.2 Unpacking the WeatherData class; 2.3 What do we know so far?; 2.4 Taking a first, misguided SWAG at the Weather Station; 2.5 What’s wrong with our implementation?; 2.6 Meet the Observer Pattern; 2.7 Publishers + Subscribers = Observer Pattern; 2.8 A day in the life of the Observer Pattern; 2.9 Five-minute drama: a subject for observation; 2.10 Two weeks later...; 2.11 The Observer Pattern defined; 2.12 The Observer Pattern defined: the class diagram; 2.13 The power of Loose Coupling; 2.14 Cubicle conversation; 2.15 Designing the Weather Station; 2.16 Implementing the Weather Station; 2.17 Implementing the Subject interface in WeatherData; 2.18 Now, let’s build those display elements; 2.19 Power up the Weather Station; 2.20 Using Java’s built-in Observer Pattern; 2.21 How Java’s built-in Observer Pattern works; 2.22 Reworking the Weather Station with the built-in support; 2.23 Running the new code; 2.24 The dark side of java.util.Observable; 2.25 Other places you’ll find the Observer Pattern in the JDK; 2.26 And the code...; 2.27 Tools for your Design Toolbox; Chapter 3: The Decorator Pattern: Decorating Objects; 3.1 Welcome to Starbuzz Coffee; 3.2 The Open-Closed Principle; 3.3 Meet the Decorator Pattern; 3.4 Constructing a drink order with Decorators; 3.5 The Decorator Pattern defined; 3.6 Decorating our Beverages; 3.7 Cubicle Conversation; 3.8 New barista training; 3.9 Writing the Starbuzz code; 3.10 Coding beverages; 3.11 Coding condiments; 3.12 Serving some coffees; 3.13 Real World Decorators: Java I/O; 3.14 Decorating the java.io classes; 3.15 Writing your own Java I/O Decorator; 3.16 Test out your new Java I/O Decorator; 3.17 Tools for your Design Toolbox; Chapter 4: The Factory Pattern: Baking with OO Goodness; 4.1 Identifying the aspects that vary; 4.2 But the pressure is on to add more pizza types; 4.3 Encapsulating object creation; 4.4 Building a simple pizza factory; 4.5 Reworking the PizzaStore class; 4.6 The Simple Factory defined; 4.7 Franchising the pizza store; 4.8 A framework for the pizza store; 4.9 Allowing the subclasses to decide; 4.10 Let’s make a PizzaStore; 4.11 Declaring a factory method; 4.12 We’re just missing one thing: PIZZA!; 4.13 You’ve waited long enough. Time for some pizzas!; 4.14 It’s finally time to meet the Factory Method Pattern; 4.15 Another perspective: parallel class hierarchies; 4.16 Factory Method Pattern defined; 4.17 A very dependent PizzaStore; 4.18 Looking at object dependencies; 4.19 The Dependency Inversion Principle; 4.20 Applying the Principle; 4.21 Inverting your thinking...; 4.22 A few guidelines to help you follow the Principle...; 4.23 Meanwhile, back at the PizzaStore...; 4.24 Families of ingredients...; 4.25 Building the ingredient factories; 4.26 Building the New York ingredient factory; 4.27 Reworking the pizzas...; 4.28 Reworking the pizzas, continued...; 4.29 Revisiting our pizza stores; 4.30 What have we done?; 4.31 More pizza for Ethan and Joel...; 4.32 Abstract Factory Pattern defined; 4.33 Factory Method and Abstract Factory compared; 4.34 Tools for your Design Toolbox; 4.35 A very dependent PizzaStore; Chapter 5: The Singleton Pattern: One of a Kind Objects; 5.1 The Little Singleton; 5.2 Dissecting the classic Singleton Pattern implementation; 5.3 The Chocolate Factory; 5.4 Singleton Pattern defined; 5.5 Houston, Hershey, PA we have a problem...; 5.6 Dealing with multithreading; 5.7 Can we improve multithreading?; 5.8 Meanwhile, back at the Chocolate Factory...; 5.9 Congratulations!; 5.10 Tools for your Design Toolbox; Chapter 6: The Command Pattern: Encapsulating Invocation; 6.1 Free hardware! Let’s check out the Remote Control...; 6.2 Taking a look at the vendor classes; 6.3 Cubicle Conversation; 6.4 Meanwhile, back at the Diner..., or, A brief introduction to the Command Pattern; 6.5 Let’s study the interaction in a little more detail...; 6.6 The Objectville Diner roles and responsibilities; 6.7 From the Diner to the Command Pattern; 6.8 Our first command object; 6.9 Using the command object; 6.10 Creating a simple test to use the Remote Control; 6.11 The Command Pattern defined; 6.12 The Command Pattern defined: the class diagram; 6.13 Assigning Commands to slots; 6.14 Implementing the Remote Control; 6.15 Implementing the Commands; 6.16 Putting the Remote Control through its paces; 6.17 Time to write that documentation...; 6.18 What are we doing?; 6.19 Time to QA that Undo button!; 6.20 Using state to implement Undo; 6.21 Adding Undo to the CeilingFan commands; 6.22 Get ready to test the ceiling fan; 6.23 Testing the ceiling fan...; 6.24 Every remote needs a Party Mode!; 6.25 Using a macro command; 6.26 The Command Pattern means lots of command classes; 6.27 Simplifying the Remote Control with lambda expressions; 6.28 Simplifying even more with method references; 6.29 Test the remote control with lambda expressions; 6.30 More uses of the Command Pattern: queuing requests; 6.31 More uses of the Command Pattern: logging requests; 6.32 Tools for your Design Toolbox; Chapter 7: The Adapter and Facade Patterns: Being Adaptive; 7.1 Adapters all around us; 7.2 Object-oriented adapters; 7.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...; 7.4 Test drive the adapter; 7.5 The Adapter Pattern explained; 7.6 Adapter Pattern defined; 7.7 Object and class adapters; 7.8 Real-world adapters; 7.9 Adapting an Enumeration to an Iterator; 7.10 And now for something different...; 7.11 Home Sweet Home Theater; 7.12 Watching a movie (the hard way); 7.13 Lights, Camera, Facade!; 7.14 Constructing your home theater facade; 7.15 Implementing the simplified interface; 7.16 Time to watch a movie (the easy way); 7.17 Facade Pattern defined; 7.18 The Principle of Least Knowledge; 7.19 How NOT to Win Friends and Influence Objects; 7.20 The Facade and the Principle of Least Knowledge; 7.21 Tools for your Design Toolbox; Chapter 8: The Template Method Pattern: Encapsulating Algorithms; 8.1 It’s time for some more caffeine; 8.2 Whipping up some coffee and tea classes (in Java); 8.3 And now the Tea...; 8.4 Sir, may I abstract your Coffee, Tea?; 8.5 Taking the design further...; 8.6 Abstracting prepareRecipe(); 8.7 What have we done?; 8.8 Meet the Template Method; 8.9 Let’s make some tea...; 8.10 What did the Template Method get us?; 8.11 Template Method Pattern defined; 8.12 Hooked on Template Method...; 8.13 Using the hook; 8.14 Let’s run the Test Drive; 8.15 The Hollywood Principle; 8.16 The Hollywood Principle and Template Method; 8.17 Template Methods in the Wild; 8.18 Sorting with Template Method; 8.19 We’ve got some ducks to sort...; 8.20 What is compareTo()?; 8.21 Comparing Ducks and Ducks; 8.22 Let’s sort some Ducks; 8.23 The making of the sorting duck machine; 8.24 Swingin’ with Frames; 8.25 Applets; 8.26 Tools for your Design Toolbox; Chapter 9: The Iterator and Composite Patterns: Well-Managed Collections; 9.1 Breaking News: Objectville Diner and Objectville Pancake House Merge; 9.2 Check out the Menu Items; 9.3 Lou and Mel’s Menu implementations; 9.4 What’s the problem with having two different menu representations?; 9.5 What now?; 9.6 Can we encapsulate the iteration?; 9.7 Meet the Iterator Pattern; 9.8 Adding an Iterator to DinerMenu; 9.9 Reworking the Diner Menu with Iterator; 9.10 Fixing up the Waitress code; 9.11 Testing our code; 9.12 What have we done so far?; 9.13 What we have so far...; 9.14 Making some improvements...; 9.15 Cleaning things up with java.util.Iterator; 9.16 We are almost there...; 9.17 What does this get us?; 9.18 Iterator Pattern defined; 9.19 Single Responsibility; 9.20 Taking a look at the Café Menu; 9.21 Reworking the Café Menu code; 9.22 Adding the Café Menu to the Waitress; 9.23 Breakfast, lunch AND dinner; 9.24 What did we do?; 9.25 We decoupled the Waitress....; 9.26 ... and we made the Waitress more extensible; 9.27 But there’s more!; 9.28 Iterators and Collections; 9.29 Is the Waitress ready for prime time?; 9.30 Just when we thought it was safe...; 9.31 What do we need?; 9.32 The Composite Pattern defined; 9.33 Designing Menus with Composite; 9.34 Implementing the Menu Component; 9.35 Implementing the Menu Item; 9.36 Implementing the Composite Menu; 9.37 Getting ready for a test drive...; 9.38 Now for the test drive...; 9.39 Getting ready for a test drive...; 9.40 Flashback to Iterator; 9.41 The Composite Iterator; 9.42 The Null Iterator; 9.43 Give me the vegetarian menu; 9.44 The magic of Iterator & Composite together...; 9.45 Tools for your Design Toolbox; Chapter 10: The State Pattern: The State of Things; 10.1 Jawva Breakers; 10.2 Cubicle Conversation; 10.3 State machines 101; 10.4 Writing the code; 10.5 In-house testing; 10.6 You knew it was coming... a change request!; 10.7 The messy STATE of things...; 10.8 The new design; 10.9 Defining the State interfaces and classes; 10.10 Implementing our State classes; 10.11 Reworking the Gumball Machine; 10.12 Now, let’s look at the complete GumballMachine class...; 10.13 Implementing more states; 10.14 Let’s take a look at what we’ve done so far...; 10.15 The State Pattern defined; 10.16 We still need to finish the Gumball 1 in 10 game; 10.17 Finishing the game; 10.18 Demo for the CEO of Mighty Gumball, Inc.; 10.19 Sanity check...; 10.20 We almost forgot!; 10.21 Tools for your Design Toolbox; Chapter 11: The Proxy Pattern: Controlling Object Access; 11.1 Coding the Monitor; 11.2 Testing the Monitor; 11.3 The role of the ‘remote proxy’; 11.4 Adding a remote proxy to the Gumball Machine monitoring code; 11.5 Remote methods 101; 11.6 Java RMI, the Big Picture; 11.7 How does the client get the stub object?; 11.8 Back to our GumballMachine remote proxy; 11.9 Getting the GumballMachine ready to be a remote service; 11.10 Registering with the RMI registry...; 11.11 Now for the GumballMonitor client...; 11.12 Writing the Monitor test drive; 11.13 Another demo for the CEO of Mighty Gumball...; 11.14 The Proxy Pattern defined; 11.15 Get ready for Virtual Proxy; 11.16 Displaying CD covers; 11.17 Designing the CD cover Virtual Proxy; 11.18 Writing the Image Proxy; 11.19 Testing the CD Cover Viewer; 11.20 What did we do?; 11.21 Using the Java API’s Proxy to create a protection proxy; 11.22 Matchmaking in Objectville; 11.23 The PersonBean implementation; 11.24 Five-minute drama: protecting subjects; 11.25 Big Picture: creating a Dynamic Proxy for the PersonBean; 11.26 Step one: creating Invocation Handlers; 11.27 Creating Invocation Handlers continued...; 11.28 Step two: creating the Proxy class and instantiating the Proxy object; 11.29 Testing the matchmaking service; 11.30 Running the code...; 11.31 The Proxy Zoo; 11.32 Tools for your Design Toolbox; 11.33 The code for the CD Cover Viewer; Chapter 12: Compound Patterns: Patterns of Patterns; 12.1 Working together; 12.2 Duck reunion; 12.3 What did we do?; 12.4 A duck’s eye view: the class diagram; 12.5 The King of Compound Patterns; 12.6 Meet the Model-View-Controller; 12.7 A closer look...; 12.8 Looking at MVC through patterns-colored glasses; 12.9 Using MVC to control the beat...; 12.10 Putting the pieces together; 12.11 Building the pieces; 12.12 Now let’s have a look at the concrete BeatModel class; 12.13 The View; 12.14 Implementing the View; 12.15 Implementing the View, continued...; 12.16 Now for the Controller; 12.17 Putting it all together...; 12.18 Exploring Strategy; 12.19 Adapting the Model; 12.20 Now we’re ready for a HeartController; 12.21 And now for a test run...; 12.22 MVC and the Web; 12.23 Model 2: DJ’ing from a cell phone; 12.24 Step one: the model; 12.25 Step two: the controller servlet; 12.26 Now we need a view...; 12.27 Putting Model 2 to the test...; 12.28 Design Patterns and Model 2; 12.29 Observer; 12.30 Tools for your Design Toolbox; 12.31 Exercise Solutions; Chapter 13: Better Living with Patterns: Patterns in the Real World; 13.1 Design Pattern defined; 13.2 Looking more closely at the Design Pattern definition; 13.3 So you wanna be a Design Patterns writer; 13.4 Organizing Design Patterns; 13.5 Pattern Categories; 13.6 Thinking in Patterns; 13.7 Your Mind on Patterns; 13.8 Don’t forget the power of the shared vocabulary; 13.9 Cruisin’ Objectville with the Gang of Four; 13.10 Your journey has just begun...; 13.11 The Patterns Zoo; 13.12 Annihilating evil with Anti-Patterns; 13.13 Tools for your Design Toolbox; 13.14 Leaving Objectville...; 13.15 Boy, it’s been great having you in Objectville.; Leftover Patterns; Bridge; Why use the Bridge Pattern?; Builder; Why use the Builder Pattern?; Chain of Responsibility; How to use the Chain of Responsibility Pattern; Flyweight; Why use the Flyweight Pattern?; Interpreter; How to implement an interpreter; Mediator; Mediator in action...; Memento; The Memento at work; Prototype; Prototype to the rescue; Visitor; The Visitor drops by; ; Mighty Gumball; Colophon;