建筑模式


名称Builder
结构builder.gif
意图将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
适用性
  • 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。
  • 当构造过程必须允许被构造的对象有不同的表示时。
example
 1 None.gif //  Builder
 2 None.gif
 3 None.gif //  Intent: "Separate the construction of a complex object from its
 4 None.gif //  representation so that the same construction process can create
 5 None.gif //  different representations". 
 6 None.gif
 7 None.gif //  For further information, read "Design Patterns", p97, Gamma et al.,
 8 None.gif //  Addison-Wesley, ISBN:0-201-63361-2
 9 None.gif
10 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Notes:
11InBlock.gif * Builder is an object creational design pattern that codifies the 
12InBlock.gif * construction process outside of the actual steps that carries out 
13InBlock.gif * the construction - thus allowing the construction process itself
14InBlock.gif * to be reused. 
15InBlock.gif * 
16ExpandedBlockEnd.gif */

17 None.gif 
18 None.gif namespace  Builder_DesignPattern
19 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
20InBlock.gif    using System;
21InBlock.gif
22InBlock.gif    // These two classes could be part of a framework,
23InBlock.gif    // which we will call DP
24InBlock.gif    // ===============================================
25InBlock.gif    
26InBlock.gif    class Director 
27ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
28InBlock.gif        public void Construct(AbstractBuilder abstractBuilder)
29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
30InBlock.gif            abstractBuilder.BuildPartA();
31InBlock.gif            if (1==1 ) //represents some local decision inside director
32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
33InBlock.gif                abstractBuilder.BuildPartB();            
34ExpandedSubBlockEnd.gif            }

35InBlock.gif            abstractBuilder.BuildPartC();            
36ExpandedSubBlockEnd.gif        }

37InBlock.gif
38ExpandedSubBlockEnd.gif    }

39InBlock.gif
40InBlock.gif    abstract class AbstractBuilder 
41ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
42InBlock.gif        abstract public void BuildPartA();
43InBlock.gif        abstract public void BuildPartB();
44InBlock.gif        abstract public void BuildPartC();
45ExpandedSubBlockEnd.gif    }

46InBlock.gif
47InBlock.gif    // These two classes could be part of an application 
48InBlock.gif    // =================================================
49InBlock.gif
50InBlock.gif    class ConcreteBuilder : AbstractBuilder 
51ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
52InBlock.gif        override public void BuildPartA()
53ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
54InBlock.gif            // Create some object here known to ConcreteBuilder
55InBlock.gif            Console.WriteLine("ConcreteBuilder.BuildPartA called");
56ExpandedSubBlockEnd.gif        }

57InBlock.gif                
58InBlock.gif        override public void BuildPartB()
59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
60InBlock.gif            // Create some object here known to ConcreteBuilder
61InBlock.gif            Console.WriteLine("ConcreteBuilder.BuildPartB called");
62ExpandedSubBlockEnd.gif        }

63InBlock.gif        
64InBlock.gif        override public void BuildPartC()
65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
66InBlock.gif            // Create some object here known to ConcreteBuilder
67InBlock.gif            Console.WriteLine("ConcreteBuilder.BuildPartC called");
68ExpandedSubBlockEnd.gif        }

69ExpandedSubBlockEnd.gif    }
    
70InBlock.gif
71ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
72InBlock.gif    ///    Summary description for Client.
73ExpandedSubBlockEnd.gif    /// </summary>

74InBlock.gif    public class Client
75ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
76InBlock.gif        public static int Main(string[] args)
77ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
78InBlock.gif            ConcreteBuilder concreteBuilder = new ConcreteBuilder();
79InBlock.gif            Director director = new Director();
80InBlock.gif
81InBlock.gif            director.Construct(concreteBuilder);
82InBlock.gif
83InBlock.gif            return 0;
84ExpandedSubBlockEnd.gif        }

85ExpandedSubBlockEnd.gif    }

86ExpandedBlockEnd.gif}

87 None.gif
88 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/06/24/180084.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值