You are on page 1of 304
Smalltalk, Objects, and Design Chamond Liu toExcel San Jose New Yark Shanghai Smalltalk, Objects, and Design Reprinted from the original 1996 edition with corrections: rights reserved. Copyright © 1999 ly Chamond Lit No part of this book may be epproduced or transmiced in any form.or by any eneans, .gtaphic, electronic, of mechanieal, including photocopying, recoeding, taping. of by any information storage ae retrieval system, without permission in writing from the publisher. “The author and the publisher of this hook make no warranties of any kind, expressed ‘or implied, with regard to che information contained in this book. The author and publisher shall not be liable in any event For any loss oF damages caused by, oF atising out of, the use of information contained in this book, All products mentioned in this book are ccadematks or registered trademarks of their respective holders. Use ofa term in this book should nat be regarded as affcering the validity of any trademark or service mark. Published by toExcel For information address: ToExcel 165 West 95th Street, Suite BAN New York, NY 10025 wwmsreercel.com ISBN 1-58348-490-6 Printed in the United States of Ameties “My good friend, every profession requires effort and devotion and practice.” —advice to the young Perceval [de Troyes 1190] Contents Acknowledgments xi Preface xiii 1 Objects 1 1d 12 13 14 Ls 16 1? Objects 1 Examples of Smalltalk messages (telegrams) 5 Pitfall: the effect versus the return 6 Why objects matter 7 Recap: objects & Exercise: warmup (the image) 9° Commentary: perspectives on objects 2 Classes and inheritance 11 al 22 23 24 25 2.6 2? 28 29 2.10 2 212 213 Classes 11 The word “class” 13 Inheritance 13 Terminology 18 Exercise: hierarchies 21 Solution and discussion: Aggregation hierarchies 21 inheritance 22 Syraxes for inheritance 23 Example: inheritance in Smalltalk 24 ise: building aclass hierarchy 26 Commentary: what is object-oriented programming? 27 Commentary: other languages 29 Commentary: history 30 3 Smalltalk introduction 32 31 32 33 3A 35 3.6 a7 3B 3a Elementary objects 32 Messages and their precedences 33 Pitfalls: basic Smalltalk gotchas 34 Examples 36 Exercise: a hypothetical method 37 Solution and discussion 38 Kinds of variables 39 Pitfall: variables # objects 40 Classes are objects 40 3.10 Control flow 42 3.11 Commentary: metackasses 43 4 Exercises—Foundations 44 41 42 43 4A 45 Precautions 44 Finding things in Smallualk 46 Elements of Smalltalk 48 Smalltalk’s debugger 57 Provecations 59 5 Abstract classes 61 Exercise in object-oriented design 62 Solution and discussion 62 Pure virtual (subclassResponsibility) methods 64 Exercise: discovering pure virtuals 65 Solution and discussion 65 Ensuring no instances 65 Conerete behavior in abstract classes 67 Summary: methods in abstract classes 68 6 Containers and other indispensable ideas 69 6.1 62 63 64 65 6.6 67 68 69 Heterogeneity and hamogencity 70 Exercise: heterogencity and homogencity 71 Exercise: dictionaries 72 Exercise: identity dictionaries 75 Overriding equality 75 Excreise: anomaly of the disappearing clement 76 Exercise: excursion into Streams 77 6.10 Containers versus aggregations 78 G.11 Shallow and deep copying 79 6.12 Commentai value and reference semantics 80 6.13 Commentary: containers in C++ 81 7 CRCeards 82 Design exercise 83 Solution and discussion 84 Common questions and answers 87 Commentary: analysis, design, and implementation 88 8 Exercises—Implementing a design 91 81 83 84 85 86 87 88 89 Creare the classes 91 Atestease 91 Write “new” methods 92 Write instance methods 92 ‘Test your solution 93 Engineering discipline 94 A minor variation 94 “Private” methods 94 Commentary: getters and setters 95 8.10 Summary % 9 When (not) to inherit 97 Historical background 97 Inverting hierarchies 98 Buy or inherit? 99 Exercise 100 Solution and discussion 100 Conclusions 102 Commentary: multiple inheritance 103 10 Use cases and dynamic relationships 105 10.1 10.2 10.3 0.4 10.5 10.6 10.7 Interaction diagrams 105 Exercise 108 Solution and discussion 109 Use cases and interaction diagrams in analysis and design 110 Limitations 112 Summary 113 Commentary: historical note 114 HL The venerable model-view-controller 115 Mal 2 3 Model-view-controller example 115 Exercise 118, How MVC works 119 114 Exercise: the original dependency mechanism 121 1.5 MVC: benefits and difficulties 122 11.6 What's become of MVC? 124 12 Building windows 127 12.1 What you need to know about Motif 128 12.2 Widget resources 130 123. Excursion: pool dictionaries 131 124 Exercise: afi window 133 12.5 Exercise: a window for the account balance 134 12.6 Exercise: a window far the sransaction log 135 12.7 Exercise: a window containing both widgets 136 12.8 Assessment: building windows 136 12,9 Callbacks and events 137 12.10 Preparation 138 12.11 Exercise: mouse event handling 138 12.12 Challenging exereise: dynamic updates 139 12.13 Summary 140 13 Designing the UI: a brief tour 142 131 User interfaces 142 13.2 Elementary examples 143 13.3 Coherent conceptual models 144 134 Metaphor 146 13.5 Magic 148 13.6 Exercise: design a user interface 151 13.7 Discussion of your results 152 13.8 Isomorphism 155 139 Summary 156 14 Polymorphism 158 14.1 Dynamic binding 158 14,2 Dynamic binding enables...polymorphism 159: 14,3 Aword on terminology 161 144 Exercise: polymorphism 162 14,5. Solutions and discussion 162 14.6 Exercise: Smalltalles Pless-ness 163 4.7 Solution 163 14.8) Summary tip 165 14,9 Commentary: performance 165 14.10 Commentary: Smalltalk, C++, and type-checking 166 the tomato in the mailbox 169 14,12 Commentary: multi-methods 170 15 Practicing polymorphism 173 15.1 15.2 5.3 15.4 15.5 15.6 Design exercise |: a shape editor 173 Solution and discussion 174 Design exercise II: undo and redo 176, Solution and discussion 177 Impkmenting undo 179 Summary 180 16 How object-oriented languages work 181 16.1 162 16.3 164 165 166 167 16.8 Virtual machines 181 Method lookup 183 Memory management: a brief history of garbage collection 187 The irony of garbage collection 191 Commentary: why not garbage collect C++? 192 Smalltalk deviates from uniformity 193 Exercises 195 Summary 197 17 Tivo kinds of inheritance 198 Beauty and the beast 198 Why types matter: polymorphism 200 Commentary: an aside on subsets 201 Commentary: what docs “consistency” mean? 201 Consistency and Smallealk 207 Exercise: Smalltall’s container “types” 208 Solution and discussion 208 Exercise: Smalltalk’s container “classes” 210 Solution and discussion 210 17.10 Summary 211 17.11 Commentary: standardizing Smalltalk 212 18 Design patterns 213 18.1 18.2 18.3 184 18.5 18.6 Notation 214 Smart container (aka collection-worker) 215. Reification 216 Command 217 Factory method 219 Objects from records 220 Proxies and ghosts, 1 224 Proxies and ghosts, Il 226 Dependency 228 Solitaire (aka singleton) 230 Duet (aka pas de deux, double dispatch) 231 Lawyer (aka object handler) 233 13 Compesite 235 14 Visitor 236 13 Conclusion 239 18.16 Commentary: history 240 19. Frameworks (one bearty example) 242 19.1 Problems 243 Mater 244 Searching (fikering) 248 Updating 251 Summary 252 Commentary: varieties of brokers 254 Commentary: buying outdoes inheriting (sometimes) 255 20 Metaclasses 257 20.1 Facts about metaclasics 257 20.2 Inheritance 258 20.3 Method new 259 204 ‘The full picture 260 20.5 Recapitulation 262 20,6 Exercises 263 21 Why developing software is still hard 264 21.1 Misconceptions 264 21.2 Where projects go awry: borders 267 21.3. Characteristics of succesful projects 269 21.4 An optimistic conclusion 272 Appendix: Some differences between dialects 275 References 277 Index 283 Acknowledgments ‘This book derives from two activities—developing and teaching courses, and develop- ing software. But the real sources are the people I've encountered along the way. Many of them have profoundly shaped my thinking about software—Dave Collins, Peter Deutsch, Amarjcet Garewal, Steve Goetze, Ralph Johnson, Doug Lea, Bertrand Meyer, Tom Morgan, Dave Thomas, Rebecca Wirfs-Brock, and Kirk Wolf—and many others have left'smaller but chaotically important impressions: Bruce Anderson, Marilyn Bates, Katherine Betz, Desmond D'Souza, Phil Hartley, Richard Helm, Felix Laura, and John Vlissides. From here the list is too long to enumerate, for it includes individuals whom I sweated over their businesses’ real object-oriented design and. programming problems, plus all the students and instructors through the years from whom I learned about teaching objects and Smalltalk and C++. 1 am grateful co the many people who were kind enough to share their opinions on draft manuscripts. Bruce Anderson assailed. hackneyed expressions and examples; Kent Beck made me rethink my pedagogical approach; Katherine Betz streamlined discussions; Michele Choate caught stylistic slips; Eric Clayberg taught me new things: about Smalltalk; Dave Collins, a closet historian, set chronologies straight; Ken Coo- per pointed out awkward transitions; Lisa Goetze suggested improvernents to the exer- cises; Steve Goetze was a sounding board for impetuous ideas and, sensitive to the zeitgeist, warned me off gratuitous soapboxes; Ralph Johnson's enthusiasm and tact kept me going when there was still no end in sight; Doug Lea urged technical respect ability where there was none; Ruth Liu distinguished whar I actually said from what | meant (0 say; ‘Tom Morgan flushed our structural and conceptual flaws; Larry Smith was the conscience of the IBM Smalltalk product; Dave Thomas alerted me to trends from the ANSI standardization

You might also like