資源描述:
《C建造者模式和示例代碼.doc》由會(huì)員上傳分享,免費(fèi)在線(xiàn)閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、模式設(shè)計(jì)c#--創(chuàng)建型--Builder名稱(chēng)Builder建造者模式結(jié)構(gòu)意圖將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的表示分離,使得同樣的構(gòu)建過(guò)程可以創(chuàng)建不同的表示。適用性·當(dāng)創(chuàng)建復(fù)雜對(duì)象的算法應(yīng)該獨(dú)立于該對(duì)象的組成部分以及它們的裝配方式時(shí)?!ぎ?dāng)構(gòu)造過(guò)程必須允許被構(gòu)造的對(duì)象有不同的表示時(shí)。CodeExamplenamespace?Builder_DesignPattern{????using?System;????//?These?two?classes?could?be?part?of?a?framework,????//?which?we?will?call?DP???
2、?//?===============================================????????class?Director?????{????????public?void?Construct(AbstractBuilder?abstractBuilder)????????{????????????abstractBuilder.BuildPartA();????????????if?(1==1?)?//represents?some?local?decision?inside?director????????????{???????
3、?????????abstractBuilder.BuildPartB();????????????????????????}????????????abstractBuilder.BuildPartC();????????????????????}????}????abstract?class?AbstractBuilder?????{????????abstract?public?void?BuildPartA();????????abstract?public?void?BuildPartB();????????abstract?public?void
4、?BuildPartC();????}????//?These?two?classes?could?be?part?of?an?application?????//?=================================================????class?ConcreteBuilder?:?AbstractBuilder?????{????????override?public?void?BuildPartA()????????{????????????//?Create?some?object?here?known?to?Con
5、creteBuilder????????????Console.WriteLine("ConcreteBuilder.BuildPartA?called");????????}????????????????????????override?public?void?BuildPartB()????????{????????????//?Create?some?object?here?known?to?ConcreteBuilder????????????Console.WriteLine("ConcreteBuilder.BuildPartB?called"
6、);????????}????????????????override?public?void?BuildPartC()????????{????????????//?Create?some?object?here?known?to?ConcreteBuilder????????????Console.WriteLine("ConcreteBuilder.BuildPartC?called");????????}????}????????///?????///????Summary?description?for?Client.????//
7、/?????public?class?Client????{????????public?static?int?Main(string[]?args)????????{????????????ConcreteBuilder?concreteBuilder?=?new?ConcreteBuilder();????????????Director?director?=?new?Director();????????????director.Construct(concreteBuilder);????????????return?0;????
8、????}????}}一、?建造者(Builder)