23个设计模式C#代码.docx
/AbstractFactory/Intent:"Provideaninterfaceforcreatingfamiliesofrelatedor/dependentobjectswithoutspecifyingtheirconcreteclasses”./Forfurtherinformation,read"DesignPatterns'",p87,Gammaetal.,/Addison-Wesley,ISBN:0-201-63361-2*Notes:* Whentheconstructionneededinvolvesmanyobjects,possibleorganised* inmulti-facetedarrangements,theentireconstructioncanbedelegated* toanabstractfactory.Thisexposesstandardisedcreationfunctionality* whichcanbecustomisedinconcreteimplementationtosuityourspecific* needs,andavoidembeddingthisinformationinhigherlevelcode-it* justneedstoknowhowtocalltheabstractfactory.* Inthissample,wehaveaframeworkwiththreeabstractoperatingclasses,* calledDPDocument,DPWorkspaceandDPViewandoneabstractconstruction* class,calledDPFactory.napplication-levelclass,calledDPApplication* isresponsibleforconstruction.* Wehaveaseriesofapp1ication-1eve1operatingclassesderivedfromthis* framework-MyDocument,MyWorkspaceandMyView.Fordesignreasonswe* assumewewishtoinstantiatethesefrominsideDPApplication.Asthere* aremultipleobjectsneededandtheycouldbearrangedindifferent* lattices,weuseafactory,MyFactory(inourexample,thereareall* simplesiblings),whichiscalledinsideDPApplication.*/namespaceAbstractFactoryDesignPatternusingSystem;/Theseclassescouldbepartofaframework,/whichwewillcallDP/=abstractclassDPDocumentabstractpublicvoidDump();abstractclassDPWorkspaceabstractpublicvoidDump();DPViewpublicvoidDump();abstractclass(abstractabstractclassabstractabstractabstractDPFactorypublicDPDocumentCreateDocument();publicDPViewCreateViewO;publicDPWorkspaceCreateWorkspaceO;abstractclassDPApplication(protectedDPDocumentdoc;protectedDPWorkspaceworkspace;protectedDPViewview;publicvoidConstructObjects(DPFactoryfactory)(/Createobjectsasneededdoc=factory.CreateDocumentO;workspace=factory,CreateWorkspaceO;view=factory.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");publicvoidCreateFamilyO(MyFactorymyFactory=newMyFactoryO;ConstructObjects(myFactory);)classMyDocument:DPDocumentpublicMyDocumentO(Console.Write1.ine(,inMyDocumentconstructor);overridepublicvoidDump()(Console.Write1.ine(,MyDocumentexists");)classMyWorkspace:DPWorkspaceConsole.Write1.ine(zzMyWorkspaceexists");classMyView:DPViewoverridepublicvoidDump()Console.Write1.ine(,MyViewexists");classMyFactory:DPFactory(overridepublicDPDocumentCreateDocument()(returnnewMyDoCUment();overridepublicDPWorkspaceCreateWorkspace()(returnnewMyWorkspace();)overridepublicDPViewCreateViewOreturnnewMyViewO;III<summary>IIISummarydescriptionforClient.Ill</summary>publicclassClient(publicstaticintMain(stringargs)(MyApplicationmyApplication=newMyApplicationO;myApplication.CreateFamilyO;myApplication.DumpStateO;returnO;/Adapter/Intent:"Converttheinterfaceofaclassintoanotherinterface/clientsexpect.Adapterletsclassesworktogetherthatcouldn,t/otherwisebecauseofincompatibleinterfaces""./Forfurtherinformation,read"DesignPatterns”,pl39,Gammaetal.,/Addison-Wesley,ISBN:0-201-63361-2*Notes:* Adaptersareoftenusedwhenclientcodeiswrittentoexpectclasses* fromoneframework,anditmeanttoworkwithclassesfromatotally* differentframework.Assumeyoucannotchangethecodeofeitherframework.* thesolutionisforyoutowriteanadapter,whichappearslikea*nativeclasstoeachframework.* Therearetwodifferenttypesofadapters-classadapterandobject* adapter.Classadaptersarebasedonmultipleinheritance-specifically* theinterfaceofthetargetclassandtheimplementationoftheadaptee.* UnfortunatelyC#supportsmultipleinheritanceforinterfacesbutnot* forclasses.Objectadaptersderivefromthetarget(singleinheritance)* andmaintainaprivateinstanceoftheadoptee.* Thesamplecodehereshowsanobjectadapter.Wehaveaclasscalled* FrameworkYAdapteewhichwewishtouse,yetthe(bulkof)theclientcode* (inGenericClientCode)iswrittentoexpectaclasscalledFrameworkXTarget.* TosolvetheprobelmwecreateanAdapterclass,whichitaFrameworkXTarget* totheclient,andcallsFrameworkYAdaptee.*/namespaceAdaPteJDeSignPaXternusingSystem;classFrameworkXTargetvirtualpublicvoidSomeRequest(intx)(/normalimplementationofSomeRequestgoeshereclassFrameworkYAdaptee(publicvoidQuiteADifferentRequest(stringstr)(Console.Write1.ine(zzQuiteADifferentRequest=0,str);)classOurAdapter:FrameworkXTarget(privateFrameworkYAdapteeadaptee=newFrameworkYAclaptee();overridepublicvoidSomeRequest(inta)(stringb;b=a.ToStringO;adaptee.QuiteADifferentRequest(b);III<summary>IIISummarydescriptionforClient.Ill<summary>publicclassClientvoidGe