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