[Graphic Design] Head First - Design Patterns.pdf

(54922 KB) Pobierz
DPFrontMatterFinalFinalwithQuotes.indd
table of contents
Table of Contents (summary)
Intro
xxv
1 Welcome to Design Patterns: an introduction
1
2 Keeping your Objects in the know: the Observer Pattern
37
3 Decorating Objects: the Decorator Pattern
79
4 Baking with OO goodness: the Factory Pattern
109
5 One of a Kind Objects: the Singleton Pattern
169
6 Encapsulating Invocation: the Command Pattern
191
7 Being Adaptive: the Adapter and Facade Patterns
235
8 Encapsulating Algorithms: theTemplate Method Pattern
275
9 Well-managed Collections: the Iterator and Composite Patterns
315
10 The State of Things: the State Pattern
385
11 Controlling Object Access: the Proxy Pattern
429
12 Patterns of Patterns: Compound Patterns
499
13 Patterns in the Real World: Better Living with Patterns
577
14 Appendix: Leftover Patterns
611
Table of Contents (the real thing)
Intro
Your brain on Design Patterns. Here you are trying to learn something, while
here your brain is doing you a favor by making sure the learning doesn’t stick . Your brain’s
thinking, “Better leave room for more important things, like which wild animals to avoid and
whether naked snowboarding is a bad idea.” So how do you trick your brain into thinking
that your life depends on knowing Design Patterns?
Who is this book for?
xxvi
We know what your brain is thinking
xxvii
Metacognition
xxix
Bend your brain into submission
xxxi
Technical reviewers
xxxiv
Acknowledgements
xxxv
x
intro to Design Patterns
Someone has already solved your problems. In this chapter,
you’ll learn why (and how) you can exploit the wisdom and lessons learned by
other developers who’ve been down the same design problem road and survived
the trip. Before we’re done, we’ll look at the use and benefits of design patterns,
look at some key OO design principles, and walk through an example of how one
pattern works. The best way to use patterns is to load your brain with them and
then recognize places in your designs and existing applications where you can
apply them . Instead of code reuse, with patterns you get experience reuse.
The SimUDuck app
2
Remember, knowing
concepts like abstraction,
inheritance, and polymorphism do
not make you a good object oriented
designer. A design guru thinks
about how to create flexible
designs that are maintainable
and that can cope with
change.
Joe thinks about inheritance...
5
How about an interface?
6
The one constant in software development
8
Separating what changes from what stays the same
10
Designing the Duck Behaviors
11
Testing the Duck code
18
Setting behavior dynamically
20
The Big Picture on encapsulated behaviors
22
HAS-A can be better than IS-A
23
The Strategy Pattern
24
The power of a shared pattern vocabulary
28
How do I use Design Patte rns?
29
Tools for your Design Toolbox
32
Exercise Solutions
34
Object that
holds state
OBSERVER
8
int
8
8
8
8
O
Automatic update/notification
Observers
xi
1 Welcome to Design Patterns
83555817.009.png 83555817.010.png 83555817.011.png 83555817.012.png 83555817.001.png
table of contents
the Observer Pattern
Don’t miss out when something interesting happens!
We’ve got a pattern that keeps your objects in the know when something they
might care about happens. Objects can even decide at runtime whether they
want to be kept informed. The Observer Pattern is one of the most heavily used
patterns in the JDK, and it’s incredibly useful. Before we’re done, we’ll also look
at one to many relationships and loose coupling (yeah, that’s right, we said
coupling). With Observer, you’ll be the life of the Patterns Party.
The Weather Monitoring application
39
Meet the Observer Pattern
44
Publishers + Subscribers = Observer Pattern
45
Five minute drama: a subject for observation
48
The Observer Pattern defined
51
The power of Loose Coupling
53
Designing the Weather Station
56
Implementing the Weather Station
57
Using Java’s built-in Observer Pattern
64
The dark side of java.util.Observable
71
Tools for your Design Toolbox
74
Exercise Solutions
78
Object that
holds state
ONE TO MANY RELATIONSHIP
8
8
8
8
8
int
O
Automatic update/notification
Observers
xii
2 Keeping your Objects in the Know
83555817.002.png 83555817.003.png 83555817.004.png 83555817.005.png
the Decorator Pattern
Just call this chapter “Design Eye for the Inheritance
Guy.” We’ll re-examine the typical overuse of inheritance and you’ll learn how
to decorate your classes at runtime using a form of object composition. Why?
Once you know the techniques of decorating, you’ll be able to give your (or
someone else’s) objects new responsibilities without making any code changes
to the underlying classes.
Welcome to Starbuzz Coffee
80
The Open-Closed Principle
86
I used to think real men
subclassed everything. That was until
I learned the power of extension
at runtime, rather than at compile
time. Now look at me!
Meet the Decorator Pattern
88
Constructing a Drink Order with Decorators
89
The Decorator Pattern Defined
91
Decorating our Beverages
92
Writing the Starbuzz code
95
Real World Decorators: Java I/O
100
Writing your own Java I/O Decorator
102
Tools for your Design Toolbox
105
Exercise Solutions
106
xiii
3 Decorating Objects
83555817.006.png 83555817.007.png
table of contents
the Factory Pattern
4 Baking with OO Goodness
Get ready to cook some loosely coupled OO designs.
There is more to making objects than just using the new operator. You’ll learn
that instantiation is an activity that shouldn’t always be done in public and can
often lead to coupling problems . And you don’t want that , do you? Find out how
Factory Patterns can help save you from embarrasing dependencies.
When you see “new”, think “concrete”
110
The clients of the A bstract
Factory are the two
instances of our Pizza Store,
NYPizzaStore and
ChicagoStylePizzaSore.
Objectville Pizza
112
Encapsulating object creation
114
NYPizzaStore
createPizza()
The abst ract PizzaIngredientFactory
is the int erface that defines how to
make a fa mily of related products
- everythin g we need to make a pizza.
Building a simple pizza factory
115
<<interface>>
Dough
The Simple Factory defined
117
ThickCrustDough
ThinCrustDough
<<interface>>
PizzaIngredientFactory
createDough()
createSauce()
createCheese()
createVeggies()
createPepperoni()
createClam()
<<interface>>
Sauce
A Framework for the pizza store
120
PlumTomatoSauce
MarinaraSauce
Allowing the subclasses to decide
121
NYPizzaIngredientFactory
ChicagoPizzaIngredientFactory
<<interface>>
Cheese
createDough()
createSauce()
createCheese()
createVeggies()
createPepperoni()
createClam()
createDough()
createSauce()
createCheese()
createVeggies()
createPepperoni()
createClam()
Mozzarella Cheese
ReggianoCheese
Let’s make a PizzaStore
123
<<interface>>
Clams
The job of the concrete
pizza facto ries is to
make pizza i ngredients.
Each factory knows
how to create the right
objects for th eir region.
FrozenClams
FreshClams
Declaring a factory method
125
Meet the Factory Method Pattern
131
Parallel class hierarchies
132
Factory Method Pattern defined
134
A very dependent PizzaStore
137
Looking at object dependencies
138
The Dependency Inversion Principle
139
Meanwhile, back at the PizzaStore...
144
Families of ingredients...
145
Building our ingredient factories
146
Looking at the Abstract Factory
153
Behind the scenes
154
Abstract Factory Pattern defined
156
Factory Method and Abstract Factory compared
160
Tools for your Design Toolbox
162
Exercise Solutions
164
xiv
83555817.008.png
Zgłoś jeśli naruszono regulamin