23个设计模式C#代码.docx

上传人:p** 文档编号:1006534 上传时间:2024-06-15 格式:DOCX 页数:14 大小:22.81KB
下载 相关 举报
23个设计模式C#代码.docx_第1页
第1页 / 共14页
23个设计模式C#代码.docx_第2页
第2页 / 共14页
23个设计模式C#代码.docx_第3页
第3页 / 共14页
23个设计模式C#代码.docx_第4页
第4页 / 共14页
23个设计模式C#代码.docx_第5页
第5页 / 共14页
23个设计模式C#代码.docx_第6页
第6页 / 共14页
23个设计模式C#代码.docx_第7页
第7页 / 共14页
23个设计模式C#代码.docx_第8页
第8页 / 共14页
23个设计模式C#代码.docx_第9页
第9页 / 共14页
23个设计模式C#代码.docx_第10页
第10页 / 共14页
亲,该文档总共14页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《23个设计模式C#代码.docx》由会员分享,可在线阅读,更多相关《23个设计模式C#代码.docx(14页珍藏版)》请在第壹文秘上搜索。

1、/AbstractFactory/Intent:Provideaninterfaceforcreatingfamiliesofrelatedor/dependentobjectswithoutspecifyingtheirconcreteclasses”./Forfurtherinformation,readDesignPatterns,p87,Gammaetal.,/Addison-Wesley,ISBN:0-201-63361-2*Notes:* Whentheconstructionneededinvolvesmanyobjects,possibleorganised* inmulti-

2、facetedarrangements,theentireconstructioncanbedelegated* toanabstractfactory.Thisexposesstandardisedcreationfunctionality* whichcanbecustomisedinconcreteimplementationtosuityourspecific* needs,andavoidembeddingthisinformationinhigherlevelcode-it* justneedstoknowhowtocalltheabstractfactory.* Inthissa

3、mple,wehaveaframeworkwiththreeabstractoperatingclasses,* calledDPDocument,DPWorkspaceandDPViewandoneabstractconstruction* class,calledDPFactory.napplication-levelclass,calledDPApplication* isresponsibleforconstruction.* Wehaveaseriesofapp1ication-1eve1operatingclassesderivedfromthis* framework-MyDoc

4、ument,MyWorkspaceandMyView.Fordesignreasonswe* assumewewishtoinstantiatethesefrominsideDPApplication.Asthere* aremultipleobjectsneededandtheycouldbearrangedindifferent* lattices,weuseafactory,MyFactory(inourexample,thereareall* simplesiblings),whichiscalledinsideDPApplication.*/namespaceAbstractFact

5、oryDesignPatternusingSystem;/Theseclassescouldbepartofaframework,/whichwewillcallDP/=abstractclassDPDocumentabstractpublicvoidDump();abstractclassDPWorkspaceabstractpublicvoidDump();DPViewpublicvoidDump();abstractclass(abstractabstractclassabstractabstractabstractDPFactorypublicDPDocumentCreateDocum

6、ent();publicDPViewCreateViewO;publicDPWorkspaceCreateWorkspaceO;abstractclassDPApplication(protectedDPDocumentdoc;protectedDPWorkspaceworkspace;protectedDPViewview;publicvoidConstructObjects(DPFactoryfactory)(/Createobjectsasneededdoc=factory.CreateDocumentO;workspace=factory,CreateWorkspaceO;view=f

7、actory.CreateViewO;publicvoidDumpState()(if(doc!-null)doc.Dump();if(workspace!=null)workspace.Dump();if(view!=null)view.DumpO;)/TheseclassescouldbepartofanapplicationclassMyApplication:DPApplicationMyFactorymyFactory=newMyFaCtory();overridepublicvoidDump()(Console.Write1.ine(,MyApplicationexists);pu

8、blicvoidCreateFamilyO(MyFactorymyFactory=newMyFactoryO;ConstructObjects(myFactory);)classMyDocument:DPDocumentpublicMyDocumentO(Console.Write1.ine(,inMyDocumentconstructor);overridepublicvoidDump()(Console.Write1.ine(,MyDocumentexists);)classMyWorkspace:DPWorkspaceConsole.Write1.ine(zzMyWorkspaceexi

9、sts);classMyView:DPViewoverridepublicvoidDump()Console.Write1.ine(,MyViewexists);classMyFactory:DPFactory(overridepublicDPDocumentCreateDocument()(returnnewMyDoCUment();overridepublicDPWorkspaceCreateWorkspace()(returnnewMyWorkspace();)overridepublicDPViewCreateViewOreturnnewMyViewO;IIIIIISummarydes

10、criptionforClient.IllpublicclassClient(publicstaticintMain(stringargs)(MyApplicationmyApplication=newMyApplicationO;myApplication.CreateFamilyO;myApplication.DumpStateO;returnO;/Adapter/Intent:Converttheinterfaceofaclassintoanotherinterface/clientsexpect.Adapterletsclassesworktogetherthatcouldn,t/ot

11、herwisebecauseofincompatibleinterfaces./Forfurtherinformation,readDesignPatterns”,pl39,Gammaetal.,/Addison-Wesley,ISBN:0-201-63361-2*Notes:* Adaptersareoftenusedwhenclientcodeiswrittentoexpectclasses* fromoneframework,anditmeanttoworkwithclassesfromatotally* differentframework.Assumeyoucannotchanget

12、hecodeofeitherframework.* thesolutionisforyoutowriteanadapter,whichappearslikea*nativeclasstoeachframework.* Therearetwodifferenttypesofadapters-classadapterandobject* adapter.Classadaptersarebasedonmultipleinheritance-specifically* theinterfaceofthetargetclassandtheimplementationoftheadaptee.* Unfo

13、rtunatelyC#supportsmultipleinheritanceforinterfacesbutnot* forclasses.Objectadaptersderivefromthetarget(singleinheritance)* andmaintainaprivateinstanceoftheadoptee.* Thesamplecodehereshowsanobjectadapter.Wehaveaclasscalled* FrameworkYAdapteewhichwewishtouse,yetthe(bulkof)theclientcode* (inGenericCli

14、entCode)iswrittentoexpectaclasscalledFrameworkXTarget.* TosolvetheprobelmwecreateanAdapterclass,whichitaFrameworkXTarget* totheclient,andcallsFrameworkYAdaptee.*/namespaceAdaPteJDeSignPaXternusingSystem;classFrameworkXTargetvirtualpublicvoidSomeRequest(intx)(/normalimplementationofSomeRequestgoesher

15、eclassFrameworkYAdaptee(publicvoidQuiteADifferentRequest(stringstr)(Console.Write1.ine(zzQuiteADifferentRequest=0,str);)classOurAdapter:FrameworkXTarget(privateFrameworkYAdapteeadaptee=newFrameworkYAclaptee();overridepublicvoidSomeRequest(inta)(stringb;b=a.ToStringO;adaptee.QuiteADifferentRequest(b);IIIIIISummarydescriptionforClient.IllpublicclassClientvoidGe

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 数据结构与算法

copyright@ 2008-2023 1wenmi网站版权所有

经营许可证编号:宁ICP备2022001189号-1

本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。第壹文秘仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知第壹文秘网,我们立即给予删除!