Preface v
1 Thinking Object-Oriented 1
1.1 Why Is OOP Popular? 2
1.2 Language and Thought. 2
1.2.1 Eskimos and Snow3
1.2.2 An Example from Computer Languages3
1.2.3 Church's Conjecture and the Whorf Hypothesis * 5
1.3 A New Paradigm6
1.4 A Way of Viewing the World.8
1.4.1 Agents and Communities8
1.4.2 Messages and Methods.9
1.4.3 Responsibilities 11
1.4.4 Classes and Instances.11
1.4.5 Class Hierarchies{Inheritance. 11
1.4.6 Method Binding and Overriding. 14
1.4.7 Summary of Object-Oriented Concepts14
1.5 Computation as Simulation *.15
1.5.1 The Power of Metaphor. 16
1.5.2 Avoiding In nite Regression 18
1.6 A Brief History *18
2 Abstraction 25
2.1 Layers of Abstraction 26
2.2 Other Forms of Abstraction.30
2.2.1 Division into Parts31
2.2.2 Encapsulation and Interchangeability 32
2.2.3 Interface and Implementation. 33
2.2.4 The Service View. 33
2.2.5 Composition34
2.2.6 Layers of Specialization. 35
2.2.7 Patterns. 38
2.3 A Short History of Abstraction Mechanisms*. 39
2.3.1 Assembly Language.39
2.3.2 Procedures. 39
2.3.3 Modules. 41
2.3.4 Abstract Data Types.42
2.3.5 A Service-Centered View43
2.3.6 Messages, Inheritance, and Polymorphism44
3 Object-Oriented Design 49
3.1 Responsibility Implies Noninterference49
3.2 Programming in the Small and in the Large 50
3.3 Why Begin with Behavior?.51
3.4 A Case Study in RDD 52
3.4.1 The Interactive Intelligent Kitchen Helper52
3.4.2 Working through Scenarios 53
3.4.3 Identi cation of Components. 55
3.5 CRC Cards{Recording Responsibility55
3.5.1 Give Components a Physical Representation 56
3.5.2 The What/Who Cycle.56
3.5.3 Documentation 57
3.6 Components and Behavior57
3.6.1 Postponing Decisions.59
3.6.2 Preparing for Change.59
3.6.3 Continuing the Scenario. 60
3.6.4 Interaction Diagrams.61
3.7 Software Components 62
3.7.1 Behavior and State62
3.7.2 Instances and Classes.63
3.7.3 Coupling and Cohesion. 63
3.7.4 Interface and Implementation{Parnas's Principles 64
3.8 Formalize the Interface 65
3.8.1 Coming up with Names. 65
3.9 Designing the Representation.66
3.10 Implementing Components.67
3.11 Integration of Components.68
3.12 Maintenance and Evolution.69
4 Classes and Methods 73
4.1 Encapsulation 73
4.2 Class De nitions74
4.2.1 C++, Java and C#.75
4.2.2 Apple Object Pascal and Delphi Pascal77
4.2.3 Smalltalk77
4.2.4 Other Languages. 79
4.3 Methods.79
4.3.1 Order of Methods in a Class Declaration 82
4.3.2 Constant or Immutable Data Fields 82
4.3.3 Separating De nition and Implementation83
4.4 Variations on Class Themes*.87
4.4.1 Methods without Classes in Oberon 87
4.4.2 Interfaces88
4.4.3 Properties89
4.4.4 Forward De nitions.90
4.4.5 Inner or Nested Classes. 91
4.4.6 Class Data Fields. 94
4.4.7 Classes as Objects96
5 Messages, Instances, and Initialization 101
5.1 Message-Passing Syntax. 101
5.2 Statically and Dynamically Typed Languages. 103
5.3 Accessing the Receiver from Within a Method. 104
5.4 Object Creation. 106
5.4.1 Creation of Arrays of Objects. 107
5.5 Pointers and Memory Allocation108
5.5.1 Memory Recovery109
5.6 Constructors 110
5.6.1 The Orthodox Canonical Class Form*. 114
5.6.2 Constant Values. 115
5.7 Destructors and Finalizers117
5.8 Metaclasses in Smalltalk*119
6 A Case Study: The Eight Queens Puzzle 125
6.1 The Eight-Queens Puzzle125
6.1.1 Creating Objects That Find Their Own Solution 126
6.2 Using Generators127
6.2.1 Initialization127
6.2.2 Finding a Solution129
6.2.3 Advancing to the Next Position130
6.3 The Eight-Queens Puzzle in Several Languages130
6.3.1 The Eight-Queens Puzzle in Object Pascal 131
6.3.2 The Eight-Queens Puzzle in C++.134
6.3.3 The Eight-Queens Puzzle in Java. 136
6.3.4 The Eight-Queens Puzzle in Objective-C139
6.3.5 The Eight-Queens Puzzle in Smalltalk. 141
6.3.6 The Eight-Queens Puzzle in Ruby143
7 A Case Study: A Billiards Game 147
7.1 The Elements of Billiards147
7.2 Graphical Objects. 148
7.2.1 The Wall Graphical Object 149
7.2.2 The Hole Graphical Object 150
7.2.3 The Ball Graphical Object 151
7.3 The Main Program. 155
7.4 Using Inheritance155
8 Inheritance and Substitution 161
8.1 An Intuitive Description of Inheritance161
8.1.1 The Is-a Test162
8.1.2 Reasons to Use Inheritance 162
8.2 Inheritance in Various Languages163
8.3 Subclass, Subtype, and Substitution. 163
8.3.1 Substitution and Strong Typing*. 167
8.4 Overriding and Virtual Methods167
8.5 Interfaces and Abstract Classes. 169
8.6 Forms of Inheritance170
8.6.1 Subclassing for Specialization (Subtyping)170
8.6.2 Subclassing for Speci cation 171
8.6.3 Subclassing for Construction. 171
8.6.4 Subclassing for Generalization172
8.6.5 Subclassing for Extension173
8.6.6 Subclassing for Limitation173
8.6.7 Subclassing for Variance. 173
8.6.8 Subclassing for Combination. 174
8.6.9 Summary of the Forms of Inheritance. 174
8.7 Variations on Inheritance*175
8.7.1 Anonymous Classes in Java 175
8.7.2 Inheritance and Constructors. 176
8.7.3 Virtual Destructors177
8.8 The Bene ts of Inheritance.178
8.8.1 Software Reusability.178
8.8.2 Code Sharing178
8.8.3 Consistency of Interface. 178
8.8.4 Software Components.178
8.8.5 Rapid Prototyping179
8.8.6 Polymorphism and Frameworks179
8.8.7 Information Hiding179
8.9 The Costs of Inheritance. 180
8.9.1 Execution Speed. 180
8.9.2 Program Size180
8.9.3 Message-Passing Overhead 180
8.9.4 Program Complexity.181
9 Case study { A Card Game 185
9.1 The Class PlayingCard 185
9.2 Data and View Classes 187
9.3 The Game. 188
9.4 Card Piles{Inheritance in Action189
9.4.1 The Default Card Pile.191
9.4.2 The Suit Piles 192
9.4.3 The Deck Pile 192
9.4.4 The Discard Pile. 194
9.4.5 The Tableau Piles195
9.5 Playing the Polymorphic Game. 197
9.6 The Graphical User Interface.197
10 Subclasses and Subtypes 205
10.1 Substitutability. 205
10.2 Subtypes. 206
10.3 The Substitutability Paradox.208
10.3.1 Is This a Problem?210
10.4 Subclassing for Construction.210
10.4.1 Private Inheritance in C++*. 212
10.5 Dynamically Typed Languages. 213
10.6 Pre and Post conditions*214
10.7 Re nement Semantics* 214
11 Static and Dynamic Behavior 219
11.1 Static versus Dynamic Typing.219
11.2 Static and Dynamic Classes.221
11.2.1 Run Time Type Determination223
11.2.2 Down Casting (Reverse Polymorphism)223
11.2.3 Run Time Testing without Language Support* . 226
11.2.4 Testing Message Understanding227
11.3 Static versus Dynamic Method Binding. 227
12 Implications of Substitution 233
12.1 Memory Layout. 233
12.1.1 Minimum Static Space Allocation235
12.1.2 Maximum Static Space Allocation237
12.1.3 Dynamic Memory Allocation. 238
12.2 Assignment239
12.2.1 Assignment in C++*.240
12.3 Copies and Clones. 243
12.3.1 Copies in Smalltalk and Objective-C 243
12.3.2 Copy Constructors in C++ 243
12.3.3 Cloning in Java ..244
12.4 Equality.244
12.4.1 Equality and Identity.245
12.4.2 The Paradoxes of Equality Testing246
13 Multiple Inheritance 251
13.1 Inheritance as Categorization.252
13.1.1 Incomparable Complex Numbers. 252
13.2 Problems Arising from Multiple Inheritance 254
13.2.1 Name Ambiguity. 255
13.2.2 Impact on Substitutability257
13.2.3 Rede nition in Eiel*.258
13.2.4 Resolution by Class Ordering in CLOS*259
13.3 Multiple Inheritance of Interfaces261
13.3.1 Mixins in CLOS*. 263
13.4 Inheritance from Common Ancestors. 265
13.4.1 Constructors and Multiple Inheritance*268
13.5 Inner Classes 269
14 Polymorphism and Software Reuse 273
14.1 Polymorphism in Programming Languages273
14.1.1 Many Tools, One Goal.275
14.2 Mechanisms for Software Reuse. 275
14.2.1 Using Composition276
14.2.2 Using Inheritance. 278
14.2.3 Composition and Inheritance Contrasted279
14.3 E ciency and Polymorphism.281
14.4 Will Widespread Software Reuse Become Reality? 282
15 Overloading 285
15.1 Type Signatures and Scopes.285
15.2 Overloading Based on Scopes.287
15.3 Overloading Based on Type Signatures288
15.3.1 Coercion and Conversion291
15.4 Rede nition297
15.5 Polyadicity*299
15.5.1 Optional Parameters.301
15.6 Multi-Methods*. 302
15.6.1 Overloading Based on Values. 304
16 Overriding 307
16.0.2 Overriding in Smalltalk Class Magnitude308
16.1 Notating Overriding309
16.2 Replacement versus Re nement. 309
16.2.1 Replacement in Smalltalk311
16.2.2 Re nement in Beta313
16.2.3 Re nement and the Subclass/Subtype distinction 316
16.2.4 Wrappers in CLOS316
16.3 Deferred Methods317
16.4 Overriding versus Shadowing.318
16.5 Covariance and Contravariance. 321
16.6 Variations on Overriding*326
16.6.1 Final methods in Java.326
16.6.2 Versioning in C#. 327
17 The Polymorphic Variable 331
17.1 Simple Polymorphic Variables.331
17.2 The Receiver Variable 333
17.2.1 The role of the polymorphic variable in frameworks . 334
17.2.2 Endpoint comparisons in Smalltalk336
17.2.3 Self and Super 337
17.3 Downcasting 339
17.4 Pure Polymorphism341
18 Generics 345
18.1 Template Functions345
18.2 Template Classes347
18.2.1 Bounded Genericity*.349
18.3 Inheritance in Template Arguments*. 349
18.3.1 Inheritance and Arrays. 351
18.4 Case Study-Combining Separate Classes. 352
19 Container Classes 359
19.1 Containers in Dynamically Typed Languages. 359
19.1.1 Containers in Smalltalk-80 360
19.2 Containers in Statically-Typed Languages361
19.2.1 The Tension between Typing and Reuse362
19.2.2 Substitution and Downcasting364
19.2.3 Using Substitution and Overriding368
19.2.4 Parametrized Classes.370
19.3 Restricting Element Types*.371
19.4 Element Traveral374
19.4.1 Iterator loops375
19.4.2 The Visitor Approach.377
20 A Case Study: The STL 385
20.1 Iterators.387
20.2 Function Objects388
20.3 Example Program{An Inventory System. 390
20.4 Example Program{Graphs392
20.4.1 Shortest Path Algorithm. 394
20.4.2 Developing the Data Structures394
20.5 A Concordance. 398
20.6 The Future of OOP401
21 Frameworks 403
21.1 Reuse and Specialization. 403
21.1.1 High and low level abstractions406
21.1.2 An Upside Down Library408
21.2 Example Frameworks409
21.2.1 The Java Applet API.409
21.2.2 A Simulation Framework410
21.2.3 An Event-Driven Simulation Framework411
22 An Example Framework: The AWT and Swing 419
22.1 The AWT Class Hierarchy ...419
22.2 The Layout Manager422
22.3 Listeners.424
22.3.1 Adapter Classes. 425
22.4 User Interface Components.426
22.5 Case Study: A Color Display.429
22.6 The Swing component library.432
22.6.1 Import Libraries. 433
22.6.2 Dierent Components.433
22.6.3 Dierent Paint Protocol. 433
22.6.4 Adding Components to a Window434
23 Object Interconnections 435
23.1 Coupling and Cohesion 436
23.1.1 Varieties of Coupling.436
23.1.2 Varieties of Cohesion.439
23.1.3 The Law of Demeter.440
23.1.4 Class-Level versus Object-Level Visibility442
23.1.5 Active Values442
23.2 Subclass Clients and User Clients444
23.3 Control Of Access and Visibility445
23.3.1 Visibility in Smalltalk.445
23.3.2 Visibility in Object Pascal445
23.3.3 Visibility in C++.446
23.3.4 Visibility in Java. 450
23.3.5 Visibility in Objective-C. 452
23.4 Intentional Dependency. 453
24 Design Patterns 457
24.1 Controlling Information Flow.458
24.2 Describing Patterns459
24.3 Iterator.460
24.4 Software Factory461
24.5 Strategy.462
24.6 Singleton. 462
24.7 Composite. 463
24.8 Decorator. 465
24.9 The Double-Dispatching Pattern466
24.10Flyweight. 467
24.11Proxy.468
24.12Facade.469
24.13Observer.469
25 Re ection and Introspection 473
25.1 Mechanisms for Understanding. 473
25.1.1 Class Objects473
25.1.2 The Class Name as String475
25.1.3 Testing the Class of an Object475
25.1.4 Creating an Instance from a Class477
25.1.5 Testing if an Object Understands a Message 478
25.1.6 Class Behavior 478
25.2 Methods as Objects479
25.3 Mechanisms for Modi cation.480
25.3.1 Method Editing in Smalltalk. 480
25.3.2 Dynamic Class Loading in Java481
25.4 Metaclasses483
26 Distributed Objects 487
26.1 Addresses, Ports, and Sockets.488
26.2 A Simple Client/Server Program490
26.3 Multiple Clients. 493
26.4 Transmitting Objects over a Network. 498
26.5 Providing More Complexity.501
27 Implementation 505
27.1 Compilers and Interpreters.505
27.2 The Receiver as Argument506
27.3 Inherited Methods. 507
27.3.1 The Problem of Multiple Inheritance 508
27.3.2 The Slicing Problem.508
27.4 Overridden Methods509
27.4.1 Eliminating Virtual Calls and Inlining. 511
27.5 Name Encoding. 511
27.6 Dispatch Tables. 512
27.6.1 A Method Cache. 513
27.7 Bytecode Interpreters 514
27.8 Just-In-Time Compilation517
A Source for the Eight-Queens Puzzle 521
A.1 Eight-Queens in Apple Object Pascal. 521
A.2 Eight-Queens in C++. 524
A.3 Eight-Queens in Java526
A.4 Eight-Queens in Objective-C.529
A.5 Eight-Queens in Ruby 532
A.6 Eight-Queens in Smalltalk534
B Source for the Billiards Game 537
B.1 The Version without Inheritance537
B.2 The Version with Inheritance.545
C Source for the Solitaire Game 551
Glossary 563
Index 595