Book description
What’s so special about design patterns?
At any given moment, someone struggles with the same software design problems you have. And, chances are, someone else has already solved your problem. 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.
What’s so special about this book?
We think your time is too valuable to spend struggling with new concepts. Using the latest research in cognitive science and learning theory to craft a multi-sensory learning experience, Head First Design Patterns uses a visually rich format designed for the way your brain works, not a text-heavy approach that puts you to sleep.
Publisher resources
Table of contents
- Dedication
- 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
-
1. Intro to Design Patterns: Welcome 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
- Integrating the Duck Behavior
- 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...
- 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
-
2. The Observer Pattern: Keeping your Objects in the know
- The Weather Monitoring application overview
- Unpacking the WeatherData class
- What do we know so far?
- Taking a first, misguided SWAG at the Weather Station
- What’s wrong with our implementation?
- 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 defined: the class diagram
- 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
- Using Java’s built-in Observer Pattern
- How Java’s built-in Observer Pattern works
- Reworking the Weather Station with the built-in support
- Running the new code
- The dark side of java.util.Observable
- Other places you’ll find the Observer Pattern in the JDK
- And the code...
- Tools for your Design Toolbox
-
3. The Decorator Pattern: Decorating Objects
- Welcome to Starbuzz Coffee
- The Open-Closed Principle
- Meet the Decorator Pattern
- Constructing a drink order with Decorators
- 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
-
4. The Factory Pattern: Baking with OO Goodness
- Identifying the aspects that vary
- But the pressure is on to add more pizza types
- Encapsulating object creation
- Building a simple pizza factory
- Reworking the PizzaStore class
- The Simple Factory defined
- Franchising the pizza store
- A framework for the pizza store
- Allowing the subclasses to decide
- Let’s make a PizzaStore
- Declaring a factory method
- We’re just missing one thing: PIZZA!
- You’ve waited long enough. Time for some pizzas!
- It’s finally time to meet the Factory Method Pattern
- Another perspective: parallel class hierarchies
- Factory Method Pattern defined
- A very dependent PizzaStore
- 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 PizzaStore...
- 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...
- Abstract Factory Pattern defined
- Factory Method and Abstract Factory compared
- Tools for your Design Toolbox
- A very dependent PizzaStore
-
5. The Singleton Pattern: One of a Kind Objects
- The Little Singleton
- Dissecting the classic Singleton Pattern implementation
- The Chocolate Factory
- Singleton Pattern defined
- Houston, Hershey, PA we have a problem...
- Dealing with multithreading
- Can we improve multithreading?
- Meanwhile, back at the Chocolate Factory...
- Congratulations!
- Tools for your Design Toolbox
-
6. The Command Pattern: Encapsulating Invocation
- 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
- Time to write that documentation...
- What are we doing?
- Time to QA that Undo button!
- Using state to implement Undo
- Adding Undo to the CeilingFan commands
- Get ready to test the ceiling fan
- Testing the ceiling fan...
- Every remote needs a Party Mode!
- Using a macro command
- The Command Pattern means lots of command classes
- Simplifying the Remote Control with lambda expressions
- Simplifying even more with method references
- Test the remote control with lambda expressions
- More uses of the Command Pattern: queuing requests
- More uses of the Command Pattern: logging requests
- Tools for your Design Toolbox
-
7. The Adapter and Facade Patterns: Being Adaptive
- 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
- Adapter Pattern defined
- Object and class adapters
- Real-world adapters
- Adapting an Enumeration to an Iterator
- 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
- The Facade and the Principle of Least Knowledge
- Tools for your Design Toolbox
-
8. The Template Method Pattern: Encapsulating Algorithms
- It’s time for some more caffeine
- Whipping up some coffee and tea classes (in Java)
- And now the Tea...
- Sir, may I abstract your Coffee, 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
- 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
- The making of the sorting duck machine
- Swingin’ with Frames
- Applets
- Tools for your Design Toolbox
-
9. The Iterator and Composite Patterns: Well-Managed Collections
- 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?
- What now?
- Can we encapsulate the iteration?
- Meet the Iterator Pattern
- Adding an Iterator to DinerMenu
- Reworking the Diner Menu with Iterator
- Fixing up the Waitress code
- Testing our code
- What have we done so far?
- What we have so far...
- Making some improvements...
- Cleaning things up with java.util.Iterator
- We are almost there...
- What does this get us?
- Iterator Pattern defined
- Single Responsibility
- Taking a look at the Café Menu
- Reworking the Café Menu code
- Adding the Café Menu to the Waitress
- Breakfast, lunch AND dinner
- What did we do?
- We decoupled the Waitress....
- ... and we made the Waitress more extensible
- But there’s more!
- Iterators and Collections
- 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 the Menu Component
- Implementing the Menu Item
- Implementing the Composite Menu
- Getting ready for a test drive...
- Now for the test drive...
- Getting ready for a test drive...
- Flashback to Iterator
- The Composite Iterator
- The Null Iterator
- Give me the vegetarian menu
- The magic of Iterator & Composite together...
- Tools for your Design Toolbox
-
10. The State Pattern: The State of Things
- Jawva Breakers
- Cubicle Conversation
- State machines 101
- Writing the code
- In-house testing
- You knew it was coming... a change request!
- 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
- 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
-
11. The Proxy Pattern: Controlling Object Access
- 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
- Java RMI, the Big Picture
- How does the client get the stub object?
- 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...
- The Proxy Pattern defined
- Get ready for Virtual Proxy
- Displaying CD covers
- Designing the CD cover Virtual Proxy
- Writing the Image Proxy
- Testing the CD Cover Viewer
- What did we do?
- Using the Java API’s Proxy to create a protection proxy
- Matchmaking in Objectville
- The PersonBean implementation
- Five-minute drama: protecting subjects
- Big Picture: creating a Dynamic Proxy for the PersonBean
- 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
- Tools for your Design Toolbox
- The code for the CD Cover Viewer
-
12. Compound Patterns: Patterns of Patterns
- Working together
- Duck reunion
- What did we do?
- A duck’s eye view: the class diagram
- The King of Compound Patterns
- Meet the Model-View-Controller
- A closer look...
- Looking at MVC through patterns-colored glasses
- Using MVC to control the beat...
- Putting the pieces together
- Building the pieces
- Now let’s have a look at the concrete BeatModel class
- The View
- Implementing the View
- Implementing the View, continued...
- Now for the Controller
- Putting it all together...
- Exploring Strategy
- Adapting the Model
- Now we’re ready for a HeartController
- And now for a test run...
- MVC and the Web
- Model 2: DJ’ing from a cell phone
- Step one: the model
- Step two: the controller servlet
- Now we need a view...
- Putting Model 2 to the test...
- Design Patterns and Model 2
- Observer
- Tools for your Design Toolbox
- Exercise Solutions
-
13. Better Living with Patterns: Patterns in the Real World
- Design Pattern defined
- Looking more closely at the Design Pattern definition
- 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 Patterns Zoo
- Annihilating evil with Anti-Patterns
- Tools for your Design Toolbox
- Leaving Objectville...
- Boy, it’s been great having you in Objectville.
-
A. 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
- B.
- C. Mighty Gumball
- Index
- About the Authors
- Colophon
- Copyright
Product information
- Title: Head First Design Patterns
- Author(s):
- Release date: October 2004
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9780596007126
You might also like
book
Head First Design Patterns, 2nd Edition
What will you learn from this book? You know you don't want to reinvent the wheel, …
video
Design Patterns in the Real World, an Analysis-Based Approach
Alan Holub takes coders deep into the reality of Gang-of-Four design patterns, those reusable guides to …
book
Design Patterns: Elements of Reusable Object-Oriented Software
Capturing a wealth of experience about the design of object-oriented software, four top-notch designers present a …
book
Learning Domain-Driven Design
Building software is harder than ever. As a developer, you not only have to chase ever-changing …