《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