在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的了对象用一定的算法构成:由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却欲相对稳定。
如何应对这种变化?如何提供一种“封闭机制”来隔离出“复杂对象的各个部分”的变化,从而保持系统中的“稳定构建算法”不随着需求改变而改变?
意图:
将一个复杂对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
出自:《设计模式》GoF
Builder模式的几个要点:
1、Builder模式主要用于“分步骤构建一个复杂的对象”。在这其中“分步骤”是一个稳定的算法,而复杂对象的各个部分则经常变化。
2、变化点在哪里,封装哪里。Builder模式主要在于应对“复杂对象各个部分”的频繁需求变动。其缺点在于难以应对“分步骤构建算法”的需求变动。
3、Abstract Factory模式解决“系列对象”的需求变化,Builder模式解决“对象部分”的需求变化。Builder模式通常和Composite模式组合使用。
稳定部分:
1using System;
2
3namespace Builder
4{
5 /// <summary>
6 /// Builder 的摘要说明。
7 /// </summary>
8 public abstract class Builder
9 {
10 public abstract void BuildDoor();
11 public abstract void BuildWall();
12 public abstract void BuildFloor();
13 public abstract void BuildWindows();
14 public abstract void BuildHouseCeiling();
15
16 public abstract House GetHouse();
17 }
18
19 public abstract class Door
20 {
21
22 }
23
24 public abstract class Wall
25 {
26
27 }
28
29 public abstract class Windows
30 {
31
32 }
33
34 public abstract class Floor
35 {
36
37 }
38
39 public abstract class HouseCeiling
40 {
41
42 }
43 public abstract class House
44 {
45
46 }
47
48 public class GameManager
49 {
50 Builder builder;
51
52
53 public GameManager(Builder builder)
54 {
55 this.builder = builder;
56 }
57
58 public House CreateHouse()
59 {
60 builder.BuildDoor();
61
62 builder.BuildWall();
63 builder.BuildWindows();
64 builder.BuildFloor();
65 builder.BuildHouseCeiling();
66
67 return builder.GetHouse();
68 }
69 }
70}
71
2
3namespace Builder
4{
5 /// <summary>
6 /// Builder 的摘要说明。
7 /// </summary>
8 public abstract class Builder
9 {
10 public abstract void BuildDoor();
11 public abstract void BuildWall();
12 public abstract void BuildFloor();
13 public abstract void BuildWindows();
14 public abstract void BuildHouseCeiling();
15
16 public abstract House GetHouse();
17 }
18
19 public abstract class Door
20 {
21
22 }
23
24 public abstract class Wall
25 {
26
27 }
28
29 public abstract class Windows
30 {
31
32 }
33
34 public abstract class Floor
35 {
36
37 }
38
39 public abstract class HouseCeiling
40 {
41
42 }
43 public abstract class House
44 {
45
46 }
47
48 public class GameManager
49 {
50 Builder builder;
51
52
53 public GameManager(Builder builder)
54 {
55 this.builder = builder;
56 }
57
58 public House CreateHouse()
59 {
60 builder.BuildDoor();
61
62 builder.BuildWall();
63 builder.BuildWindows();
64 builder.BuildFloor();
65 builder.BuildHouseCeiling();
66
67 return builder.GetHouse();
68 }
69 }
70}
71
变化部分:
1using System;
2
3namespace Builder
4{
5 /// <summary>
6 /// BuilderA 的摘要说明。
7 /// </summary>
8 public class DoorA : Door
9 {
10
11 }
12
13 public class WallA : Wall
14 {
15
16 }
17
18 public class WindowsA : Windows
19 {
20
21 }
22
23 public class FloorA : Floor
24 {
25
26 }
27
28 public class HouseCeilingA : HouseCeiling
29 {
30
31 }
32
33 public class HouseBuilderA : Builder
34 {
35 public override void BuildDoor()
36 {
37 Console.Write("BuildDoorA"+"\n");
38 }
39 public override void BuildWall()
40 {
41 Console.Write("BuildWallA"+"\n");
42 }
43 public override void BuildWindows()
44 {
45 Console.Write("BuildWindowsA"+"\n");
46 }
47 public override void BuildFloor()
48 {
49 Console.Write("BuildFloorA"+"\n");
50 }
51 public override void BuildHouseCeiling()
52 {
53 Console.Write("BuildHouseCeilingA"+"\n");
54 }
55 public override House GetHouse()
56 {
57 return null;
58
59 }
60 }
61}
62
2
3namespace Builder
4{
5 /// <summary>
6 /// BuilderA 的摘要说明。
7 /// </summary>
8 public class DoorA : Door
9 {
10
11 }
12
13 public class WallA : Wall
14 {
15
16 }
17
18 public class WindowsA : Windows
19 {
20
21 }
22
23 public class FloorA : Floor
24 {
25
26 }
27
28 public class HouseCeilingA : HouseCeiling
29 {
30
31 }
32
33 public class HouseBuilderA : Builder
34 {
35 public override void BuildDoor()
36 {
37 Console.Write("BuildDoorA"+"\n");
38 }
39 public override void BuildWall()
40 {
41 Console.Write("BuildWallA"+"\n");
42 }
43 public override void BuildWindows()
44 {
45 Console.Write("BuildWindowsA"+"\n");
46 }
47 public override void BuildFloor()
48 {
49 Console.Write("BuildFloorA"+"\n");
50 }
51 public override void BuildHouseCeiling()
52 {
53 Console.Write("BuildHouseCeilingA"+"\n");
54 }
55 public override House GetHouse()
56 {
57 return null;
58
59 }
60 }
61}
62
主程序:
1using System;
2
3namespace Builder
4{
5 /// <summary>
6 /// Class1 的摘要说明。
7 /// </summary>
8 class Class1
9 {
10 /// <summary>
11 /// 应用程序的主入口点。
12 /// </summary>
13 [STAThread]
14 static void Main(string[] args)
15 {
16 //变化点
17 GameManager g = new GameManager(new HouseBuilderA());
18 //GameManager g = new GameManager(new HouseBuilderB());
19 House h = g.CreateHouse();
20 Console.ReadLine();
21 }
22 }
23}
24
2
3namespace Builder
4{
5 /// <summary>
6 /// Class1 的摘要说明。
7 /// </summary>
8 class Class1
9 {
10 /// <summary>
11 /// 应用程序的主入口点。
12 /// </summary>
13 [STAThread]
14 static void Main(string[] args)
15 {
16 //变化点
17 GameManager g = new GameManager(new HouseBuilderA());
18 //GameManager g = new GameManager(new HouseBuilderB());
19 House h = g.CreateHouse();
20 Console.ReadLine();
21 }
22 }
23}
24