Design Patterns:Elements of Rousable Object-Oriented Software Contents Preface to CD…5 preface to Book........................................................... Guide to Readers..............10 1 Introduct1on..,,..,,,,,..,,,,.,.,.....,.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 Deaign Patterns...............................18 1.50 rganiz1 ng the Cata1og.,.......21 1,6 ow Des1 gn Patterns Solve Des.1 gn 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.1De81 gn Problem8,..。.+··。….…。…····。·..。.,。·。.。.。·.…·。·。46 2.2 pocument structure..............................................47 2.3 Formatting… 2.4 Embellishing the User Interte ce …56 2.5 Supporting Multiple Look-and-Feel Standards...........60 2.6 Supporting Multiple Window Systems.............................64 2.70ser0 perations72 2.8 Spelling Checking and Hyphenation..............................77 2.9Summary 90 Design Pattern catalog..............................................93 3 Creational Patterns Factory................................................... 8u1dex.110 Fact0 ry Method,,,,.,。,。,,,。,,,,,,,,,,t,,,,。,,,,,,.,,,。,,,,,,121 Prototype.........................................................133 Singleton...... ......144 Discussion of Creational patterng........................ 153
Design Patterns: Elements of Reusable Object-Oriented Software 2 Contents Preface to CD ........................................................ 5 Preface to Book ...................................................... 7 Foreword ............................................................. 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
Design Patterns:Elements of Rousable Object-Orientod Software 4 structural patterns 155 ......................................................... Bridge。,。,,,。。。。,171 Comp0site.,....,.,.,..,,.,.,..,.,.,,,.,...。,..,.................183 Facade. 208 eight 218 233 Discussion of structural Patterns.................................246 5 Behavioral Patterna 249 chain of Responsibility........................................... Command.............................................63 Interpreter.......................................................274 t江at0江.289 ..................... 305 16 ObBCEVOE..........................................................326 State。。338 Strategy,,349 Template Method............................................ .360 visitor........ 366 Discussion of Behavioral Patterns................................. 382 6conclugion...................................................... 388 6.1 What to Expect from Design Patterns................... 388 6.2 A Brief Hstoy… 39 6.3 The Pattern community........................................393 6.4 An Invitation.,...,....:.............395 6.5 A Parting Thought...............................................396 aG10aay 397 B Gudde to Notation ................................................ B.1 class Diagram....................................................404 B.2 object Diagram.................................................406 B.3 Interaction Diagram. .....407 c Foundation classes................................409 C.2 Iterator........................................................412 C.3 ListIterator..................................................413
Design Patterns: Elements of Reusable Object-Oriented Software 3 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
Design Patterns:Elemente of Rousable Object-Orionted Software c.4p01nt..413 C.5 Bect 414 Bibliography..................................................................416
Design Patterns: Elements of Reusable Object-Oriented Software 4 C.4 Point ......................................................... 413 C.5 Rect .......................................................... 414 Bibliography ....................................................... 416
Design Patterns:Elements of Rousable Object-Oriented Software Preface to CD As we were writing Design Patterns,we knew the patterns we weredescribing had value because they had proven themselves in manydifferent contexts.Our hope was that other software engineers wouldbenefit from these patterns as much as we had. Now,three years after its debut,we find ourselves both grateful andthrilled by how the book has been received.Lots of people use it.Many tell us the patterns have helped them desigr and build bettersystems.Many others have been inspired to write their own patterns,and the pool of patterns is growing.And many have commented on whatmight be improved about the book and what they would like to see inthe future. A recurring comment in all the feedback has been how well-suited thebook is to hypertext.There are numerous crosseferences,andchasing referencesis something a computer can do very well.Sincemuch of the software development process takes place on computers,itwould be natural to have a book like ours as an on-line resource.Observations like these got us excited about the potential of thismedium.So when Mike Hendrickson approached us about turning the bookinto a CD-ROM,we jumped at the chance. two years and several egabytes oflater,ve're delighted thatyou can finally obtain this edition,the Design Patterns CD,and put its unique capabilities to work.Now you can access a patternfrom 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 bookhandy without lugging an extra two pounds of paper Hypertext is a relatively new publishing venue,one we arelearning to use just like everyone else.If you have ideas on howto improve this edition,please send them todesign-patterns-cdecs.uiuc.edu.If you have questions or suggestions concerning the patternsthemselves,send them to thegang-of-4-patternsecs.uluc.edumailing list.(To subscribe,send e-mail to gang-of-4-patterns@cs.uiuc.eduwith the subject"su e".)This list as quite a few readers,and many of them can answer questions as well as we can-andusually a lot faster!Also,be sure to check out thepatterns Home Page athttp://hillside.net/patterna/.There you'll find other books and mailing lista on patterns,notto mention conference information and patterns published on-line. cecand eaon vork ee ison-Wesley for theiron-going encourageme and support.Jeft Helgesen,Jason Jones,andDaniel Savarese garner many thanks 5
Design Patterns: Elements of Reusable Object-Oriented Software 5 Preface to CD As we were writing Design Patterns, we knew the patterns we weredescribing had value because they had proven themselves in manydifferent contexts. Our hope was that other software engineers wouldbenefit from these patterns as much as we had. Now, three years after its debut, we find ourselves both grateful andthrilled by how the book has been received. Lots of people use it.Many tell us the patterns have helped them design and build bettersystems. Many others have been inspired to write their own patterns,and the pool of patterns is growing. And many have commented on whatmight be improved about the book and what they would like to see inthe future. A recurring comment in all the feedback has been how well-suited thebook is to hypertext. There are numerous cross-references, andchasing references is something a computer can do very well. Sincemuch of the software development process takes place on computers, itwould be natural to have a book like ours as an on-line resource.Observations like these got us excited about the potential of thismedium. So when Mike Hendrickson approached us about turning the bookinto a CD-ROM, we jumped at the chance. Two years and several megabytes of e-mail later, we're delighted thatyou can finally obtain this edition, the Design Patterns CD,and put its unique capabilities to work. Now you can access a patternfrom 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 bookhandy without lugging an extra two pounds of paper. Hypertext is a relatively new publishing venue, one we arelearning to use just like everyone else. If you have ideas on howto improve this edition, please send them todesign-patterns-cd@cs.uiuc.edu.If you have questions or suggestions concerning the patternsthemselves, send them to thegang-of-4-patterns@cs.uiuc.edumailing list. (To subscribe, send e-mail to gang-of-4-patterns@cs.uiuc.eduwith the subject "subscribe".) This list has quite a few readers, and many of them can answer questions as well as we can—andusually a lot faster! Also, be sure to check out thePatterns Home Page athttp://hillside.net/patterns/.There you'll find other books and mailing lists on patterns, notto mention conference information and patterns published on-line. This CD entailed considerable design and implementation work. We areindebted to Mike Hendrickson and the team at Addison-Wesley for theiron-going encouragement and support. Jeff Helgesen, Jason Jones, andDaniel Savarese garner many thanks
Design Patterns:Elemente of Rousable Object-Oriented Software for their development effort andfor patience despite what must appear to have been our insatiableappetite for revision.A special acknowledgment is due I8M Resea h,which contir to underwrite much of this activity. We also thankthe reviewers,including Robert Brunner,Sandeep Dani,Bob Koss,ScottMeyers,Stefan Schulz,and the Patterns Discussion Group at theUniversity of Illinois Urbana-Champaign.Their advice led to at leastone major redesign and several minor one8】 Finally,we thank all who have taken time to comment on DesignPatterns.Your feed nvaluable to us as we striveto better our understanding and presentation of this material Zurich,Switzerland E.G. Sydney,Australia R.H. Urbana,Illinois R.J. Hawthorne,New York J.V. August 1997 6
Design Patterns: Elements of Reusable Object-Oriented Software 6 for their development effort andfor patience despite what must appear to have been our insatiableappetite for revision. A special acknowledgment is due IBM Research,which continues to underwrite much of this activity. We also thankthe reviewers, including Robert Brunner, Sandeep Dani, Bob Koss, ScottMeyers, Stefan Schulz, and the Patterns Discussion Group at theUniversity of Illinois Urbana-Champaign. Their advice led to at leastone major redesign and several minor ones. Finally, we thank all who have taken time to comment on DesignPatterns. Your feedback has been invaluable to us as we striveto better our understanding and presentation of this material. Zurich, Switzerland E.G. Sydney, Australia R.H. Urbana, Illinois R.J. Hawthorne, New York J.V. August 1997