Table Of Content(cid:2) (cid:2)
“Driver-Kak” — 2014/12/4 — 15:01 — page ii — #2
(cid:2) (cid:2)
(cid:2) (cid:2)
(cid:2) (cid:2)
(cid:2) (cid:2)
“Driver-Kak” — 2014/12/4 — 15:01 — page i — #1
(cid:2) (cid:2)
DESIGNING WITH OBJECTS
(cid:2) (cid:2)
(cid:2) (cid:2)
(cid:2) (cid:2)
“Driver-Kak” — 2014/12/4 — 15:01 — page ii — #2
(cid:2) (cid:2)
(cid:2) (cid:2)
(cid:2) (cid:2)
(cid:2) (cid:2)
“Driver-Kak” — 2014/12/4 — 15:01 — page iii — #3
(cid:2) (cid:2)
DESIGNING WITH
OBJECTS
Object-Oriented Design Patterns
Explained with Stories from Harry Potter
AVINASHC.KAK
PurdueUniversity
(cid:2) (cid:2)
(cid:2) (cid:2)
(cid:2) (cid:2)
“Driver-Kak” — 2014/12/4 — 15:01 — page iv — #4
(cid:2) (cid:2)
CoverImage:Courtesyfromtheauthor
Copyright(cid:2)c 2015byJohnWiley&Sons,Inc.Allrightsreserved
PublishedbyJohnWiley&Sons,Inc.,Hoboken,NewJersey
PublishedsimultaneouslyinCanada
Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmittedinanyformorbyany
means,electronic,mechanical,photocopying,recording,scanning,orotherwise,exceptaspermittedunder
Section107or108ofthe1976UnitedStatesCopyrightAct,withouteitherthepriorwrittenpermissionofthe
Publisher,orauthorizationthroughpaymentoftheappropriateper-copyfeetotheCopyrightClearanceCenter,
Inc.,222RosewoodDrive,Danvers,MA01923,(978)750-8400,fax(978)750-4470,oronthewebat
www.copyright.com.RequeststothePublisherforpermissionshouldbeaddressedtothePermissions
Department,JohnWiley&Sons,Inc.,111RiverStreet,Hoboken,NJ07030,(201)748-6011,fax(201)
748-6008,oronlineathttp://www.wiley.com/go/permission.
LimitofLiability/DisclaimerofWarranty:Whilethepublisherandauthorhaveusedtheirbesteffortsin
preparingthisbook,theymakenorepresentationsorwarrantieswithrespecttotheaccuracyorcompletenessof
thecontentsofthisbookandspecificallydisclaimanyimpliedwarrantiesofmerchantabilityorfitnessfora
particularpurpose.Nowarrantymaybecreatedorextendedbysalesrepresentativesorwrittensalesmaterials.
Theadviceandstrategiescontainedhereinmaynotbesuitableforyoursituation.Youshouldconsultwitha
professionalwhereappropriate.Neitherthepublishernorauthorshallbeliableforanylossofprofitoranyother
commercialdamages,includingbutnotlimitedtospecial,incidental,consequential,orotherdamages.
Forgeneralinformationonourotherproductsandservicesorfortechnicalsupport,pleasecontactourCustomer
CareDepartmentwithintheUnitedStatesat(800)762-2974,outsidetheUnitedStatesat(317)572-3993or
fax(317)572-4002.
Wileyalsopublishesitsbooksinavarietyofelectronicformats.Somecontentthatappearsinprintmaynotbe
availableinelectronicformats.FormoreinformationaboutWileyproducts,visitourwebsiteatwww.wiley.com.
LibraryofCongressCataloging-in-PublicationData:
Kak,AvinashC.
Designingwithobjects:object-orienteddesignpatternsexplainedwithstoriesfromHarryPotter/
AvinashC.Kak.
pages cm
Includesindex.
ISBN978-1-118-58120-9(paperback)
1. Object-orientedprogramming(Computerscience) 2. Rowling,J.K.–Themes,motives.
3. Rowling,J.K.–Characters. 4. Potter,Harry(Fictitiouscharacter) I. Title.
QA76.64.K35482014
(cid:3)
005.117–dc23
2014007132
PrintedintheUnitedStatesofAmerica
10 9 8 7 6 5 4 3 2 1
(cid:2) (cid:2)
(cid:2) (cid:2)
(cid:2) (cid:2)
“Driver-Kak” — 2014/12/1 — 13:26 — page v — #5
(cid:2) (cid:2)
CONTENTS
PREFACE xiv
ACKNOWLEDGMENTS xix
1 WhyLearnDesignPatternsandWhyDoSowithHelpfromHarryPotter? 1
1.1 TheOODesignPatterns“Bible”byGoF 2
1.2 ButWhatHasHarryPotterGottoDowithOODesignPatterns? 4
1.3 IsFamiliaritywithHarryPotteraRequirementforUnderstanding
ThisBook? 6
1.4 HowthePatternExplanationsareOrganized 7
1.5 TheTerminologyofObject-OrientedProgramming 7
1.6 TheUMLNotationUsedintheClassDiagrams 12
1.6.1 AssociationasaRelationshipBetweenClasses 14
1.6.2 AggregationandCompositionasRelationships
BetweenClasses 15
1.6.3 RepresentingAttributes 16
1.6.4 RepresentingOperations 17
I CREATIONALPATTERNS 19
2 AbstractFactory 21
2.1 TheConceptofaFactoryinSoftware 21
2.2 IntentandApplicability 22
2.3 IntroductiontotheAbstractFactoryPattern 22
2.4 TheAbstractFactoryPatterninReal-WorldApplications 23
2.5 HarryPotterStoryUsedtoIllustratetheAbstractFactoryPattern 23
2.6 ATopLevelViewofthePatternDemonstration 25
(cid:2) (cid:2)
(cid:2) (cid:2)
(cid:2) (cid:2)
“Driver-Kak” — 2014/12/1 — 13:26 — page vi — #6
(cid:2) (cid:2)
vi CONTENTS
2.7 TheAbstractClassFactory 27
2.8 TheHelperClassFactoryStore 27
2.9 TheAbstractClassEnchanted 30
2.10 TheConcreteClassesforMagicalObjects 31
2.11 TheConcreteFactoryClasses 32
2.12 TheClientClassDiagonAlleyRetailer 34
2.13 PlayingwiththeCode 36
3 Builder 38
3.1 BuildingComplexObjects 38
3.2 IntentandApplicability 39
3.3 IntroductiontotheBuilderPattern 39
3.4 TheBuilderPatterninReal-WorldApplications 41
3.5 HarryPotterStoryUsedtoIllustratetheBuilderPattern 42
3.6 ATop-LevelViewofthePatternDemonstration 43
3.7 TheAbstractClassPotionMaker 45
3.8 TheConcreteExtensionsofPotionMaker 46
3.9 TheDirectorClass 52
3.10 ThePotionClass 54
3.11 TheIngredientClass 55
3.12 ThePotionMakingFeasibilityViolationClass 55
3.13 TheClientClass 55
3.14 PlayingwiththeCode 56
4 FactoryMethod 59
4.1 RevisitingtheConceptofaFactoryinSoftware 59
4.2 IntentandApplicability 60
4.3 IntroductiontotheFactoryMethodPattern 60
4.4 TheFactoryMethodPatterninReal-WorldApplications 61
4.5 HarryPotterStoryUsedtoIllustratetheFactoryMethodPattern 62
4.6 ATopLevelViewofthePatternDemonstration 63
4.7 TheEnchantedClassHierarchy 65
4.8 TheArtifactFactoryClassHierarchyandtheFactory
MethodsContainedTherein 68
4.9 TheClientClass 70
4.10 PlayingwiththeCode 71
5 Prototype 73
5.1 WhyNotMakeNewObjectsbyCopyingOldObjects? 73
5.2 IntentandApplicability 74
5.3 IntroductiontothePrototypePattern 74
5.4 ThePrototypePatterninReal-WorldApplications 77
5.5 HarryPotterStoryUsedtoIllustrateThePrototypePattern 78
5.6 ATopLevelViewofthePatternDemonstration 79
5.7 TheDragonClass 81
5.8 ThePrototypeManagerAndDuplicatorClass 84
(cid:2) (cid:2)
(cid:2) (cid:2)
(cid:2) (cid:2)
“Driver-Kak” — 2014/12/1 — 13:26 — page vii — #7
(cid:2) (cid:2)
CONTENTS vii
5.9 TheDragonAficionadoClass 87
5.10 TheUnknownDragonExceptionClass 88
5.11 PlayingwiththeCode 88
6 Singleton 90
6.1 SingularObjects 90
6.2 IntentandApplicability 90
6.3 IntroductiontotheSingletonPattern 91
6.4 TheSingletonPatterninReal-WorldApplications 91
6.5 HarryPotterStoryUsedtoIllustratetheSingletonPattern 92
6.6 ATopLevelViewofthePatternDemonstration 92
6.7 TheMinisterForMagicClass 93
6.8 TheTestSingletonClass 94
6.9 VariationsontheSingletonPattern 95
6.10 PlayingwiththeCode 97
II STRUCTURALPATTERNS 99
7 Adapter 101
7.1 GettingThingstoWorkTogether 101
7.2 IntentandApplicability 102
7.3 IntroductiontotheAdapterPattern 102
7.4 TheAdapterPatterninReal-WorldApplications 103
7.5 HarryPotterStoryUsedtoIllustratetheAdapterPattern 104
7.6 ATopLevelViewofthePatternDemonstration 104
7.7 TheTargetInterface:TeachingDADA 106
7.8 TheTeacherForDADAClass 107
7.9 TheAdapterForSafeTeachingClass 108
7.10 TheClientClassSchoolOfMagic 110
7.11 ObjectAdapter 111
7.12 PluggableAdapter 113
7.13 PlayingwiththeCode 119
8 Bridge 122
8.1 ConceptsandTheirImplementation 122
8.2 IntentandApplicability 123
8.3 IntroductiontotheBridgePattern 123
8.4 TheBridgePatterninReal-WorldApplications 126
8.5 HarryPotterStoryUsedtoIllustratetheBridgePattern 127
8.6 ATopLevelViewofthePatternDemonstration 130
8.7 TheHumanoidClass 130
8.8 TheDementor,Goblin,andHouseElfClasses 132
8.9 TheHumanoid ImpClass 134
8.10 ImplementationClassesfortheDementor,Goblin,
andHouseElfClasses 137
(cid:2) (cid:2)
(cid:2) (cid:2)
(cid:2) (cid:2)
“Driver-Kak” — 2014/12/1 — 13:26 — page viii — #8
(cid:2) (cid:2)
viii CONTENTS
8.11 TheClientClassMinistryOfMagic 139
8.12 PlayingwiththeCode 140
9 Composite 142
9.1 RelationshipsThatLoopBack 142
9.2 IntentandApplicability 143
9.3 IntroductiontotheCompositePattern 144
9.4 TheCompositePatterninReal-WorldApplications 145
9.5 HarryPotterStoryUsedtoIllustratetheCompositePattern 146
9.6 ATopLevelViewofthePatternDemonstration 147
9.7 TheWizardTraitsInterface 148
9.8 TheWizardClass 149
9.9 TheAurorClass 152
9.10 TheObliviatorClass 153
9.11 TheDepartmentHeadClass 155
9.12 TheMinisterforMagicClass 156
9.13 TheClientClassTest 158
9.14 PlayingwiththeCode 159
10 Decorator 161
10.1 OnionasaMetaphor 161
10.2 IntentandApplicability 162
10.3 IntroductiontotheDecoratorPattern 162
10.4 TheDecoratorPatterninReal-WorldApplications 164
10.5 HarryPotterStoryUsedtoIllustratetheDecoratorPattern 165
10.6 ATopLevelViewofthePatternDemonstration 168
10.7 TheCoreMessageDeliveryClassClass 170
10.8 TheDecoratorClasses 170
10.9 TheClientClassTest 172
10.10 PlayingwiththeCode 173
11 Facade 175
11.1 HidingComplexity 175
11.2 IntentandApplicability 175
11.3 IntroductiontotheFacadePattern 176
11.4 TheFacadePatterninReal-WorldApplications 177
11.5 HarryPotterStoryUsedtoIllustratetheFacadePattern 177
11.6 ATopLevelViewofthePatternDemonstration 179
11.7 TheAbstractRootClassFacade 181
11.8 TheNetworkClass 183
11.9 TheNode,Link,andPathClasses 193
11.10 TheThreeConcreteFacadeClasses 198
11.11 TestingtheDemonstrationCode 203
11.12 PlayingwiththeCode 207
(cid:2) (cid:2)
(cid:2) (cid:2)