資源描述:
《c#設(shè)計模式工廠型設(shè)計示例》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、word資料下載可編輯模式設(shè)計c#--創(chuàng)建型--factorymethod名稱FactoryMethod結(jié)構(gòu)意圖定義一個用于創(chuàng)建對象的接口,讓子類決定實(shí)例化哪一個類。FactoryMethod使一個類的實(shí)例化延遲到其子類。適用性·當(dāng)一個類不知道它所必須創(chuàng)建的對象的類的時候?!ぎ?dāng)一個類希望由它的子類來指定它所創(chuàng)建的對象的時候?!ぎ?dāng)類將創(chuàng)建對象的職責(zé)委托給多個幫助子類中的某一個,并且你希望將哪一個幫助子類是代理者這一信息局部化的時候。????????????????????????????????????n
2、amespace?FactoryMethod_DesignPattern{????using?System;????//?These?two?classes?could?be?part?of?a?framework,????//?which?we?will?call?DP????//?===============================================????????class?DPDocument?????{????????}????abstract?class?DPAppl
3、ication?????{????????protected?DPDocument?doc;????????????????abstract?public?void?CreateDocument();專業(yè)技術(shù)資料word資料下載可編輯????????public?void?ConstructObjects()????????{????????????????????????//?Create?objects?as?needed????????????//?.?.?.????????????//?incl
4、uding?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?M
6、yDocument?:?DPDocument?????{????}????///?????///????Summary?description?for?Client.????///?????public?class?Client????{????????public?static?int?Main(string[]?args)????????{????????????MyApplication?myApplication?=?new?MyApplication();
7、????????????myApplication.ConstructObjects();專業(yè)技術(shù)資料word資料下載可編輯????????????myApplication.Dump();????????????????????????return?0;????????}????}}?????????http://zhenyulu.cnblogs.com/articles/36590.html一、?工廠方法(FactoryMethod)模式工廠方法(FactoryMethod)模式是類的創(chuàng)建模式,其用
8、意是定義一個創(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)建工作交給子類去做。這個核心類僅僅負(fù)責(zé)給出具體工廠必須實(shí)現(xiàn)的接口,而不接觸哪一個產(chǎn)品類被實(shí)例化這種細(xì)節(jié)。這使得工廠方法模式可以允許系統(tǒng)在不修改工廠角色的情況下引進(jìn)新產(chǎn)品。在FactoryMet