logo资料库

design patterns elements of reusable object-oriented software.pdf

第1页 / 共434页
第2页 / 共434页
第3页 / 共434页
第4页 / 共434页
第5页 / 共434页
第6页 / 共434页
第7页 / 共434页
第8页 / 共434页
资料共434页,剩余部分请下载后查看
Contents
Preface to CD
Preface to Book
Foreword
Guide to Readers
1. Introduction
What is a Design Pattern?
Design Patterns in Smalltalk MVC
Describing Design Patterns
Pattern Name and Classification
Intent
Also Known As
Motivation
Applicability
Structure
Participants
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
The Catalog of Design Patterns
Abstract Factory (99)
Adapter (157)
Bridge (171)
Builder (110)
Chain of Responsibility (251)
Command (263)
Composite (183)
Decorator (196)
Facade (208)
Factory Method (121)
Flyweight (218)
Interpreter (274)
Iterator (289)
Mediator (305)
Memento (316)
Observer (326)
Prototype (133)
Proxy (233)
Singleton (144)
State (338)
Strategy (349)
Template Method (360)
Visitor (366)
Organizing the Catalog
How Design Patterns Solve Design Problems
Finding Appropriate Objects
Determining Object Granularity
Specifying Object Interfaces
Specifying Object Implementations
Putting Reuse Mechanisms to Work
Designing for Change
How to Select a Design Pattern
How to Use a Design Pattern
2. A Case Study: Design a Document Editor
Design Problems
Document Structure
Recursive Composition
Glyphs
Composite Pattern
Formatting
Encapsulating the Formatting Algorithm
Compositor and Composition
Strategy Pattern
Embellishing the User Interface
Transparent Enclosure
Monoglyph
Decorator Pattern
Factories and Product Classes
Abstract Factory Pattern
Supporting Multiple Window Systems
Can We Use an Abstract Factory?
Encapsulating Implementation Dependencies
Window and WindowImp
WindowImp Subclasses
Configuring Windows with WindowImps
Bridge Pattern
User Operations
Encapsulating a Request
Command Class and Subclasses
Undoability
Command History
Command Pattern
Spelling Checking and Hyphenation
Accessing Scattered Information
Encapsulating Access and Traversal
Iterator Class and Subclasses
Iterator Pattern
Traversal versus Traversal Actions
Encapsulating the Analysis
Visitor Class and Subclasses
Visitor Pattern
Summary
3. Creational Patterns
Abstract Factory
Intent
Also Known As
Motivation
Applicability
Structure
 Client
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Builder
Intent
Motivation
Applicability
Structure
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Factory Method
Intent
Also Known As
Motivation
Applicability
Structure
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Prototype
Intent
Motivation
Applicability
Structure
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Singleton
Intent
Motivation
Applicability
Structure
 Singleton
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Discussion of Creational Patterns
4. Structural Patterns
Adapter
Intent
Also Known As
Motivation
Applicability
Structure
Participants
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Bridge
Intent
Also Known As
Motivation
Applicability
Structure
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Composite
Intent
Motivation
Applicability
Structure
Participants
 Client
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Decorator
Intent
Also Known As
Motivation
Applicability
[Sym93a]
[Sym93b]
[Sza92]
[US87]
[VL88]
[VL90]
[WBJ90]
Implementation
[Sym93a]
[Sym93b]
[Sza92]
[US87]
[VL88]
[VL90]
[WBJ90]
Sample Code
[Sym93a]
[Sym93b]
[Sza92]
[US87]
[VL88]
[VL90]
[WBJ90]
[Sym93a]
[Sym93b]
[Sza92]
[US87]
[VL88]
[VL90]
[WBJ90]
[SS86]
[SS94]
[Str91]
[Str93]
[Str94]
[Sut63]
[Swe85]
Façade
Intent
Motivation
[SS86]
[SS94]
[Str91]
[Str93]
[Str94]
[Sut63]
[Swe85]
Structure
Collaborations
Consequences
[SS86]
[SS94]
[Str91]
[Str93]
[Str94]
[Sut63]
[Swe85]
Sample Code
[SS86]
[SS94]
[Str91]
[Str93]
[Str94]
[Sut63]
[Swe85]
[Pas86]
[Pug90]
[RBP+91]
[Rum94]
[SE84]
[Sha90]
[Sny86]
[Pas86]
[Pug90]
[RBP+91]
[Rum94]
[SE84]
[Sha90]
[Sny86]
[Pas86]
[Pug90]
[RBP+91]
[Rum94]
[SE84]
[Sha90]
[Sny86]
[Pas86]
[Pug90]
[RBP+91]
[Rum94]
[SE84]
[Sha90]
[Sny86]
[Pas86]
[Pug90]
[RBP+91]
[Rum94]
[SE84]
[Sha90]
[Sny86]
Participants
 Flyweight
 FlyweightFactory
 Client
