Design Patterns 设计模式.docx
上传人:qw****27 上传时间:2024-09-11 格式:DOCX 页数:36 大小:1.2MB 金币:15 举报 版权申诉
预览加载中,请您耐心等待几秒...

Design Patterns 设计模式.docx

DesignPatterns设计模式.docx

预览

免费试读已结束,剩余 26 页请下载文档后查看

15 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

2012/11/19Monday设计模式分类标准设计模式有两种分类标准,按用途来分:按scope来分,主要分成类模式和对象模式两类。类模式处理类和其子类之间的关系,这种关系一般是通过继承实现的,因此在编译时就已经确定。对象模式处理对象间的关系,这种关系可在运行时修改,更灵活。因为大部分的模式都用到了继承,因此只有少数几个模式被认为是类模式。Classpatternsdealwithrelationshipsbetweenclassesandtheirsubclasses.Theserelationshipsareestablishedthroughinheritance,sotheyarestatic—fixedatcompile-time.Objectpatternsdealwithobjectrelationships,whichcanbechangedatrun-timeandaremoredynamic.Almostallpatternsuseinheritancetosomeextent.Sotheonlypatternslabeled"classpatterns"arethosethatfocusonclassrelationships.NotethatmostpatternsareintheObjectscope.Anobjectpackagesbothdataandtheproceduresthatoperateonthatdata.Theproceduresaretypicallycalledmethodsoroperations.Anobjectperformsanoperationwhenitreceivesarequest(ormessage)fromaclient.Requestsaretheonlywaytogetanobjecttoexecuteanoperation.Operationsaretheonlywaytochangeanobject'sinternaldata.Becauseoftheserestrictions,theobject'sinternalstateissaidtobeencapsulated;itcannotbeaccesseddirectly,anditsrepresentationisinvisiblefromoutsidetheobject.面向对象设计的难点:decomposingasystemintoobjects,可考虑的因素太多,又没有唯一合适的标准。Manyobjectsinadesigncomefromtheanalysismodel.Butobject-orienteddesignsoftenendupwithclassesthathavenocounterpartsintherealworld.Someofthesearelow-levelclasseslikearrays.Othersaremuchhigher-level.Forexample,theComposite(183)patternintroducesanabstractionfortreatingobjectsuniformlythatdoesn'thaveaphysicalcounterpart.Strictmodelingoftherealworldleadstoasystemthatreflectstoday'srealitiesbutnotnecessarilytomorrow's.Theabstractionsthatemergeduringdesignarekeytomakingadesignflexible.Everyoperationdeclaredbyanobjectspecifiestheoperation'sname,theobjectsittakesasparameters,andtheoperation'sreturnvalue.Thisisknownastheoperation'ssignature.Thesetofallsignaturesdefinedbyanobject'soperationsiscalledtheinterfacetotheobject.Atypeisanameusedtodenoteaparticularinterface.Therun-timeassociationofarequesttoanobjectandoneofitsoperationsisknownasdynamicbinding.,dynamicbindingletsyousubstituteobjectsthathaveidentica