ebook img

How to Design Classes. Data: Structure and Organization PDF

666 Pages·2012·2.711 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview How to Design Classes. Data: Structure and Organization

How to Design Classes D : June 15, 2012 RAFT How to Design Classes Data: Structure and Organization Matthias Felleisen Matthew Flatt Robert Bruce Findler Kathryn E. Gray Shriram Krishnamurthi Viera K. Proulx c 2003,2004, 2005,2006,2007,2008Felleisen,Flatt,Findler,Gray, (cid:13) Krishnamurthi,Proulx Howtodesignclass: object-orientedprogrammingandcomputing MatthiasFelleisen,MatthewFlatt,RobertBruceFindler,KathrynE.Gray, ShriramKrishnamurthi,VieraK.Proulx p. cm. Includesindex. ISBN0-262-06218-6 (hc.: alk.paper) 1. ComputerProgramming. 2. Electronicdataprocessing. QA76.6 .H697 2001 005.1’2—dc21 00-048169 CONTENTS v Contents Preface xii Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii I The Varietiesof Data 7 1 PrimitiveFormsofData 8 2 Classes 9 2.1 FingerExercisesonPlainClasses . . . . . . . . . . . . . . . . 15 2.2 DesigningClasses . . . . . . . . . . . . . . . . . . . . . . . . . 18 3 ClassReferences, ObjectContainment 19 3.1 FingerExercisesonObjectContainment . . . . . . . . . . . . 25 3.2 DesigningClassesthatRefertoClasses . . . . . . . . . . . . 26 4 UnionsofClasses 27 4.1 TypesvsClasses . . . . . . . . . . . . . . . . . . . . . . . . . . 30 4.2 FingerExercisesonUnions . . . . . . . . . . . . . . . . . . . 32 4.3 DesigningUnionsofClasses . . . . . . . . . . . . . . . . . . . 35 5 Unions,Self-References andMutualReferences 37 5.1 ContainmentinUnions,Part1 . . . . . . . . . . . . . . . . . 37 5.2 ContainmentinUnions,Part2 . . . . . . . . . . . . . . . . . 44 5.3 FingerExercisesonContainmentinUnions . . . . . . . . . . 49 6 DesigningClassHierarchies 50 6.1 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 6.2 CaseStudy: FightingUFOs . . . . . . . . . . . . . . . . . . . 56 Intermezzo1: Classesand Interfaces 64 VocabularyandGrammar . . . . . . . . . . . . . . . . . . . . . . . 64 Meaning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 SyntaxErrors,TypeErrors,andRun-timeErrors . . . . . . . . . . 69 II Functional Methods 83 vi CONTENTS 8 Expressions1,ComputingwithPrimitiveTypes 83 9 Expressions2,MethodCalls 85 10 MethodsforClasses 87 10.1 DesignsthroughTemplates . . . . . . . . . . . . . . . . . . . 87 10.2 FingerExercises . . . . . . . . . . . . . . . . . . . . . . . . . . 93 10.3 DesigningMethodsforClasses . . . . . . . . . . . . . . . . . 95 10.4 ConditionalComputations . . . . . . . . . . . . . . . . . . . . 96 10.5 Composingmethods . . . . . . . . . . . . . . . . . . . . . . . 104 11 MethodsandObjectContainment 107 11.1 FingerExercises . . . . . . . . . . . . . . . . . . . . . . . . . . 113 11.2 DesigningMethodsforClassesthatContainClasses . . . . . 113 12 MethodsandUnionsofClasses 114 12.1 Example: PlainGeometricShapes. . . . . . . . . . . . . . . . 115 12.2 SignalingErrors . . . . . . . . . . . . . . . . . . . . . . . . . . 127 13 Types,Classes, andHowMethodCallsCompute 128 13.1 MethodDispatch . . . . . . . . . . . . . . . . . . . . . . . . . 128 13.2 TheRoleofTypes . . . . . . . . . . . . . . . . . . . . . . . . . 132 14 MethodsandUnionsofClasses(Continued) 135 14.1 HowLibrariesWork,Part1: DrawingGeometricShapes . . 135 14.2 FingerExercises . . . . . . . . . . . . . . . . . . . . . . . . . . 140 14.3 DesigningMethodsforUnionsofClasses . . . . . . . . . . . 142 15 MethodsandClasseswithMutualReferences 142 15.1 Example: ManagingaRunner’sLogs. . . . . . . . . . . . . . 142 15.2 Example: Sorting . . . . . . . . . . . . . . . . . . . . . . . . . 150 15.3 Example: OverlappingShapes. . . . . . . . . . . . . . . . . . 153 15.4 Example: RiverSystems . . . . . . . . . . . . . . . . . . . . . 163 15.5 FingerExercises . . . . . . . . . . . . . . . . . . . . . . . . . . 171 16 DesigningMethods 173 16.1 TheVarietiesofTemplates . . . . . . . . . . . . . . . . . . . . 174 16.2 WishLists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 16.3 CaseStudy: FightingUFOs,withMethods . . . . . . . . . . 179 CONTENTS vii Intermezzo2: Methods 194 VocabularyandGrammarforMethods . . . . . . . . . . . . . . . . 195 TypeChecking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Meaning: EvaluatingMethodCalls . . . . . . . . . . . . . . . . . . 202 SyntaxErrors,TypeErrors,andRun-timeErrors . . . . . . . . . . 212 III Abstractingwith Classes 221 18 SimilaritiesinClasses 222 18.1 CommonFields,Superclasses . . . . . . . . . . . . . . . . . . 222 18.2 AbstractClasses,AbstractMethods. . . . . . . . . . . . . . . 226 18.3 LiftingMethods,InheritingMethods . . . . . . . . . . . . . . 228 18.4 CreatingaSuperclass,CreatingaUnion . . . . . . . . . . . . 234 18.5 DerivingSubclasses . . . . . . . . . . . . . . . . . . . . . . . . 250 19 DesigningClassHierarchieswithMethods 251 19.1 LocalVariablesandComposition . . . . . . . . . . . . . . . . 252 19.2 AbstractingwithMethods . . . . . . . . . . . . . . . . . . . . 255 19.3 AbstractingwithinUnionsofClasses . . . . . . . . . . . . . . 258 19.4 AbstractingthroughtheCreationofUnions . . . . . . . . . . 261 19.5 DerivingSubclassesfrom“Library”Classes . . . . . . . . . . 265 19.6 CreatingSubclassesforSpecialObjects . . . . . . . . . . . . . 267 19.7 HowLibrariesWork,Part2: DerivingClasses . . . . . . . . . 272 19.8 CaseStudy: FightingUFOs,AlltheWay . . . . . . . . . . . . 277 19.9 MiniProject: Worm . . . . . . . . . . . . . . . . . . . . . . . . 287 20 StateEncapsulationandSelf-Preservation 291 20.1 ThePowerofConstructors . . . . . . . . . . . . . . . . . . . . 292 20.2 OverloadingConstructors . . . . . . . . . . . . . . . . . . . . 296 20.3 EncapsulatingandPrivacy . . . . . . . . . . . . . . . . . . . . 298 20.4 GuidelinesforStateEncapsulation . . . . . . . . . . . . . . . 304 20.5 FingerExercisesonEncapsulation . . . . . . . . . . . . . . . 305 21 ExtensionalEquality,Part1 308 21.1 EqualityforPlainClasses . . . . . . . . . . . . . . . . . . . . 308 21.2 EqualityforInheritance . . . . . . . . . . . . . . . . . . . . . 313 21.3 EqualityforContainmentandUnions . . . . . . . . . . . . . 315 viii CONTENTS Intermezzo3:AbstractClasses, Privacy 322 AbstractClassesandClassExtensions . . . . . . . . . . . . . . . . 322 PrivacyforMethods . . . . . . . . . . . . . . . . . . . . . . . . . . 323 OverloadingConstructorsandMethods . . . . . . . . . . . . . . . 323 IV Circular Objects, ImperativeMethods 327 23 CircularData 328 23.1 DesigningClassesforCircular Objects,Constructors . . . . . 337 23.2 TheTrueNatureofConstructors . . . . . . . . . . . . . . . . 340 23.3 CircularityandEncapsulation . . . . . . . . . . . . . . . . . . 340 23.4 Example: FamilyTrees . . . . . . . . . . . . . . . . . . . . . . 343 24 MethodsonCircularData 348 25 TheStateoftheWorldandHowItChanges 359 26 AssignmentsandChangesintheWorld 361 26.1 Example: DroppingBlocks . . . . . . . . . . . . . . . . . . . . 362 26.2 Example: Accounts . . . . . . . . . . . . . . . . . . . . . . . . 364 26.3 HowLibrariesWork3: AnAlternativeWorld . . . . . . . . . 367 27 DesigningStatefulClasses, ImperativeMethods 373 27.1 WhentoUseStatefulClassesandImperativeMethods . . . 375 27.2 HowtoDesignStatefulClasses,ImperativeMethods . . . . 378 27.3 ImperativeMethodsandTemplates . . . . . . . . . . . . . . 384 27.4 ImperativeMethodsandAbstraction . . . . . . . . . . . . . . 387 27.5 Danger! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 27.6 CaseStudy: MoreonBankAccounts . . . . . . . . . . . . . . 395 27.7 CaseStudyRepeated: AStatefulApproachtoUFOs . . . . . 399 27.8 CaseStudy: ADeckofCards . . . . . . . . . . . . . . . . . . 408 27.8.1 CachingandInvisibleAssignments . . . . . . . . . . 418 27.8.2 MergingListTraversals: SplittingLists . . . . . . . . 420 27.9 Endnote: WheredoWorldsComefrom? Wheredotheygo? 423 28 Equality 434 28.1 ExtensionalEquality,Part2 . . . . . . . . . . . . . . . . . . . 435 28.2 IntensionalEquality . . . . . . . . . . . . . . . . . . . . . . . . 435 28.3 ExtensionalEqualitywithnull . . . . . . . . . . . . . . . . . 440 28.4 ExtensionalEqualitywithCast . . . . . . . . . . . . . . . . . 442 CONTENTS ix 28.5 Danger! ExtensionalEqualityandCycles . . . . . . . . . . . 443 Intermezzo4: Assignments 449 V AbstractingData Representations 452 30 TypesandSimilarities betweenPlainClasses 452 30.1 ClasseswithCommonStructure,DifferentTypes . . . . . . . 453 30.2 AbstractingTypesviaSubtyping . . . . . . . . . . . . . . . . 454 30.3 AbstractingTypesviaGenerics . . . . . . . . . . . . . . . . . 457 31 TypesandSimilarities betweenHierarchies 464 31.1 AbstractingTypesviaSubtyping,Part2 . . . . . . . . . . . . 465 31.2 AbstractingTypesviaSubtypingplusInterfaces . . . . . . . 471 31.3 AbstractingTypesviaGenerics,Part2 . . . . . . . . . . . . . 476 31.4 AbstractingTypesviaGenericsplusInterfaces . . . . . . . . 479 32 DesigningGeneralClassesandFrameworks 484 32.1 SubtypingSummarized . . . . . . . . . . . . . . . . . . . . . 488 32.2 GeneralizingviaSubtyping . . . . . . . . . . . . . . . . . . . 490 32.3 GeneralizingviaGenerics . . . . . . . . . . . . . . . . . . . . 500 32.4 FingerExercises: Sets,Stacks,Queues,andTreesAgain . . . 503 32.5 Errors,alsoknownasRuntimeExceptions. . . . . . . . . . . 508 33 Designing(to)Interfaces 511 33.1 OrganizingPrograms,HidingAuxiliaryMethods . . . . . . 511 33.2 Getters,Predicates,andSetters . . . . . . . . . . . . . . . . . 517 33.3 InterfacesasSpecifications . . . . . . . . . . . . . . . . . . . . 520 34 ExtensibleFrameworks: AbstractingConstructors 526 34.1 DataExtensionsareEasy . . . . . . . . . . . . . . . . . . . . . 528 34.2 FunctionExtension: ADesignthatdoesn’tQuiteWork... . 531 34.3 ... andhowtoFixit(Mostly) . . . . . . . . . . . . . . . . . . 535 34.4 FunctionExtension: Take2 . . . . . . . . . . . . . . . . . . . . 540 34.5 MiniProject: TheTowersofHanoi . . . . . . . . . . . . . . . 544 Intermezzo5: GenericClasses 548 x CONTENTS VI AbstractingData Traversals 551 36 PatternsinTraversals 551 36.1 Example: MenusDesignedfromScratch . . . . . . . . . . . . 553 36.2 Example: MenusDesignedasLists . . . . . . . . . . . . . . . 554 36.3 MethodsasObjects . . . . . . . . . . . . . . . . . . . . . . . . 556 36.4 ListTraversalsandPolymorphicMethods . . . . . . . . . . . 561 36.5 Example: fold. . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 36.6 Example: ArithmeticExpressionsandTraversals . . . . . . . 575 37 DesigningAbstractTraversalMethods 587 37.1 MethodsasObjectsviaSubtyping . . . . . . . . . . . . . . . 587 37.2 MethodsasObjectsviaGenerics . . . . . . . . . . . . . . . . 590 37.3 AbstractingoverMethodCalls,AnonymousInnerClasses . 591 37.4 InnerClasses,AnonymousClasses . . . . . . . . . . . . . . . 594 37.5 VisitorTraversalsandDesigningVisitors . . . . . . . . . . . 596 37.6 FingerExercises: VisitingLists,Trees,Sets . . . . . . . . . . . 608 37.7 ExtendedExercise: GraphsandVisitors . . . . . . . . . . . . 610 37.8 WheretoUseTraversals,orAggregation . . . . . . . . . . . 613 37.9 Object-OrientedandFunctionalProgramming . . . . . . . . 617 38 TraversingwithEffects 619 38.1 AbstractingoverImperativeTraversals: theforEachMethod 619 38.2 UsingtheforEachMethod . . . . . . . . . . . . . . . . . . . . 621 38.3 UsingforEachwithAnonymousClasses . . . . . . . . . . . . 626 38.4 MiniProjects,includingaFinalLookat“WaroftheWorlds” 631 38.5 AbusingtheforEachMethod . . . . . . . . . . . . . . . . . . . 635 39 ExtensibleFrameworkswithVisitors 642 Intermezzo6:GenericMethods, InnerClasses 643 VII Loopsand Arrays 645 41 TheDesignFlaw 645 42 Loops 645 42.1 DesigningLoops . . . . . . . . . . . . . . . . . . . . . . . . . 645 42.2 DesigningNestedLoops . . . . . . . . . . . . . . . . . . . . . 645

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.