資源描述:
《c#設(shè)計(jì)模式工廠型設(shè)計(jì)示例》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、WORD格式可編輯模式設(shè)計(jì)c#--創(chuàng)建型--factorymethod名稱FactoryMethod結(jié)構(gòu)意圖定義一個(gè)用于創(chuàng)建對象的接口,讓子類決定實(shí)例化哪一個(gè)類。FactoryMethod使一個(gè)類的實(shí)例化延遲到其子類。適用性·當(dāng)一個(gè)類不知道它所必須創(chuàng)建的對象的類的時(shí)候?!ぎ?dāng)一個(gè)類希望由它的子類來指定它所創(chuàng)建的對象的時(shí)候。·當(dāng)類將創(chuàng)建對象的職責(zé)委托給多個(gè)幫助子類中的某一個(gè),并且你希望將哪一個(gè)幫助子類是代理者這一信息局部化的時(shí)候。????????????????????????????????????nam
2、espace?FactoryMethod_DesignPattern{????using?System;????//?These?two?classes?could?be?part?of?a?framework,????//?which?we?will?call?DP????//?===============================================????????class?DPDocument?????{????????}????abstract?class?DPApplic
3、ation?????{????????protected?DPDocument?doc;????????????????abstract?public?void?CreateDocument();專業(yè)知識整理分享WORD格式可編輯????????public?void?ConstructObjects()????????{????????????????????????//?Create?objects?as?needed????????????//?.?.?.????????????//?includ
4、ing?document????????????CreateDocument();????????}????????????????abstract?public?void?Dump();????}????//?These?two?classes?could?be?part?of?an?application?????//?=================================================????class?MyApplication?:?DPApplication???
5、??{????????override?public?void?CreateDocument()????????{????????????doc?=?new?MyDocument();????????????????????}????????????????????override?public?void?Dump()????????{????????????Console.WriteLine("MyApplication?exists");????????}????}????????class?MyD
6、ocument?:?DPDocument?????{????}????///?????///????Summary?description?for?Client.????///?????public?class?Client????{????????public?static?int?Main(string[]?args)????????{????????????MyApplication?myApplication?=?new?MyApplication();??
7、??????????myApplication.ConstructObjects();專業(yè)知識整理分享WORD格式可編輯????????????myApplication.Dump();????????????????????????return?0;????????}????}}?????????http://zhenyulu.cnblogs.com/articles/36590.html一、?工廠方法(FactoryMethod)模式工廠方法(FactoryMethod)模式是類的創(chuàng)建模式,其用意是
8、定義一個(gè)創(chuàng)建產(chǎn)品對象的工廠接口,將實(shí)際創(chuàng)建工作推遲到子類中。工廠方法模式是簡單工廠模式的進(jìn)一步抽象和推廣。由于使用了多態(tài)性,工廠方法模式保持了簡單工廠模式的優(yōu)點(diǎn),而且克服了它的缺點(diǎn)。在工廠方法模式中,核心的工廠類不再負(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)品。在FactoryMetho