Collaborations
[Pas86]
[Pug90]
[RBP+91]
[Rum94]
[SE84]
[Sha90]
[Sny86]
Sample Code
[Pas86]
[Pug90]
[RBP+91]
[Rum94]
[SE84]
[Sha90]
[Sny86]
[Pas86]
[Pug90]
[RBP+91]
[Rum94]
[SE84]
[Sha90]
[Sny86]
[Pas86]
[Pug90]
[RBP+91]
[Rum94]
[SE84]
[Sha90]
[Sny86]
[Pas86]
[Pug90]
[RBP+91]
[Rum94]
[SE84]
[Sha90]
[Sny86]
Related Patterns
[McC87]
[Mey88]
[Mur93]
[OJ90]
[OJ93]
[P+88]
[Par90]
Proxy
Intent
Also Known As
Motivation
[McC87]
[Mey88]
[Mur93]
[OJ90]
[OJ93]
[P+88]
[Par90]
Structure
[McC87]
[Mey88]
[Mur93]
[OJ90]
[OJ93]
[P+88]
[Par90]
Implementation
[McC87]
[Mey88]
[Mur93]
[OJ90]
[OJ93]
[P+88]
[Par90]
[LG86]
[Lie85]
[Lie86]
[Lin92]
[LP93]
[LR93]
[LVC89]
[Mar91]
[LG86]
[Lie85]
[Lie86]
[Lin92]
[LP93]
[LR93]
[LVC89]
[Mar91]
Known Uses
Related Patterns
[LG86]
[Lie85]
[Lie86]
[Lin92]
[LP93]
[LR93]
[LVC89]
[Mar91]
Discussion of Structural Patterns
Adapter versus Bridge
[LG86]
[Lie85]
[Lie86]
[Lin92]
[LP93]
[LR93]
[LVC89]
[Mar91]
[LG86]
[Lie85]
[Lie86]
[Lin92]
[LP93]
[LR93]
[LVC89]
[Mar91]
Chain of Responsibility
Intent
Motivation
[JZ91]
[Kir92]
[Knu73]
[Knu84]
[Kof93]
[KP88]
[LaL94]
[LCI+92]
[Lea88]
Applicability
Structure
Participants
 Client
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Command
Intent
Also Known As
Motivation
Applicability
Structure
 Command
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Interpreter
Intent
Motivation
Applicability
Structure
 Context
 Client
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Iterator
Intent
Also Known As
Motivation
Applicability
Structure
 Iterator
 ConcreteIterator
 Aggregate
 ConcreteAggregate
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Mediator
Intent
Motivation
Applicability
Structure
Participants
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Memento
Intent
Also Known As
Motivation
Applicability
Structure
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Observer
Intent
Also Known As
Motivation
Applicability
Structure
 Subject
 Observer
 ConcreteSubject
 ConcreteObserver
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
State
Intent
Also Known As
Motivation
Applicability
Structure
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Strategy
Intent
Also Known As
Motivation
Applicability
Structure
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Template Method
Intent
Motivation
Applicability
Structure
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Visitor
Intent
Motivation
[JZ91]
[Kir92]
[Knu73]
[Knu84]
[Kof93]
[KP88]
[LaL94]
[LCI+92]
[Lea88]
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
Discussion of Behavioral Patterns
Encapsulating Variation
Objects as Arguments
Should Communication be Encapsulated or Distributed?
Decoupling Senders and Receivers
Summary
6. Conclusion
What to Expect from Design Patterns
A Common Design Vocabulary
A Documentation and Learning Aid
An Adjunct to Existing Methods
A Target for Refactoring
A Brief History
The Pattern Community
Alexander's Pattern Languages
Patterns in Software
An Invitation
A Parting Thought
Appendix A: Glossary
abstract class
abstract coupling
abstract operation
acquaintance relationship
aggregate object
aggregation relationship
black-box reuse
Class
class diagram
class operation
concrete class
constructor
coupling
delegation
design pattern
destructor
dynamic binding
encapsulation
framework
friend class
inheritance
instance variable
interaction diagram
interface
metaclass
mixin class
object
object composition
object diagram
object reference
operation
overriding
parameterized type
parent class
polymorphism
private inheritance
protocol
receiver
request
signature
subclass
derived class.
subtype
supertype
toolkit
type
white-box reuse
Appendix B: Guide to Notation
Class Diagram
[JZ91]
[Kir92]
[Knu73]
[Knu84]
[Kof93]
[KP88]
[LaL94]
[LCI+92]
[Lea88]
Object Diagram
Interaction Diagram
Appendix C: Foundation Classes
List
Construction, Destruction, Initialization, and Assignment
Accessing
[JZ91]
[Kir92]
[Knu73]
[Knu84]
[Kof93]
[KP88]
[LaL94]
[LCI+92]
[Lea88]
Stack Interface
Iterator
ListIterator
Point
[JZ91]
[Kir92]
[Knu73]
[Knu84]
[Kof93]
[KP88]
[LaL94]
[LCI+92]
[Lea88]
[JZ91]
[Kir92]
[Knu73]
[Knu84]
[Kof93]
[KP88]
[LaL94]
[LCI+92]
[Lea88]
[JZ91]
[Kir92]
[Knu73]
[Knu84]
[Kof93]
[KP88]
[LaL94]
[LCI+92]
[Lea88]
[Car92]
[CIRM93]
[CL90]
[CL92]
[Coa92]
[Coo92]
[Cop92]
[Cur89]
[dCLF93]
[Deu89]
[Ede92]
[EG92]
[ES90]
[Foo92]
[GA89]
[Gam91]
[Gam92]
[Gla90]
[GM92]
[GR83]
[HHMV92]
[HO87]
[ION94]
[JCJO92]
[JF88]
[JML92]
[Joh92]
[JZ91]
[Kir92]
[Knu73]
[Knu84]
[Kof93]
[KP88]
[LaL94]
[LCI+92]
[Lea88]
[LG86]
[Lie85]
[Lie86]
[Lin92]
[LP93]
[LR93]
[LVC89]
[Mar91]
[McC87]
[Mey88]
[Mur93]
[OJ90]
[OJ93]
[P+88]
[Par90]
[Pas86]
[Pug90]
[RBP+91]
[Rum94]
[SE84]
[Sha90]
[Sny86]
[SS86]
[SS94]
[Str91]
[Str93]
[Str94]
[Sut63]
[Swe85]
[Sym93a]
[Sym93b]
[Sza92]
[US87]
[VL88]
[VL90]
[WBJ90]
[WBWW90]
[WGM88]
Design pattern relationships
Design Patterns: Elements of Reusable Object-Oriented Software Design Patterns Elements of Reusable Object-Oriented Software 1
Design Patterns: Elements of Reusable Object-Oriented Software Contents Preface to CD ............................................................... Preface to Book ............................................................. Foreword .................................................................... 5 7 9 Guide to Readers ............................................................... 10 1 Introduction ................................................................. 11 1.1 What Is a Design Pattern? ................................................ 12 1.2 Design Patterns in Smalltalk MVC ......................................... 14 1.3 Describing Design Patterns ............................................... 16 1.4 The Catalog of Design Patterns ........................................... 18 1.5 Organizing the Catalog ................................................... 21 1.6 How Design Patterns Solve Design Problems ................................ 23 1.7 How to Select a Design Pattern ........................................... 42 1.8 How to Use a Design Pattern .............................................. 44 2 A Case Study: Designing a Document Editor .................................... 46 2.1 Design Problems .......................................................... 46 2.2 Document Structure ....................................................... 47 2.3 Formatting ............................................................... 53 2.4 Embellishing the User Interface .......................................... 56 2.5 Supporting Multiple Look-and-Feel Standards .............................. 60 2.6 Supporting Multiple Window Systems ....................................... 64 2.7 User Operations .......................................................... 72 2.8 Spelling Checking and Hyphenation ........................................ 77 2.9 Summary .................................................................. 90 Design Pattern Catalog ......................................................... 93 3 Creational Patterns .......................................................... 94 Abstract Factory ............................................................. 99 Builder ..................................................................... 110 Factory Method .............................................................. 121 Prototype ................................................................... 133 Singleton ................................................................... 144 Discussion of Creational Patterns ............................................. 153 2
Design Patterns: Elements of Reusable Object-Oriented Software 4 Structural Patterns ......................................................... 155 Adapter ..................................................................... 157 Bridge ...................................................................... 171 Composite ................................................................... 183 Decorator ................................................................... 196 Façade ...................................................................... 208 Flyweight ................................................................... 218 Proxy ....................................................................... 233 Discussion of Structural Patterns ............................................. 246 5 Behavioral Patterns ......................................................... 249 Chain of Responsibility ..................................................... 251 Command ..................................................................... 263 Interpreter ................................................................. 274 Iterator .................................................................... 289 Mediator .................................................................... 305 Memento ..................................................................... 316 Observer .................................................................... 326 State ....................................................................... 338 Strategy .................................................................... 349 Template Method ............................................................. 360 Visitor ..................................................................... 366 Discussion of Behavioral Patterns ............................................. 382 6 Conclusion .................................................................. 388 6.1 What to Expect from Design Patterns ..................................... 388 6.2 A Brief History ......................................................... 392 6.3 The Pattern Community ................................................... 393 6.4 An Invitation ........................................................... 395 6.5 A Parting Thought ....................................................... 396 A Glossary .................................................................... 397 B Guide to Notation ........................................................... 404 B.1 Class Diagram ........................................................... 404 B.2 Object Diagram .......................................................... 406 B.3 Interaction Diagram ..................................................... 407 C Foundation Classes .......................................................... 409 C.1 List .................................................................... 409 C.2 Iterator ................................................................ 412 C.3 ListIterator ............................................................ 413 C.4 Point ................................................................... 413 3
Design Patterns: Elements of Reusable Object-Oriented Software C.5 Rect .................................................................... 414 Bibliography .................................................................. 416 4
Design Patterns: Elements of Reusable Object-Oriented Software As we were writing Design Patterns, we knew the patterns we were describing had Preface to CD value because they had proven themselves in many different contexts. Our hope was that other software engineers would benefit from these patterns as much as we had. Now, three years after its debut, we find ourselves both grateful and thrilled by how the book has been received. Lots of people use it. Many tell us the patterns have helped them design and build better systems. Many others have been inspired to write their own patterns, and the pool of patterns is growing. And many have commented on what might be improved about the book and what they would like to see in the future. A recurring comment in all the feedback has been how well-suited the book is to hypertext. There are numerous cross-references, and chasing references is something a computer can do very well. Since much of the software development process takes place on computers, it would be natural to have a book like ours as an on-line resource. Observations like these got us excited about the potential of this medium. So when Mike Hendrickson approached us about turning the book into a CD-ROM, we jumped at the chance. Two years and several megabytes of e-mail later, we're delighted that you can finally obtain this edition, the Design Patterns CD, and put its unique capabilities to work. Now you can access a pattern from your computer even when someone has borrowed your book. You can search the text for key words and phrases. It's also considerably easier to incorporate parts of it in your own on-line documentation. And if you travel with a notebook computer, you can keep the book handy without lugging an extra two pounds of paper. Hypertext is a relatively new publishing venue, one we are learning to use just like everyone else. If you have ideas on how to improve this edition, please send them to design-patterns-cd@cs.uiuc.edu. If you have questions or suggestions concerning the patterns themselves, send them to the gang-of-4-patterns@cs.uiuc.edu mailing list. (To subscribe, send e-mail to gang-of-4-patterns@cs.uiuc.edu with the subject "subscribe".) This list has quite a few readers, and many of them can answer questions as well as we can – and usually a lot faster! Also, be sure to check out the Patterns Home Page at http://hillside.net/patterns/. There you'll find other books and mailing lists on patterns, not to mention conference information and patterns published on-line. This CD entailed considerable design and implementation work. We are indebted to Mike Hendrickson and the team at Addison-Wesley for their on-going encouragement and support. Jeff Helgesen, Jason Jones, and Daniel Savarese garner many thanks for 5
Design Patterns: Elements of Reusable Object-Oriented Software their development effort and for patience despite what must appear to have been our insatiable appetite for revision. A special acknowledgment is due IBM Research, which continues to underwrite much of this activity. We also thank the reviewers, including Robert Brunner, Sandeep Dani, Bob Koss, Scott Meyers, Stefan Schulz, and the Patterns Discussion Group at the University of Illinois Urbana-Champaign. Their advice led to at least one major redesign and several minor ones. Sydney, Australia Urbana, Illinois Hawthorne, New York August 1997 Finally, we thank all who have taken time to comment on Design Patterns. Your feedback has been invaluable to us as we strive to better our understanding and presentation of this material. Zurich, Switzerland E.G. R.H. R.J. J.V. 6
Preface to Book This book isn't an introduction to object-oriented technology or design. Many books already do a good job of that. This book assumes you are reasonably proficient in at least one object-oriented programming language, and you should have some experience in object-oriented design as well. You definitely shouldn't have to rush to the nearest dictionary the moment we mention "types" and "polymorphism," or "interface" as opposed to "implementation" inheritance. Design Patterns: Elements of Reusable Object-Oriented Software On the other hand, this isn't an advanced technical treatise either. It’s a book of design patterns that describes simple and elegant solutions to specific problems in object-oriented software design. Design patterns capture solutions that have developed and evolved overtime. Hence they aren't the designs people tend to generate initially. They reflect untold redesign and recoding as developers have struggled for greater reuse and flexibility in their software. Design patterns capture these solutions in a succinct and easily applied form. The design patterns require neither unusual language features nor amazing programming tricks with which to astound your friends and managers. All can be implemented in standard object-oriented languages, though they might take a little more work than ad hoc solutions. But the extra effort invariably pays dividends in increased flexibility and reusability. Once you understand the design patterns and have had an "Aha!" (and not just a "Huh?") experience with them, you won't ever think about object-oriented design in the same way. You'll have insights that can make your own designs more flexible, modular, reusable, and understandable — which is why you're interested in object- oriented technology in the first place, right? A word of warning and encouragement: Don't worry if you don’t understand this book completely on the first reading. We didn’t understand it all on the first writing! Remember that this isn't a book to read once and put on a shelf. We hope you'll find yourself referring to it again and again for design insights and for inspiration. This book has had a long gestation. It has seen four countries, three of its authors' marriages, and the birth of two (unrelated) offspring. Many people have had a part in its development. Special thanks are due Bruce Anderson, Kent Beck, and André Wein and for their inspiration and advice. We also thank those who reviewed drafts of the manuscript: Roger Bielefeld, Grady Booch, Tom Cargill, Marshall Cline, Ralph Hyre, Brian Kernighan, Thomas Laliberty, Mark Lorenz, Arthur Riel, Doug Schmidt, Clovis Tondo, Steve Vinoski, and Rebecca Wirfs-Brock. We are also grateful to the team at Addison-Wesley for their help and patience: Kate Habib, Tiffany Moore, 7
Design Patterns: Elements of Reusable Object-Oriented Software Lisa Raffaele, Pradeepa Siva, and John Wait. Special thanks to Carl Kessler, Danny Sabbah, and Mark Wegman at IBM Research for their unflagging support of this work. Last but certainly not least, we thank everyone on the Internet and points beyond who commented on versions of the patterns, offered encouraging words, and told us that what we were doing was worthwhile. These people include but are not limited to Jon Avotins, Steve Berczuk, Julian Berdych, Matthias Bohlen, John Brant, Allan Clarke, Paul Chisholm, Jens Coldewey, Dave Collins, Jim Coplien, Don Dwiggins, Gabriele Elia, Doug Felt, Brian Foote, Denis Fortin, Ward Harold, Hermann Hueni, Nayeem Islam, Bikramjit Kalra, Paul Keefer, Thomas Kofler, Doug Lea, Dan LaLiberte, James Long, Ann Louise Luu, Pundi Madhavan, Brian Marick, Robert Martin, Dave McComb, Carl McConnell, Christine Mingins, Hanspeter Mössenböck, Eric Newton, Marianne Ozkan, Roxsan Payette, Larry Podmolik, George Radin, Sita Ramakrishnan, Russ Ramirez, Alexander Ran, Dirk Riehle, Bryan Rosenburg, Aamod Sane, Duri Schmidt, Robert Seidl, Xin Shu, and Bill Walker. We don't consider this collection of design patterns complete and static; it's more a recording of our current thoughts on design. We welcome comments on it, whether criticisms of our examples, references and known uses we've missed, or design patterns we should have included. You can write us care of Addison-Wesley, or send electronic mail to design-patterns@cs.uiuc.edu. You can also obtain softcopy for the code in the Sample Code sections by sending the message "send design pattern source" to design-patterns-source@cs.uiuc.edu. And now there's a Web page at http://st- www.cs.uiuc.edu/users/patterns/DPBook/DPBook.html for late-breaking information and updates. Mountain View, California Montreal, Quebec Urbana, Illinois Hawthorne, New York August 1994 E.G. R.H. R.J. J.V. 8
分享到:
收藏