欢迎来到天天文库
浏览记录
ID:27770986
大小:161.50 KB
页数:9页
时间:2018-12-06
《10个经典设计模式定义及图解》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、《10个经典设计模式——定义及图解》1、TheStrategyPatterndefinesafamilyofalgorithms,encapsulateseachone,andmakestheminterchangeable.Strategyletsthealgorithmvaryindependentlyfromclientsthatuseit.2、TheObserverPatterndefinesaone-to-manyrelationshipbetweenobjectssothatwhenoneobjectcha
2、ngesstate,allofitsdependentsarenotifiedandupdatedautomatically.Eachsubjectcanhavemanyobserversupdate()willbecalledwhentheSubject’sstatechanges.ThesemethodswillbecalledWhentoregisterasobservers,removethemselvesfromBeingobservers,orupdatealltheobserverswheneversta
3、techanges.Forexample:第9页(共9页)3、TheDecoratorPatternattachesadditionalresponsibilitiestoanobjectdynamically.Decoratorsprovideaflexiblealternativetosubclassingforextendingfunctionality.Forexample:第9页(共9页)4、TheFactoryMethodPatterndefinesaninterfaceforcreatinganobjec
4、t,butletssubclassesdecidewhichclasstoinstantiate.FactoryMethodletsaclassdeferinstantiationtosubclasses.Forexample:第9页(共9页)5、TheSingletonPatternensuresaclasshasonlyoneinstance,andprovidesaglobalpointofaccesstoit.HoldsouroneandonlyinstanceofSingleton.Youcanconveni
5、entlyaccessthemethodanywhere?6、TheCommandPatternencapsulatesarequestasanobject,therebylettingyouparameterizeotherobjectswithdifferentrequests,queueorlogrequests,andsupportundoableoperations.第9页(共9页)publicvoidexecute(){receiver.action();}Forexample:7、TheAdapterPa
6、tternconvertstheinterfaceofaclassintoanotherinterfacetheclientsexpect.Adapterletsclassesworktogetherthatcouldn’totherwisebecauseofincompatibleinterfaces.第9页(共9页)Forexample:8、TheFacadePatternprovidesaunifiedinterfacetoasetofinterfacesinasubsystem.Façadedefinesahi
7、gher-levelinterfacethatmakesthesubsystemeasiertouse.第9页(共9页)Forexample:9、TheTemplateMethodPatterndefinestheskeletonofaalgorithminamethod,deferringsomestepstosubclasses.TemplateMethodletssubclassesredefinecertainstepsofanalgorithmwithoutchangingthealgorithm’sstru
8、cture.第9页(共9页)primitiveOperation1();primitiveOperation2();Forexample:10、TheStatePatternallowsanobjecttoalteritsbehaviorwhenitsinternalstatechanges.第9页(共9页)state.handl
此文档下载收益归作者所有