資源描述:
《c設(shè)計(jì)模式工廠型設(shè)計(jì)示例》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫。
1、模式設(shè)計(jì)c#--創(chuàng)建型--factorymethod名稱FactoryMethod結(jié)構(gòu)意圖定義一個(gè)用于創(chuàng)建對(duì)象的接口,讓子類決定實(shí)例化哪一個(gè)類。FactoryMethod使一個(gè)類的實(shí)例化延遲到其子類。適用性·當(dāng)一個(gè)類不知道它所必須創(chuàng)建的對(duì)象的類的時(shí)候?!ぎ?dāng)一個(gè)類希望由它的子類來指定它所創(chuàng)建的對(duì)象的時(shí)候?!ぎ?dāng)類將創(chuàng)建對(duì)象的職責(zé)委托給多個(gè)幫助子類中的某一個(gè),并且你希望將哪一個(gè)幫助子類是代理者這一信息局部化的時(shí)候。????????????????????????????????????namespace?FactoryMet
2、hod_DesignPattern{????using?System;????//?These?two?classes?could?be?part?of?a?framework,????//?which?we?will?call?DP????//?===============================================????????class?DPDocument?????{????????}????abstract?class?DPApplication?????{????????protec
3、ted?DPDocument?doc;????????????????abstract?public?void?CreateDocument();????????public?void?ConstructObjects()????????{????????????????????????//?Create?objects?as?needed????????????//?.?.?.????????????//?including?document????????????CreateDocument();????????}
4、????????????????abstract?public?void?Dump();????}????//?These?two?classes?could?be?part?of?an?application?????//?=================================================????class?MyApplication?:?DPApplication?????{????????override?public?void?CreateDocument()????????{?
5、???????????doc?=?new?MyDocument();????????????????????}????????????????????override?public?void?Dump()????????{????????????Console.WriteLine("MyApplication?exists");????????}????}????????class?MyDocument?:?DPDocument?????{????}????///?????///????Summary
6、?description?for?Client.????///?????public?class?Client????{????????public?static?int?Main(string[]?args)????????{????????????MyApplication?myApplication?=?new?MyApplication();????????????myApplication.ConstructObjects();????????????myApplication.Dump(
7、);????????????????????????return?0;????????}????}}?????????http://zhenyulu.cnblogs.com/articles/36590.html一、?工廠方法(FactoryMethod)模式工廠方法(FactoryMethod)模式是類的創(chuàng)建模式,其用意是定義一個(gè)創(chuàng)建產(chǎn)品對(duì)象的工廠接口,將實(shí)際創(chuàng)建工作推遲到子類中。工廠方法模式是簡單工廠模式的進(jìn)一步抽象和推廣。由于使用了多態(tài)性,工廠方法模式保持了簡單工廠模式的優(yōu)點(diǎn),而且克服了它的缺點(diǎn)。在工廠方法模式中
8、,核心的工廠類不再負(fù)責(zé)所有產(chǎn)品的創(chuàng)建,而是將具體創(chuàng)建工作交給子類去做。這個(gè)核心類僅僅負(fù)責(zé)給出具體工廠必須實(shí)現(xiàn)的接口,而不接觸哪一個(gè)產(chǎn)品類被實(shí)例化這種細(xì)節(jié)。這使得工廠方法模式可以允許系統(tǒng)在不修改工廠角色的情況下引進(jìn)新產(chǎn)品。在FactoryMethod模式中,工廠類與產(chǎn)品類往往具有平行的等級(jí)結(jié)構(gòu),它們之間一一對(duì)應(yīng)。二、?Factory