Who is this book for? |
|
xxvi | |
We know what you're thinking |
|
xxvii | |
And we know what your brain is thinking |
|
xxvii | |
We think of a "Head First" reader as a learner |
|
xxviii | |
Metacognition: thinking about thinking |
|
xxix | |
Here's what WE did |
|
xxx | |
Here's what YOU can do to bend your brain into submission |
|
xxxi | |
Read Me |
|
xxxii | |
Tech Reviewers |
|
xxxiv | |
Acknowledgments |
|
xxxv | |
|
|
|
1 Welcome to Design Patterns |
|
|
|
It started with a simple SimUDuck app |
|
|
2 | (1) |
|
But now we need the ducks to FLY |
|
|
3 | (1) |
|
But something went horribly wrong |
|
|
4 | (1) |
|
Joe thinks about inheritance |
|
|
5 | (1) |
|
|
6 | (1) |
|
What would you do if you were Joe? |
|
|
7 | (1) |
|
The one constant in software development |
|
|
8 | (1) |
|
Zeroing in on the problem |
|
|
9 | (1) |
|
Separating what changes from what stays the same |
|
|
10 | (1) |
|
Designing the Duck Behaviors |
|
|
11 | (2) |
|
Implementing the Duck Behaviors |
|
|
13 | (2) |
|
Integrating the Duck Behavior |
|
|
15 | (3) |
|
|
18 | (2) |
|
Setting behavior dynamically |
|
|
20 | (2) |
|
The Big Picture on encapsulated behaviors |
|
|
22 | (1) |
|
HAS-A can be better than IS-A |
|
|
23 | (1) |
|
Speaking of Design Patterns... |
|
|
24 | (2) |
|
Overheard at the local diner... |
|
|
26 | (1) |
|
Overheard in the next cubicle... |
|
|
27 | (1) |
|
The power of a shared pattern vocabulary |
|
|
28 | (1) |
|
How do I use Design Patterns? |
|
|
29 | (3) |
|
Tools for your Design Toolbox |
|
|
32 | (7) |
|
|
|
2 Keeping your Objects in the Know |
|
|
|
The Weather Monitoring application overview |
|
|
39 | (5) |
|
Meet the Observer Pattern |
|
|
44 | (1) |
|
Publishers + Subscribers = Observer Pattern |
|
|
45 | (6) |
|
The Observer Pattern defined |
|
|
51 | (3) |
|
The Power of Loose Coupling |
|
|
54 | (3) |
|
Designing the Weather Station |
|
|
57 | (1) |
|
Implementing the Weather Station |
|
|
58 | (3) |
|
Power up the Weather Station |
|
|
61 | (4) |
|
Looking for the Observer Pattern in the Wild |
|
|
65 | (1) |
|
Coding the life-changing application |
|
|
66 | (3) |
|
Meanwhile, back at Weather-O-Rama |
|
|
69 | (2) |
|
|
71 | (1) |
|
Tools for your Design Toolbox |
|
|
72 | (1) |
|
Design Principle Challenge |
|
|
73 | (7) |
|
|
|
|
|
Welcome to Starbuzz Coffee |
|
|
80 | (6) |
|
The Open-Closed Principle |
|
|
86 | (2) |
|
Meet the Decorator Pattern |
|
|
88 | (1) |
|
Constructing a drink order with Decorators |
|
|
89 | (2) |
|
The Decorator Pattern defined |
|
|
91 | (1) |
|
|
92 | (3) |
|
Writing the Starbuzz code |
|
|
95 | (1) |
|
|
96 | (1) |
|
|
97 | (1) |
|
|
98 | (2) |
|
Real-World Decorators: Java I/O |
|
|
100 | (1) |
|
Decorating the java.io classes |
|
|
101 | (1) |
|
Writing your own Java I/O Decorator |
|
|
102 | (1) |
|
Test out your new Java I/O Decorator |
|
|
103 | (2) |
|
Tools for your Design Toolbox |
|
|
105 | (7) |
|
|
|
4 Baking with OO Goodness |
|
|
|
Identifying the aspects that vary |
|
|
112 | (2) |
|
Encapsulating object creation |
|
|
114 | (1) |
|
Building a simple pizza factory |
|
|
115 | (2) |
|
The Simple Factory defined |
|
|
117 | (3) |
|
A framework for the pizza store |
|
|
120 | (1) |
|
Allowing the subclasses to decide |
|
|
121 | (4) |
|
Declaring a factory method |
|
|
125 | (6) |
|
It's finally time to meet the Factory Method Pattern |
|
|
131 | (1) |
|
View Creators and Products in Parallel |
|
|
132 | (2) |
|
Factory Method Pattern defined |
|
|
134 | (4) |
|
Looking at object dependencies |
|
|
138 | (1) |
|
The Dependency Inversion Principle |
|
|
139 | (1) |
|
|
140 | (5) |
|
|
145 | (1) |
|
Building the ingredient factories |
|
|
146 | (3) |
|
|
149 | (3) |
|
Revisiting our pizza stores |
|
|
152 | (1) |
|
|
153 | (3) |
|
Abstract Factory Pattern defined |
|
|
156 | (4) |
|
Factory Method and Abstract Factory compared |
|
|
160 | (2) |
|
Tools for your Design Toolbox |
|
|
162 | (11) |
|
|
|
|
|
Dissecting the classic Singleton Pattern implementation |
|
|
173 | (2) |
|
|
175 | (2) |
|
Singleton Pattern defined |
|
|
177 | (1) |
|
Houston, we have a problem |
|
|
178 | (2) |
|
Dealing with multithreading |
|
|
180 | (1) |
|
Can we improve multithreading? |
|
|
181 | (2) |
|
Meanwhile, back at the Chocolate Factory |
|
|
183 | (3) |
|
Tools for your Design Toolbox |
|
|
186 | (6) |
|
|
|
6 Encapsulating Invocation |
|
|
|
|
192 | (2) |
|
Taking a look at the vendor classes |
|
|
194 | (3) |
|
A brief introduction to the Command Pattern |
|
|
197 | (4) |
|
From the Diner to the Command Pattern |
|
|
201 | (2) |
|
|
203 | (1) |
|
|
204 | (5) |
|
Assigning Commands to slots |
|
|
209 | (1) |
|
Implementing the Remote Control |
|
|
210 | (1) |
|
Implementing the Commands |
|
|
211 | (1) |
|
Putting the Remote Control through its paces |
|
|
212 | (3) |
|
Time to write that documentation |
|
|
215 | (2) |
|
|
217 | (3) |
|
Time to OA that Undo button! |
|
|
220 | (1) |
|
Using state to implement Undo |
|
|
221 | (1) |
|
Adding Undo to the Ceiling Fan commands |
|
|
222 | (3) |
|
Every remote needs a Party Mode! |
|
|
225 | (1) |
|
|
226 | (3) |
|
More uses of the Command Pattern: queuing requests |
|
|
229 | (1) |
|
More uses of the Command Pattern: logging requests |
|
|
230 | (1) |
|
Command Pattern in the Real World |
|
|
231 | (2) |
|
Tools for your Design Toolbox |
|
|
233 | (5) |
|
The Adapter And Facade Patterns |
|
|
|
|
|
|
238 | (1) |
|
|
239 | (1) |
|
If it walks like a duck and quacks like a duck, then it must might be a duck turkey wrapped with a duck adapter |
|
|
240 | (2) |
|
|
242 | (1) |
|
The Adapter Pattern explained |
|
|
243 | (2) |
|
|
245 | (1) |
|
Object and class adapters |
|
|
246 | (4) |
|
|
250 | (1) |
|
Adapting an Enumeration to an Iterator |
|
|
251 | (6) |
|
|
257 | (1) |
|
Watching a movie (the hard way) |
|
|
258 | (2) |
|
|
260 | (3) |
|
Constructing your home theater facade |
|
|
263 | (1) |
|
Implementing the simplified interface |
|
|
264 | (1) |
|
Time to watch a movie (the easy way) |
|
|
265 | (1) |
|
|
266 | (1) |
|
The Principle of Least Knowledge |
|
|
267 | (1) |
|
How NOT to Win Friends and Influence Objects |
|
|
268 | (3) |
|
The Facade Pattern and the Principle of Least Knowledge |
|
|
271 | (1) |
|
Tools for your Design Toolbox |
|
|
272 | (6) |
|
The Template Method Pattern |
|
|
|
8 Encapsulating Algorithms |
|
|
|
It's time for some more caffeine |
|
|
278 | (1) |
|
Whipping up some coffee and tea classes (in Java) |
|
|
279 | (3) |
|
Let's abstract that Coffee and Tea |
|
|
282 | (1) |
|
Taking the design further |
|
|
283 | (1) |
|
Abstracting prepareRecipe() |
|
|
284 | (3) |
|
|
287 | (1) |
|
|
288 | (2) |
|
What did the Template Method get us? |
|
|
290 | (1) |
|
Template Method Pattern defined |
|
|
291 | (3) |
|
Hooked on Template Method |
|
|
294 | (1) |
|
|
295 | (4) |
|
The Hollywood Principle and Template Method |
|
|
299 | (2) |
|
Template Methods in the Wild |
|
|
301 | (1) |
|
Sorting with Template Method |
|
|
302 | (1) |
|
We've got some ducks to sort |
|
|
303 | (1) |
|
|
303 | (1) |
|
Comparing Ducks and Ducks |
|
|
304 | (1) |
|
|
305 | (1) |
|
The making of the sorting duck machine |
|
|
306 | (2) |
|
|
308 | (1) |
|
Custom Lists with AbstractList |
|
|
309 | (4) |
|
Tools for your Design Toolbox |
|
|
313 | (5) |
|
Tie Iterator And Composite Patterns |
|
|
|
9 Well-Managed Collections |
|
|
|
Breaking News: Objectville Diner and Objectville Pancake House Merge |
|
|
318 | (1) |
|
|
319 | (4) |
|
Implementing the spec: our first attempt |
|
|
323 | (2) |
|
Can we encapsulate the iteration? |
|
|
325 | (2) |
|
Meet the Iterator Pattern |
|
|
327 | (1) |
|
Adding an Iterator to DinerMenu |
|
|
328 | (1) |
|
Reworking the DinerMenu with Iterator |
|
|
329 | (1) |
|
Fixing up the Waitress code |
|
|
330 | (1) |
|
|
331 | (2) |
|
Reviewing our current design... |
|
|
333 | (2) |
|
Cleaning things up with java.util.Iterator |
|
|
335 | (3) |
|
|
338 | (1) |
|
The Iterator Pattern Structure |
|
|
339 | (1) |
|
The Single Responsibility Principle |
|
|
340 | (3) |
|
Meet Java's Iterable interface |
|
|
343 | (1) |
|
|
344 | (3) |
|
Taking a look at the Cafe Menu |
|
|
347 | (6) |
|
Iterators and Collections |
|
|
353 | (2) |
|
Is the Waitress ready for prime time? |
|
|
355 | (5) |
|
The Composite Pattern defined |
|
|
360 | (3) |
|
Designing Menus with Composite |
|
|
363 | (1) |
|
Implementing MenuComponent |
|
|
364 | (1) |
|
Implementing the Menultem |
|
|
365 | (1) |
|
Implementing the Composite Menu |
|
|
366 | (3) |
|
|
369 | (7) |
|
Tools for your Design Toolbox |
|
|
376 | (6) |
|
|
|
|
|
|
382 | (2) |
|
|
384 | (2) |
|
|
386 | (2) |
|
|
388 | (2) |
|
You knew it was coming... a change request! |
|
|
390 | (2) |
|
The messy STATE of things... |
|
|
392 | (2) |
|
|
394 | (1) |
|
Defining the State interfaces and classes |
|
|
395 | (3) |
|
Reworking the Gumball Machine |
|
|
398 | (1) |
|
Now, let's look at the complete GumballMachine class |
|
|
399 | (1) |
|
|
400 | (6) |
|
The State Pattern defined |
|
|
406 | (3) |
|
We still need to finish the Gumball 1 in 10 game |
|
|
409 | (1) |
|
|
410 | (1) |
|
Demo for the CEO of Mighty Gumball, Inc. |
|
|
411 | (2) |
|
|
413 | (3) |
|
|
416 | (3) |
|
Tools for your Design Toolbox |
|
|
419 | (8) |
|
|
|
11 Controlling Object Access |
|
|
|
|
427 | (1) |
|
|
428 | (5) |
|
|
433 | (13) |
|
Getting the GumballMachine ready to be a remote service |
|
|
446 | (2) |
|
Registering with the RMI registry |
|
|
448 | (7) |
|
The Proxy Pattern defined |
|
|
455 | (2) |
|
Get ready for the Virtual Proxy |
|
|
457 | (2) |
|
Designing the Album Cover Virtual Proxy |
|
|
459 | (1) |
|
|
460 | (9) |
|
Using the Java API's Proxy to create a protection proxy |
|
|
469 | (1) |
|
Geeky Matchmaking in Objectville |
|
|
470 | (1) |
|
The Person implementation |
|
|
471 | (2) |
|
Five-minute drama: protecting subjects |
|
|
473 | (1) |
|
Big Picture: creating a Dynamic Proxy for the Person |
|
|
474 | (8) |
|
|
482 | (3) |
|
Tools for your Design Toolbox |
|
|
485 | (4) |
|
The code for the Album Cover Viewer |
|
|
489 | (5) |
|
|
|
|
|
|
494 | (1) |
|
|
495 | (22) |
|
|
517 | (1) |
|
A bird's duck's-eye view: the class diagram |
|
|
518 | (2) |
|
The king of Compound Patterns |
|
|
520 | (3) |
|
Meet Model-View-Controller |
|
|
523 | (1) |
|
|
524 | (2) |
|
Understanding MVC as a set of Patterns |
|
|
526 | (2) |
|
Using MVC to control the beat |
|
|
528 | (3) |
|
|
531 | (1) |
|
Now let's have a look at the concrete BeatModel class |
|
|
532 | (1) |
|
|
533 | (1) |
|
|
534 | (2) |
|
|
536 | (2) |
|
|
538 | (1) |
|
|
539 | (1) |
|
|
540 | (2) |
|
|
542 | (3) |
|
Tools for your Design Toolbox |
|
|
545 | (20) |
|
Better Living With Patterns |
|
|
|
13 Patterns in the Real World |
|
|
|
|
565 | (2) |
|
Looking more closely at the Design Pattern definition |
|
|
567 | (1) |
|
May the force be with you |
|
|
568 | (5) |
|
So you wanna be a Design Patterns writer |
|
|
573 | (2) |
|
Organizing Design Patterns |
|
|
575 | (5) |
|
|
580 | (3) |
|
|
583 | (2) |
|
Don't forget the power of the shared vocabulary |
|
|
585 | (2) |
|
Cruisin' Objectville with the Gang of Four |
|
|
587 | (1) |
|
Your journey has just begun... |
|
|
588 | (2) |
|
|
590 | (2) |
|
Annihilating evil with Anti-Patterns |
|
|
592 | (2) |
|
Tools for your Design Toolbox |
|
|
594 | (1) |
|
|
595 | (3) |
|
14 Appendix: Leftover Patterns |
|
|
|
|
598 | (2) |
|
|
600 | (2) |
|
|
602 | (2) |
|
|
604 | (2) |
|
|
606 | (2) |
|
|
608 | (2) |
|
|
610 | (2) |
|
|
612 | (2) |
|
|
614 | |