資源描述:
《23種設(shè)計(jì)模式_UML_類(lèi)圖及對(duì)應(yīng)示例代碼》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、.?23種設(shè)計(jì)模式UML類(lèi)圖及對(duì)應(yīng)示例代碼(一)?收藏1.DoFactory.GangOfFour.Abstract.Structural?????AbstractFactory:提供一個(gè)創(chuàng)建一系列相關(guān)或相互依賴(lài)對(duì)象的接口,而無(wú)需指定它們具體的類(lèi)。???工廠模式:客戶類(lèi)和工廠類(lèi)分開(kāi)。消費(fèi)者任何時(shí)候需要某種產(chǎn)品,只需向工廠請(qǐng)求即可。消費(fèi)者無(wú)須修改就可以接納新產(chǎn)品。缺點(diǎn)是當(dāng)產(chǎn)品修改時(shí),工廠類(lèi)也要做相應(yīng)的修改。如:如何創(chuàng)建及如何向客戶端提供。using?System;namespace?DoFactory.GangOfFour.Abstract.Structural{????//
2、/?????///?MainApp?startup?class?for?Structural????///?Abstract?Factory?Design?Pattern.????///?..????class?MainApp????{????????///?????????///?Entry?point?into?console?application.????????///?????????public?static?void?Main()????????{????????????//?Abst
3、ract?factory?#1????????????AbstractFactory?factory1?=?new?ConcreteFactory1();????????????Client?client1?=?new?Client(factory1);????????????client1.Run();????????????//?Abstract?factory?#2????????????AbstractFactory?factory2?=?new?ConcreteFactory2();????????????Client?client2?=?new?Client(f
4、actory2);????????????client2.Run();????????????//?Wait?for?user?input????????????Console.Read();????????}????}????//?"AbstractFactory"????abstract?class?AbstractFactory????{????????public?abstract?AbstractProductA?CreateProductA();????????public?abstract?AbstractProductB?CreateProductB();?
5、???}????//?"ConcreteFactory1"????class?ConcreteFactory1?:?AbstractFactory????{????????public?override?AbstractProductA?CreateProductA()????????{????????????return?new?ProductA1();????????}????????public?override?AbstractProductB?CreateProductB()????????{????????????return?new..?ProductB1()
6、;????????}????}????//?"ConcreteFactory2"????class?ConcreteFactory2?:?AbstractFactory????{????????public?override?AbstractProductA?CreateProductA()????????{????????????return?new?ProductA2();????????}????????public?override?AbstractProductB?CreateProductB()????????{????????????return?new?Pr
7、oductB2();????????}????}????//?"AbstractProductA"????abstract?class?AbstractProductA????{????}????//?"AbstractProductB"????abstract?class?AbstractProductB????{????????public?abstract?void?Interact(AbstractProductA?a);????}????//?"ProductA1"????class?ProductA1?