2018年9月21日:开源日报第197期 – 开源工场

AsfarastheowneroftheBaseclassisconcerned,itstillfunctionsasadvertised。

Andalloftheautomatedtestsstillpass。

ButtheownerisoblivioustotheDerivedclass。

AndtheownerofDerivedclassisinforarudeawakening。

NowArrayCountaddAll()callsitsparent’saddAll()whichinternallycallstheadd()whichhasbeenOVERRIDENbytheDerivedclass。

ThiscausesthecounttobeincrementedeachtimetheDerivedclass’sadd()iscalledandthenit’sincrementedAGAINbythenumberofelementsthatwereaddedintheDerivedclass’saddAll()。

IT’SCOUNTEDTWICE。

Ifthiscanhappen,anditdoes,theauthoroftheDerivedclassmustKNOWhowtheBaseclasshasbeenimplemented。

AndtheymustbeinformedabouteverychangeintheBaseclasssinceitcouldbreaktheirDerivedclassinunpredictableways。

Ugh!

ThishugecrackisforeverthreateningthestabilityofpreciousInheritancepillar。

TheFragileBaseClassSolution

OnceagainContainandDelegatetotherescue。

ByusingContainandDelegate,wegofromWhiteBoxprogrammingtoBlackBoxprogramming。

WithWhiteBoxprogramming,wehavetolookattheimplementationofthebaseclass。

WithBlackBoxprogramming,wecanbecompletelyignorantoftheimplementationsincewecannotinjectcodeintotheBaseclassbyoverridingoneofitsfunctions。

WeonlyhavetoconcernourselveswiththeInterface。

Thistrendisdisturbing…

InheritancewassupposedtobeahugewinforReuse。

ObjectOrientedlanguagesdon’tmakeContainandDelegateeasytodo。

TheyweredesignedtomakeInheritanceeasy。

Ifyou’relikeme,you’restartingtowonderaboutthisInheritancething。

Butmoreimportant,thisshouldshakeyourconfidenceinthepowerofClassificationviaHierarchies。

TheHierarchyProblem

EverytimeIstartatanewcompany,IstrugglewiththeproblemwhenI’mcreatingaplacetoputmyCompanyDocuments,e。

g。

theEmployeeHandbook。

DoIcreateafoldercalledDocumentsandthencreateafoldercalledCompanyinthat?

OrdoIcreateafoldercalledCompanyandthencreateafoldercalledDocumentsinthat?

Bothwork。

Butwhichisright?

Whichisbest?

TheideaofCategoricalHierarchieswasthattherewereBaseClasses(parents)thatweremoregeneralandthatDerivedClasses(children)weremorespecializedversionsofthoseclasses。

Andevenmorespecializedaswemakeourwaydowntheinheritancechain。

(SeetheShapeHierarchyabove)

Butifaparentandchildcouldarbitrarilyswitchplaces,thenclearlysomethingiswrongwiththismodel。

TheHierarchySolution

What’swrongis…

CategoricalHierarchiesdon’twork。

Sowhatarehierarchiesgoodfor?

Containment。

Ifyoulookattherealworld,you’llseeContainment(orExclusiveOwnership)Hierarchieseverywhere。

Whatyouwon’tfindisCategoricalHierarchies。

Letthatsinkinforamoment。

TheObjectOrientedParadigmwaspredicatedupontherealworld,onefilledwithObjects。

Butthenitusesabrokenmodel,viz。

CategoricalHierarchies,wherethereisnoreal-worldanalogy。

ButtherealworldisfilledwithContainmentHierarchies。

AgreatexampleofaContainmentHierarchyisyoursocks。

Theyareinasockdrawerwhichiscontainedinonedrawerinyourdresserwhichiscontainedinyourbedroomwhichiscontainedinyourhouse,etc。

DirectoriesonyourharddriveareanotherexampleofaContainmentHierarchy。

Theycontainsfiles。

Sohowdowecategorizethen?

Well,ifyouthinkoftheCompanyDocuments,itprettymuchdoesn’tmatterwhereIputthem。

IcanputtheminafolderofDocumentsorafoldercalledStuff。

ThewayIcategorizeitiswithtags。


Close Menu
bck体育扫码下载_bck体育手机版官网下载_bckbet如何下载