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.
Author(s): Eric Freeman, Elisabeth Robson
Edition: 2
Publisher: O'Reilly Media
Year: 2020
Language: English
Commentary: Vector PDF
Pages: 672
City: Sebastopol, CA
Tags: Java; Design Patterns; Best Practices; Object-Oriented Programming; Programming Style; Elementary
Authors of Head First Design Patterns
Creators of the Head First Series
Table of Contents (summary)
Table of Contents (the real thing)
Intro
Who is this book for?
We know what you’re 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
Tech Reviewers, 2nd Edition
Acknowledgments
Very Special Thanks
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
Designing the Duck Behaviors
Implementing the Duck Behaviors
Integrating the Duck Behaviors
More integration...
Testing the Duck code
Setting behavior dynamically
The Big Picture on encapsulated behavior
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?
Skeptical Developer
Friendly Patterns Guru
Tools for your Design Toolbox
2: the Observer Pattern: Keeping your Objects in the Know
The Weather Monitoring application overview
Unpacking the WeatherData class
Our 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
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
Coding the life-changing application
Meanwhile, back at Weather-O-Rama
Test Drive the new code
Tools for your Design Toolbox
Design Principle Challenge
3: the Decorator Pattern: Decorating Objects
Welcome to Starbuzz Coffee
The Open-Closed Principle
Meet the Decorator Pattern
Constructing a drink order with Decorators
Now let’s see how this all really works
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
Encapsulating object creation
Building a simple pizza factory
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
So how do they order?
Let’s check out how these pizzas are really made to order
We're just missing one thing: Pizzas!
You’ve waited long enough. Time for some pizzas!
It’s finally time to meet the Factory Method Pattern
View Creators and Products in Parallel
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...
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
5 the Singleton Pattern: One-of-a-Kind Objects
The Little Singleton
Dissecting the classic Singleton Pattern
The Chocolate Factory
Singleton Pattern defined
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
Assigning Commands to slots
Implementing the Remote Control
Implementing the Commands
Putting the Remote Control through its paces
Time to write that documentation...
The updated code, using lambda expressiobs
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
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
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...
Test drive the adapter
The Adapter Pattern explained
Here’s how the Client uses the Adapter
Adapter Pattern defined
Object and class adapters
Real-world adapters
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
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...
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
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
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?
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...
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
What did we do?
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...
Getting ready for a test drive...
Tools for your Design Toolbox
Code Magnets Solution
10: the State Pattern: The State of Things
Java 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 Mac
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 three: run rmiregistry
Step four: start the service
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
The Proxy Pattern defined
Get ready for the Virtual Proxy
Displaying Album covers
Designing the Album Cover Virtual Proxy
Writing the Image Proxy
Testing the Album Cover Viewer
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
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 Album Cover Viewer
12: compound patterns: Patterns of Patterns
Working together
Duck reunion
Safety versus transparency
What did we do?
A duck’s-eye view: the class diagram
The King of Compound Patterns
Meet Model-View-Controller
A closer look...
Understanding MVC as a set of Patterns
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
Now for the Controller
And here’s the implementation of the controller
Putting it all together...
Exploring Strategy
Adapting the Model
And now for a test run...
Tools for your Design Toolbox
13: better living with patterns: Patterns in the Real World
Design Pattern defined
Looking more closely at the Design Pattern definition
May the force be with you
So you wanna be a Design Patterns writer
Organizing Design Patterns
Thinking in Patterns
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...
14: appendix: Leftover Patterns
Bridge
Builder
Chain of Responsibility
Flyweight
Interpreter
Mediator
Memento
Prototype
Visitor
Index