Builder生成器(创建型模式)

动机:
  在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的了对象用一定的算法构成:由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却欲相对稳定。
  如何应对这种变化?如何提供一种“封闭机制”来隔离出“复杂对象的各个部分”的变化,从而保持系统中的“稳定构建算法”不随着需求改变而改变?

意图:
  将一个复杂对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
  出自:《设计模式》GoF

Builder模式的几个要点:
  1、Builder模式主要用于“分步骤构建一个复杂的对象”。在这其中“分步骤”是一个稳定的算法,而复杂对象的各个部分则经常变化。
  2、变化点在哪里,封装哪里。Builder模式主要在于应对“复杂对象各个部分”的频繁需求变动。其缺点在于难以应对“分步骤构建算法”的需求变动。
  3、Abstract Factory模式解决“系列对象”的需求变化,Builder模式解决“对象部分”的需求变化。Builder模式通常和Composite模式组合使用。
稳定部分:

 1 None.gif using  System;
 2 None.gif
 3 None.gif namespace  Builder
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// Builder 的摘要说明。
 7ExpandedSubBlockEnd.gif    /// </summary>

 8InBlock.gif    public abstract class Builder
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        public abstract void BuildDoor();
11InBlock.gif        public abstract void BuildWall();
12InBlock.gif        public abstract void BuildFloor();
13InBlock.gif        public abstract void BuildWindows();
14InBlock.gif        public abstract void BuildHouseCeiling();
15InBlock.gif
16InBlock.gif        public abstract House GetHouse();
17ExpandedSubBlockEnd.gif    }

18InBlock.gif
19InBlock.gif    public abstract class Door
20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
21InBlock.gif
22ExpandedSubBlockEnd.gif    }

23InBlock.gif
24InBlock.gif    public abstract class Wall 
25ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
26InBlock.gif
27ExpandedSubBlockEnd.gif    }

28InBlock.gif
29InBlock.gif    public abstract class Windows
30ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
31InBlock.gif
32ExpandedSubBlockEnd.gif    }

33InBlock.gif
34InBlock.gif    public abstract class Floor
35ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
36InBlock.gif
37ExpandedSubBlockEnd.gif    }

38InBlock.gif
39InBlock.gif    public abstract class HouseCeiling
40ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
41InBlock.gif
42ExpandedSubBlockEnd.gif    }

43InBlock.gif    public abstract class House
44ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
45InBlock.gif
46ExpandedSubBlockEnd.gif    }

47InBlock.gif
48InBlock.gif    public class GameManager
49ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
50InBlock.gif        Builder builder;
51InBlock.gif        
52InBlock.gif
53InBlock.gif        public GameManager(Builder builder)
54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
55InBlock.gif            this.builder = builder;
56ExpandedSubBlockEnd.gif        }

57InBlock.gif    
58InBlock.gif        public     House CreateHouse()
59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
60InBlock.gif            builder.BuildDoor();    
61InBlock.gif
62InBlock.gif            builder.BuildWall();
63InBlock.gif            builder.BuildWindows();        
64InBlock.gif            builder.BuildFloor();
65InBlock.gif            builder.BuildHouseCeiling();
66InBlock.gif
67InBlock.gif            return builder.GetHouse();
68ExpandedSubBlockEnd.gif        }

69ExpandedSubBlockEnd.gif    }

70ExpandedBlockEnd.gif}

71 None.gif

变化部分:
 1 None.gif using  System;
 2 None.gif
 3 None.gif namespace  Builder
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// BuilderA 的摘要说明。
 7ExpandedSubBlockEnd.gif    /// </summary>

 8InBlock.gif    public class DoorA : Door
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif         
11ExpandedSubBlockEnd.gif    }

12InBlock.gif
13InBlock.gif    public class WallA : Wall
14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
15InBlock.gif
16ExpandedSubBlockEnd.gif    }

17InBlock.gif
18InBlock.gif    public class WindowsA : Windows
19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
20InBlock.gif
21ExpandedSubBlockEnd.gif    }

22InBlock.gif
23InBlock.gif    public class FloorA : Floor
24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
25InBlock.gif
26ExpandedSubBlockEnd.gif    }

27InBlock.gif
28InBlock.gif    public class HouseCeilingA : HouseCeiling
29ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
30InBlock.gif
31ExpandedSubBlockEnd.gif    }

32InBlock.gif
33InBlock.gif    public class HouseBuilderA : Builder
34ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
35InBlock.gif        public override void BuildDoor()
36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
37InBlock.gif              Console.Write("BuildDoorA"+"\n");
38ExpandedSubBlockEnd.gif        }

39InBlock.gif        public override void BuildWall()
40ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
41InBlock.gif        Console.Write("BuildWallA"+"\n");
42ExpandedSubBlockEnd.gif        }

43InBlock.gif        public override void BuildWindows()
44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
45InBlock.gif        Console.Write("BuildWindowsA"+"\n");
46ExpandedSubBlockEnd.gif        }

47InBlock.gif        public override void BuildFloor()
48ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
49InBlock.gif        Console.Write("BuildFloorA"+"\n");
50ExpandedSubBlockEnd.gif        }

51InBlock.gif        public override void BuildHouseCeiling()
52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
53InBlock.gif        Console.Write("BuildHouseCeilingA"+"\n");
54ExpandedSubBlockEnd.gif        }

55InBlock.gif        public override House GetHouse()
56ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
57InBlock.gif            return null;
58InBlock.gif
59ExpandedSubBlockEnd.gif        }

60ExpandedSubBlockEnd.gif    }

61ExpandedBlockEnd.gif}

62 None.gif

主程序:
 1 None.gif using  System;
 2 None.gif
 3 None.gif namespace  Builder
 4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// Class1 的摘要说明。
 7ExpandedSubBlockEnd.gif    /// </summary>

 8InBlock.gif    class Class1
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
11InBlock.gif        /// 应用程序的主入口点。
12ExpandedSubBlockEnd.gif        /// </summary>

13InBlock.gif        [STAThread]
14InBlock.gif        static void Main(string[] args)
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            //变化点
17InBlock.gif            GameManager g = new GameManager(new HouseBuilderA());
18InBlock.gif            //GameManager g = new GameManager(new HouseBuilderB());
19InBlock.gif            House h = g.CreateHouse();
20InBlock.gif            Console.ReadLine();
21ExpandedSubBlockEnd.gif        }

22ExpandedSubBlockEnd.gif    }

23ExpandedBlockEnd.gif}

24 None.gif

转载于:https://www.cnblogs.com/walker/archive/2006/08/17/479420.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值