Head First design patterns: Includes index 9780596007126, 0596007124

You're not alone. At any given moment, somewhere in the world someone struggles with the same software design prob

670 121 53MB

English Pages xxxvi, 638 Seiten Seiten [694] Year 2004

Report DMCA / Copyright

DOWNLOAD FILE

Head First design patterns: Includes index
 9780596007126, 0596007124

Table of contents :
Intro: How to use this book......Page 27
Chapter 1: Intro to design patterns......Page 39
It started with a simple SimUDuck app......Page 40
But now we need the ducks to FLY......Page 41
Designing the Duck Behaviors......Page 49
Implementing the Duck Behaviors......Page 51
Integrating the Duck Behavior......Page 53
More integration......Page 54
Testing the Duck code......Page 56
Setting the behavior dynamically......Page 58
The Big Picture on encapsulated behaviors......Page 60
HAS-A can be better than IS-A......Page 61
Speaking of Design Patterns......Page 62
The power of a shared vocabulary......Page 66
How do I use Design Patterns?......Page 67
Tools for your Design Toolbox......Page 70
Design Patterns Crossword......Page 71
Design Puzzle Solution......Page 72
Design Patterns Crossword Solution......Page 74
Chapter 2: The Observer Pattern......Page 75
The Weather Monitoring application overview......Page 77
Unpacking the WeatherData class......Page 78
What do we know so far?......Page 79
Taking a first, misguided SWAG at the Weather Station......Page 80
What's wrong with our implementation?......Page 81
Meet the Observer Pattern......Page 82
Publishers + Subscribers = Observer Pattern......Page 83
A day in the life of the Observer Pattern......Page 84
Five-minute drama: a subject for observation......Page 86
The Observer Pattern defined......Page 89
The Observer Pattern defined: the class diagram......Page 90
The power of Loose Coupling......Page 91
Designing the Weather Station......Page 94
Implementing the Weather Station......Page 95
Implementing the Subject interface in Weather Data......Page 96
Now, let's build those display elements......Page 97
Power up the Weather Station......Page 98
Using Java's built-in Observer Pattern......Page 102
How Java's built-in Observer Pattern works......Page 103
Reworking the Weather Station with the built-in support......Page 105
Tools for your Design Toolbox......Page 113
Design Patterns Crossword......Page 115
Design Patterns Crossword Solution......Page 118
Chapter 3: The Decorator Pattern......Page 119
The Open-Closed Principle......Page 126
Meet the Decorator Pattern......Page 128
Constructing a drink order with Decorators......Page 129
The Decorator Pattern defined......Page 131
Decorating our Beverages......Page 132
New barista training......Page 134
Writing the Starbuzz code......Page 135
Coding beverages......Page 136
Serving some coffees......Page 138
Real World Decorators: Java I/O......Page 140
Decorating the java.io classes......Page 141
Writing your own Java I/O Decorator......Page 142
Test out your new Java I/O Decorator......Page 143
Tools for your Design Toolbox......Page 145
Chapter 4: The Factory Pattern......Page 149
Identifying the aspects that vary......Page 152
Encapsulating object creation......Page 154
Building a simple pizza factory......Page 155
Reworking the PizzaStore class......Page 156
The Simple Factory defined......Page 157
Franchising the pizza store......Page 158
A framework for the pizza store......Page 160
Allowing the subclasses to decide......Page 161
Let's make a PizzaStore......Page 163
Declaring a factory method......Page 165
It's finally time to meet the Factory Method Pattern......Page 171
Another perspective: parallel class hierarchies......Page 172
Factory Method Pattern defined......Page 174
Looking at object dependencies......Page 178
The Dependency Inversion Principle......Page 179
Applying the Principle......Page 180
Inverting your thinking......Page 182
A few guidelines to help you follow the Principle......Page 183
Building the ingredient factories......Page 186
Abstract Factory Pattern defined......Page 196
Factory method and Abstract Factory compared......Page 200
Tools for your Design Toolbox......Page 202
Design Patterns Crossword......Page 203
Design Puzzle Solution......Page 205
Design Patterns Crossword Solution......Page 208
Chapter 5: The Singleton Pattern......Page 209
Dissecting the classic Singleton Pattern implementation......Page 213
The Chocolate Factory......Page 215
Singleton Pattern defined......Page 217
Dealing with multithreading......Page 220
Meanwhile, back at the chocolate factory......Page 223
Tools for your Design Toolbox......Page 226
Design Patterns Crossword......Page 227
Chapter 6: The Command Pattern......Page 231
Free hardware! Let's check out the Remote Control......Page 233
Taking a look at the vendor classes......Page 234
The Objectville Diner roles and responsibilities......Page 239
From the Diner to the Command Pattern......Page 241
Our first command object......Page 243
Using the command object......Page 244
The Command Pattern defined......Page 246
The Command Pattern defined: the class diagram......Page 247
Assigning Commands to slots......Page 249
Implementing the Remote Control......Page 250
Implementing the Commands......Page 251
Putting the Remote Control through its paces......Page 252
Time to write that documentation......Page 255
Time to QA that Undo button!......Page 259
Using state to implement Undo......Page 260
Adding Undo to the CeilingFan commands......Page 261
Testing the ceiling fan......Page 263
Every remote needs a Party Mode!......Page 264
Using a macro command......Page 265
The Command Pattern means lots of command classes......Page 268
Simplifying the Remote Control with lambda expressions......Page 269
Test the remote control with lambda expressions......Page 272
More uses of the Command Pattern: queuing requests......Page 275
More uses of the Command Pattern: logging requests......Page 276
Tools for your Design Toolbox......Page 277
Design Patterns Crossword......Page 278
Chapter 7: The Adapter and Facade Patterns......Page 281
Adapters all around us......Page 282
Object-oriented adapters......Page 283
Test drive the adapter......Page 286
The Adapter Pattern explained......Page 287
Adapter Pattern defined......Page 289
Object and class adapters......Page 290
Real-world adapters......Page 294
Adapting an Enumeration to an Iterator......Page 295
Home Sweet Home Theater......Page 301
Lights, Camera, Facade!......Page 304
Constructing your home theater facade......Page 307
Implementing the simplified interface......Page 308
Facade Pattern defined......Page 310
The Principle of Least Knowledge......Page 311
How NOT to Win Friends and Influence Objects......Page 312
The Facade and the Principle of Least Knowledge......Page 315
Tools for your Design Toolbox......Page 316
Design Patterns Crossword......Page 317
Design Patterns Crossword Solution......Page 320
Chapter 8: The Template Method Pattern......Page 321
Whipping up some coffee and tea classes (in Java)......Page 323
Sir, may I abstract your Coffee, Tea?......Page 326
Taking the design further......Page 327
Abstracting prepareRecipe()......Page 328
Meet the Template Method......Page 332
What did the Template Method get us?......Page 334
Template Method Pattern defined......Page 335
Hooked on Template Method......Page 338
Using the hook......Page 339
Let's run the Test Drive......Page 340
The Hollywood Principle......Page 342
The Hollywood Principle and Template Method......Page 343
Template Methods in the Wild......Page 345
Sorting with Template Method......Page 346
Comparing Ducks and Ducks......Page 348
The making of the sorting duck machine......Page 350
Swingin' with Frames......Page 352
Applet......Page 353
Design Patterns Crossword......Page 356
Tools for your Design Toolbox......Page 357
Design Pattern Crossword Solution......Page 359
Chapter 9: The Iterator and Composite Patterns......Page 361
Breaking News: Objectville Diner and Objectville Pancake House Merge......Page 362
Check out the Menu Items......Page 363
Lou and Mel's Menu Implementations......Page 364
What's the problem with having two different menu representations?......Page 366
Can we encapsulate the iteration?......Page 370
Meet the Iterator Pattern......Page 372
Adding an Iterator to DinerMenu......Page 373
Reworking the Diner Menu with Iterator......Page 374
Fixing up the Waitress Code......Page 375
Testing our code......Page 376
Cleaning things up with java.util.Iterator......Page 380
Iterator Pattern defined......Page 383
Taking a look at the Cafe Menu......Page 389
Reworking the Cafe Menu code......Page 390
Adding the Cafe Menu to the Waitress......Page 391
Iterators and Collections......Page 395
The Composite Pattern defined......Page 402
Designing Menus with Composite......Page 405
Implementing the Menu Component......Page 406
Implementing the Menu Item......Page 407
Implementing the Composite Menu......Page 408
Flashback to Iterator......Page 414
The Composite Iterator......Page 415
The Null Iterator......Page 418
The magic of Iterator & Composite together......Page 420
Design Patterns Crossword......Page 424
Tools for your Design Toolbox......Page 426
Design Patterns Crossword Solution......Page 430
Chapter 10: The State Pattern......Page 431
Java Breakers......Page 432
State machines 101......Page 434
Writing the code......Page 436
In-house testing......Page 438
The messy STATE of things......Page 442
The new design......Page 444
Defining the State interfaces and classes......Page 445
Implementing our State classes......Page 447
Reworking the Gumball Machine......Page 448
Implementing more states......Page 450
The State Pattern defined......Page 456
Tools for your Design Toolbox......Page 469
Design Puzzle Solution......Page 470
Chapter 11: The Proxy Pattern......Page 475
Coding the Monitor......Page 477
Testing the Monitor......Page 478
The role of the 'remote proxy'......Page 480
Adding a remote proxy to the Gumball Machine monitoring code......Page 482
Remote methods 101......Page 483
How the method call happens......Page 484
Java RMI, the Big Picture......Page 486
Making the Remote service......Page 487
Step one: make a Remote interface......Page 488
Step two: make a Remote implementation......Page 489
Step three: run rmiregistry......Page 490
Complete code for the server side......Page 491
How does the client get the stub object?......Page 492
Complete client code......Page 494
Registering with the RMI registry......Page 498
Writing the Monitor test drive......Page 500
The Proxy Pattern defined......Page 505
Get ready for Virtual Proxy......Page 507
Displaying CD covers......Page 508
Designing the CD cover Virtual Proxy......Page 509
Writing the Image Proxy......Page 510
Using the Java API's Proxy to create a protection proxy......Page 519
Matchmaking in Objectville......Page 520
Step one: creating Invocation Handlers......Page 525
Step two: creating the Proxy class and instantiating the Proxy object......Page 528
Testing the matchmaking service......Page 529
The Proxy Zoo......Page 532
Design Patterns Crossword......Page 534
Tools for your Design Toolbox......Page 535
Design Patterns Crossword Solution......Page 537
Chapter 12: Compound Patterns......Page 543
Working together......Page 544
Duck reunion......Page 545
The King of Compound Patterns......Page 570
Meet the Model-View-Controller......Page 573
Looking at MVC through patterns-colored glasses......Page 576
Using MVC to control the beat......Page 578
Putting the pieces together......Page 580
Building the pieces......Page 581
The View......Page 583
Implementing the View......Page 584
Now for the Controller......Page 586
Exploring Strategy......Page 589
Adapting the Model......Page 590
MVC and the Web......Page 593
Design Patterns and Model 2......Page 601
Tools for your Design Toolbox......Page 604
Chapter 13: Better Living with Patterns......Page 621
Design Pattern defined......Page 623
Looking more closely at the Design Pattern definition......Page 625
So you wanna be a Design Patterns writer......Page 631
Organizing Design Patterns......Page 633
Pattern Categories......Page 634
Thinking in Patterns......Page 638
Don't forget the power of the shared vocabulary......Page 643
The Patterns Zoo......Page 648
Annihilating evil with Anti-Patterns......Page 650
Tools for your Design Patterns......Page 652
Leaving Objectville......Page 653
Chapter 14: Appendix......Page 655
Why use the Bridge Pattern?......Page 657
Builder......Page 658
Why use the Builder Pattern?......Page 659
Chain of Responsbility......Page 660
How to use the Chain of Responsibility Pattern......Page 661
Flyweight......Page 662
Why use the Flyweight Pattern?......Page 663
Interpreter......Page 664
How to implement an interpreter......Page 665
Mediator......Page 666
Mediator in action......Page 667
Memento......Page 668
The Memento at work......Page 669
Prototype......Page 670
Prototype to the rescue......Page 671
Visitor......Page 672
The Visitor drops by......Page 673
Index......Page 675
Colophon......Page 692

Polecaj historie