windows编程潘爱民com讲座续介绍一些重要的模式_第1页
windows编程潘爱民com讲座续介绍一些重要的模式_第2页
windows编程潘爱民com讲座续介绍一些重要的模式_第3页
windows编程潘爱民com讲座续介绍一些重要的模式_第4页
windows编程潘爱民com讲座续介绍一些重要的模式_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

软件设计模式(二软件设计模式(二内复习复习:patternDesignpatternsrepresentsolutionstoproblemsthatarisewhendevelo softwarewithinaparticularcontext关于patternpatternpattern复习:如何描述一个模Designpatternname,AliasesorAlsoKnownProblem,IntentorContext,RelatedExamples,Known复习:creationalFactory本质:用一个virtualmethod完成创建过

Structural一个product族的factorymethod构成了一个factory通过product原型来构造product,Clone+prototype通过一个构造算法和builder接口把构造过程与客 单实例类型,如何构造这单个实例?如 这单个实例把对象的获取过程与客

**77Adapter(一Converttheinterfaceofaclassintoanotherinterfacesexpect.Adapterletsclassesworktogethercouldn'totherwisebecause patibleSometimesatoolkitclassthat'sdesignedforreuseisn'treusableonlybecauseitsinterfacedoesn'tmatchthe-specificinterfaceanapplicationrequires.Adapter模式(二youwanttouseanexistingclass,anditsinterfacedoesnotmatchtheoneyouneed.youwanttocreateareusableclassthatcooperateswithunrelatedorunforeseenclasses,thatis,classesthatdon'tnecessarilyhavecompatibleinterfaces.(objectadapteronly)youneedtouseseveralexistingsubclasses,butit'simpracticaltoadapttheirinterfacebysubclassingeveryone.Anobjectadaptercanadapttheinterfaceofitsparentclass.Adapter模式Adapter模式(三Adapter模式(三–、Target、Adaptee、classadapter——objectadapter——

Adapter模式(四

Adapter模式(五class无法adaptadaptee的子类,但是可以重载adaptee的行object可以adaptadapteeHowmuchadaptingdoesAdapterPluggableUsingtwo-wayadapterstoprovide针对classadapter,用多重继承来实

使用C++继承机制实现class使用内嵌对象技术实现object使 对操作,供客户插入?——hook技术Adapter模式(Adapter模式(六RelatedC++COM中的模式8:Bridge(一Decouplean ionfromitsimplementationsothatthetwocanvaryindependently抽象(接口)与实现都非常。Bridge模Bridge模式(二Applicability:UsetheBridgepattern对一个实现的修改不影响客户(无须重新编译Bridge模式(三 ion,ion,Bridge模式(四二进制兼容对客户隐藏实现细

Bridge模式(五OnlyoneCreatingtherightImplementor如何创建?根据客户环境,或者通过Sharing资源管理 计数技UsingmultipleRelated–Related–Factory可以用来创建和配置Bridge模–与Adapter–handle:文件handle、窗口插StringRep(constcharintcount;char*rep;Counted

class{String(constStringString&operator=(constStringStringRepComposite模式(Composite模式(三–,Component,Leaf,典型的composite对象结99:Composite(一Composeobjectsintotreestructurestorepresentpart-wholehierarchiesCompositelets streatindividualobjectsandcompositionsofobjectsuniformly”递归组Composite模式(二youwant stobeabletoignoretheindividualobjects.swilltreatallobjectsinthecompositestructureuniformly.Composite模式(Composite模式(四objectsandcompositeobjects。定义了包含leafmakesthesimple。客户一致地处理复合对makesiteasiertoaddnewkindsofcomponentscanmakeyourdesignoverlygeneral。使得系统过于Composite模式(五ExplicitparentSharingizingtheComponentDeclaringthechildmanagementShouldComponentimplementalistofChildCachingtoimproveWhoshoulddeleteWhat'sthebestdatastructureforstoringComposite模Composite模式(六Related广泛应用于OO领MFC中的组件层次:ActiveX10:Facade(一Provideaunifiedinterfacetoasetofinterfacesinasubsystem.Facadedefinesahigher-levelinterfacethatmakesthesubsystemeasiertouse.Facade模式(三–Facade模式(三–facade,subsystemApplicability:UsetheFacadepattern FlyWeight模式(三FlyWeight模式(三FlyWeight模式(四Struct(续–,flyweight,concreteFlyweight,–Facade模式(四—— Facade模式(五Related–facade对象的创建:singleton MFCFakeMFCMapMapMapPluginMDINetscape1111:FlyWeight(一Usesharingtosupportlargenumbersoffine-grainedobjectsefficiently.当对象的粒度太小的时候,大量对象将会产生巨大的资源消耗,因此考虑用共享对象(lyegh)来实现逻辑上的大量对象。Flght对象可用于不同的onxt中,本身固有的状态不随onxt发生变化,而其他的状态随onxt而变化FlyWeight模式(二ofthefollowingaretrue:AnapplicationusesalargenumberofStoragecostsarehighbecauseofthesheertyofMostobjectstatecanbemadeManygroupsofobjectsmayberecedbyrelativelyfewsharedobjectsonceextrinsicstateisremoved.Theapplicationdoesn'tdependonobjectidentity.Sinceflyweightobjectsmaybeshared,identitytestswillreturntrueforconceptuallydistinctobjects.FlyWeight模式(FlyWeight模式(五把对象的状态分开:intrinsicand Removingextrinsicstate,尽可能做到实时计算(通过Managingsharedobjects,客户不能直接实例化 Related与Composite可以用flyweight实现State和Strategy用flyweight实现celltearoff“DesignPatternsStructuralStructuralPatternsStructuralPatternsStructuralpatterns小Adapter、bridge、composite用于构造对象组合结

BehavioralChainofResponsibilityInterpreter MediatorMementoStateTemteMethod12:12:Command(一Action,Encapsulatearequestasanobject,therebylettingyou swithdifferentrequests,queueorlogrequests,andsupportundoableoperations.Command模式(二parameterizeobjectsbyanactiontoperformspecify,queue,andexecuterequestsatdifferentsupportsupportloggingchangessothattheycanbereappliedincaseofasystemcrashstructureasystemaroundhigh-leveloperationsbuiltonprimitivesoperations——transactionsCommand模Command模式(三 Command模式(四

Command模式(五Related模式可用来实 mand组为实现Related模式可用来实 mand组为实现o,可以用其他行为模式来管理状态,如模式。 mnd被放到istrylst之前,可以prototype模 自Commandsarefirst-classobjectsTheycanbemanipulatedandextendedlikeanyotherobjectYoucanassemblecommandsintoacompositecommandAnexampleis It'seasytoaddnewCommands,becauseyoudon'thavetochangeexistingclassesHowinligentshouldacommandSupportingundoandAvoidingerroraccumulationintheundoUsingC++tem模式模式 tor(一ProvideawaytoaccesstheelementsofanaggregateobjectsequentiallywithoutexposingitsunderlyingAnaggregateobjectsuchasalistshouldgiveyouawaytoaccessitselementswithoutexposingitsinternalSeparatingthetraversalmechanismfromtheListobjectletsusdefinei torsfordifferenttraversalpolicieswithoutenumeratingthemintheListinterface. tor模式(二 torpatternwhenexposingitsinternalrepresentation.toprovideauniforminterfacefortraversingpolymorphici tor模式(三– tor、 tor模式(四Itsupportsvariationsinthetraversalofan torssimplifytheAggregateMorethanonetraversalcanbependingonanWhocontrolsthe externalitorversusinternaliWhodefinesthetraversal——itor定义算法——itor如何数Howrobustisthe tor模式(五Implementation(续Additional tor基本操作:First,Next,IsDone,andUsingpolymorphic tors torsmayhaveprivileged torsforcomposites——适合于internali Null

常被用 posite模式的复合结Polymorphici torsrelyonfactorymethodstoinstantiatetheappropriateICOMenumerator:connectableobject、C++在STL中,itor是连接algorithm和container14:14:Observer(一Aliases:Dependents,Publish-Defineaone-to-manydependencybetweenobjectssothatwhenoneobjectchangesstate,allitsdependentsarenotifiedandupdatedautomatically. ThekeyobjectsinthispatternaresubjectandAsubjectmayhaveanynumberofdependentAllobserversarenotifiedwheneverthesubjectundergoesachangeinstateObserver模式(二Whenan ionhastwoaspects,onedependentontheother.Encapsulatingtheseaspectsinseparateobjectsletsyouvaryandreusethemindependently.Whenachangetooneobjectrequireschangingandyoudon'tknowhowmanyobjectsneedtobeWhenanobjectshouldbeabletonotifyotherobjectswithoutmakingassumptionsaboutwhotheseobjectsare.Inotherwords,youdon'twanttheseobjectstightlyObserver模式Observer模式(三–Subject、ConcreteSubject、Observer、Observer模式(四

Observer模式(五Observer模式(六Mediator:用Mediator模Observer模式(六Mediator:用Mediator模式封装复杂的更新语COMpropertyCOM+EventSupportforbroadcastUnexpected subjectstotheirObservingmorethanoneWhotriggersthe orMakingsureSubjectstateisself-consistentbeforesubject向observer传递变化信中间插入15:15:Strategy(一Defineafamilyofalgorithms,encapsulateeachone,andmaketheminterchangeable.Strategyletsthealgorithmvaryindependentlyfrom sthatuseit.Strategy模式(二manyrelatedclassesdifferonlyintheiryouneeddifferentvariantsofananalgorithmusesdatathat asmultipleconditionalstatementsinitsStrategy模Strategy模式(三Strategy、ConcreteStrategy、StrategyandContextincttoimplementthechosenAcontextforwardsrequestsfromitsstoitsStrategy模式(四FamiliesofrelatedAnalternativetoStrategieseliminateconditionalsmustbeawareofdifferentCommunicationoverheadbetweenStrategyandIncreasednumberofDefiningtheStrategyandContextStrategiesastemteMakingStrategyobjectsStrategy模Strategy模式(五flyweight:考虑用flyweight模式来实现strategy

16:Visitor(一Representanoperationtobeperformedontheelementsofanobjectstructure.Visitorletsyoudefineanewoperationwithoutchangingtheclassesoftheelementsonwhichit 不 不 piler例 TypeCheckGenerateCodePrettyPrintVariableRefNodeAssignmentNodeVisitVariableRefVisitAssignmentVisitVa

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论