资源描述:
《holub on patterns_learning design patterns by looking at code》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、Chapter1:Preliminaries—Os(OO)andDesignPatterns101NormallyabookofthissortwouldstartwithaquotefromChristopherAlexander,thearchitect(ofbuildings)whocameupwiththenotionofadesignpattern.I'vefoundthatthoughAlexanderisabrilliantmanwhowriteswonderfulbooks,hisprosecanbe
2、abitopaqueattimes,soI'llskipthemandatoryquote.Hisideaslaunchedtheentiredesign-patternmovement,however.Similarly,theseminalbookondesignpatternsinsoftwareisGamma,Helm,Johnson,andVlissides'sDesignPatterns:ElementsofReusableObject-OrientedSoftware(Addison-Wesley,19
3、95).(ThefourauthorsarejokinglycalledtheGangofFourbymostworkingdesigners.)Mybookwouldn'texistiftheGang-of-Fourbookhadn'tbeenwritten,andI(andOOprogrammersingeneral)oweanenormousdebtofgratitudetotheauthors.Nonetheless,theGang-of-Fourbookisaformalacademicpresentati
4、onofpatterns,andmostbeginnersfindittoodensetopenetrate.Attheriskoflosingsomeacademicprecision,I'lltakeakinderandgentlerapproach.Patternsvs.IdiomsLet'sstartexploringthenotionofapatternbydiscussingsimpleprogrammingidioms.Manydesignpatternsareusedsocommonlythat,in
5、manyprogrammers'minds,theyceasetobepatternsatallbutareidiomsofthelanguage.Thatis,youdon'tthinkofthesepatternsasanythingspecial—they'rejust"howthingsaredone."Somepeopledistinguishbetweenpatternsandidiomsbasedonusage(forexample,apatternisrepresentedinaformalway,a
6、ndanidiomisn't).Idon'tseeadistinction,however.Anidiomisjustapattern,theuseofwhichhasbecomecommonplace.Derivationisagreatexampleoftheevolutionofpatterntoidiom.Backintheearly1980swhenCwasking,derivationwasadesignpattern.Youcanfindseveralexamplesofan"extends"relat
7、ionshipinC.Forexample,thestandardimplementationofmalloc()usesaheader(thebaseclass)that'sextendedtocreateanotherstruct(thederivedclass),whicheffectivelyinheritsthefree()methodfromthebaseclass.AbstractfunctionswerealsopartoftheDerivationpattern.Itwascommonplacein
8、Ctopassaroundtablesoffunctionpointers,initializeddifferentlyfordifferent"classes."ThisisexactlyhowC++implementsbothabstractmethodsandinterfaceinheritance,butbackintheCworld,