Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Head First: Design Patterns
Dedication
A Note Regarding Supplemental Files
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?
Who should probably back away from this book?
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 people
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...
The updated code, using lambda expressions
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
Okay, here’s what we know so far...
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
Give it a spin
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
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 PizzaStore
Declaring a factory method
Let’s see how it works: ordering pizzas with the pizza factory method
So how do they order?
Let’s check out how these pizzas are really made to order...
We’re just missing one thing: PIZZA!
Our PizzaStore isn’t going to be very popular without some pizzas, so let’s implement them
Now we just need some concrete subclasses... how about defining New York and Chicago style cheese pizzas?
You’ve waited long enough. Time for some pizzas!
It’s finally time to meet the Factory Method Pattern
The Creator classes
The Product classes
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...
Ensuring consistency in your ingredients
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...
From here things change, because we are using an ingredient factory
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
A small Socratic exercise in the style of The Little Lisper
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?
1. Do nothing if the performance of getInstance() isn’t critical to your application.
2. Move to an eagerly created instance rather than a lazily created one.
3. Use “double-checked locking” to reduce the use of synchronization in getInstance().
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
Now, let’s check out the execution of our remote control test...
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
Do we really need all these command classes?
Simplifying the Remote Control with lambda expressions
Simplifying even more with method references
What if we need to do more than one thing in our lambda expression?
Test the remote control with lambda expressions
Check out the results of all those lambda expression commands...
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
Here’s how the Client uses the Adapter
Adapter Pattern defined
Object and class adapters
Real-world adapters
Old-world Enumerators
New-world Iterators
And today...
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 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?
The Java-Enabled Waitress Specification
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
Here’s the test run...
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
Here’s the test run; check out the new dinner menu from the Café!
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
Fixing the print() method
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...
And now let’s put the monitor in the hands of the CEO. Hopefully, this time he’ll love it
The Proxy Pattern defined
Get ready for Virtual Proxy
Remote Proxy
Virtual Proxy
Displaying CD covers
Designing the CD cover Virtual Proxy
How ImageProxy is going to work
Writing the Image Proxy
Testing the CD Cover Viewer
Things to try...
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
If Elvis were a compound pattern, his name would be Model-View-Controller, and he’d be singing a little song like this...
Meet the Model-View-Controller
A closer look...
Looking at MVC through patterns-colored glasses
Observer
Strategy
Composite
Using MVC to control the beat...
Meet the Java DJ View
The controller is in the middle...
Let’s not forget about the model underneath it all...
Putting the pieces together
Building the pieces
Let’s check out the BeatModelInterface before looking at the implementation
Now let’s have a look at the concrete BeatModel class
The View
Implementing the View
Implementing the View, continued...
Now for the Controller
And here’s the implementation of the controller
Putting it all together...
And now for a test run...
Things to do
Exploring Strategy
Adapting the Model
Now we’re ready for a HeartController
And that’s it! Now it’s time for some test code...
And now for a test run...
Things to do
MVC and the Web
Model 2: DJ’ing from a cell phone
The plan
Step one: the model
Step two: the controller servlet
Now we need a view...
Putting Model 2 to the test...
Things to do
Design Patterns and Model 2
Model 2 is an adaptation of MVC to the Web
Observer
Strategy
Composite
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
← Prev
Back
Next →
← Prev
Back
Next →