資源描述:
《23種設(shè)計模式_加示例代碼》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、23種設(shè)計模式UML類圖及對應(yīng)示例代碼(一)?收藏1.DoFactory.GangOfFour.Abstract.Structural?????AbstractFactory:提供一個創(chuàng)建一系列相關(guān)或相互依賴對象的接口,而無需指定它們具體的類。???工廠模式:客戶類和工廠類分開。消費者任何時候需要某種產(chǎn)品,只需向工廠請求即可。消費者無須修改就可以接納新產(chǎn)品。缺點是當(dāng)產(chǎn)品修改時,工廠類也要做相應(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()????????{???????
3、?????//?Abstract?factory?#1????????????AbstractFactory?factory1?=?new?ConcreteFactory1();????????????Client?client1?=?new?Client(factory1);????????????client1.Run();????????????//?Abstract?factory?#2????????????AbstractFactory?factory2?=?new?ConcreteFactory2();????????????Client?cl
4、ient2?=?new?Client(factory2);????????????client2.Run();????????????//?Wait?for?user?input????????????Console.Read();????????}????}????//?"AbstractFactory"????abstract?class?AbstractFactory????{????????public?abstract?AbstractProductA?CreateProductA();????????public?abstract?Abstrac
5、tProductB?CreateProductB();????}????//?"ConcreteFactory1"????class?ConcreteFactory1?:?AbstractFactory????{????????public?override?AbstractProductA?CreateProductA()????????{????????????return?new?ProductA1();????????}????????public?override?AbstractProductB?CreateProductB()????????{
6、????????????return?new?ProductB1();????????}????}????//?"ConcreteFactory2"????class?ConcreteFactory2?:?AbstractFactory????{????????public?override?AbstractProductA?CreateProductA()????????{????????????return?new?ProductA2();????????}????????public?override?AbstractProductB?CreatePr
7、oductB()????????{????????????return?new?ProductB2();????????}????}????//?"AbstractProductA"????abstract?class?AbstractProductA????{????}????//?"AbstractProductB"????abstract?class?AbstractProductB????{????????public?abstract?void?Interact(AbstractProductA?a);????}????//?"ProductA1"
8、????class?ProductA1?:?Abst