建造者模式(Builder Pattern)简单随笔
提供一种“封装机制”来隔离出“复杂对象的各个部分”的变化,从而保持系统中的“稳定构建算法”不随着需求改变而改变。
一、建造者模式简介
建造者模式(Builder Pattern),将一个复杂对象的构建与它的表示分离,使的同样的构建过程可以创建不同的表示。
建造者模式的优点是:使得建造代码与表示代码分离,由于建造者隐藏了该产品是如何组装的,所以如要改变一个产品的内部表示,只需要再定义一个具体的建造者就可以了。
二、解决的问题 主要用于创建一些复杂的对象,这些对象内部构建间的建造顺序通常是稳定的,但是对象内部的构建通常面临着复杂的变化。
三、建造者模式图解
现以客户向包工头建立一所房子为例:
builder以及继承类如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 public abstract class Builder
9 {
10 public abstract void CreateWall();
11 public abstract void CreateDoll();
12 public abstract void CreateFloor();
13 public abstract House ReturnHouseModel();
14 }
15 }
16
17 public class BlueBuilder:Builder
18 {
19 House house = new House();
20 public override void CreateWall()
21 {
22 //throw new NotImplementedException();
23 house.Add("Create blue Wall");
24 }
25
26 public override void CreateDoll()
27 {
28 //throw new NotImplementedException();
29 house.Add("Create blue Doll");
30 }
31
32 public override void CreateFloor()
33 {
34 //throw new NotImplementedException();
35 house.Add("Create Blue Floor");
36 }
37
38 public override House ReturnHouseModel()
39 {
40 //throw new NotImplementedException();
41 return house;
42 }
43 }
44
45 public class WhiteBuilder : Builder
46 {
47 House house = new House();
48 public override void CreateWall()
49 {
50 //throw new NotImplementedException();
51 house.Add("Create White Wall");
52 }
53
54 public override void CreateDoll()
55 {
56 //throw new NotImplementedException();
57 house.Add("Create White Doll");
58 }
59
60 public override void CreateFloor()
61 {
62 //throw new NotImplementedException();
63 house.Add("Create White Floor");
64 }
65
66 public override House ReturnHouseModel()
67 {
68 //throw new NotImplementedException();
69 return house;
70 }
71 }
产品为房子:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 public class House
9 {
10 Queue<string> queue = new Queue<string>();
11 public void Add(string str)
12 {
13 queue.Enqueue(str);
14 }
15
16 public void Show()
17 {
18 if (queue != null)
19 {
20
21 Console.WriteLine("**************************House Building!****************************");
22 while (queue != null && queue.Count > 0)
23 {
24 Console.WriteLine(queue.Dequeue().ToString());
25 }
26 }
27 Console.WriteLine("---------------------------House Builded!----------------------------");
28 }
29 }
30 }
31
包工头通过 命令Builder来生成相应的房子
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 public class HouseCreator
9 {
10 public void Create(Builder builder)
11 {
12 builder.CreateDoll();
13 builder.CreateFloor();
14 builder.CreateWall();
15 }
16 }
17 }
18
客户端代码:获取房子
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 HouseCreator creator = new HouseCreator();
13 Builder builder;
14 Console.WriteLine("please select the house style!");
15 Console.WriteLine("blue ------1");
16 Console.WriteLine("white------2");
17 Console.WriteLine("Exit ------exit");
18 string input = Console.ReadLine();
19
20 while (input.Trim() != "")
21 {
22 if (input == "exit")
23 {
24 Environment.Exit(0);
25 return;
26 }
27 string style;
28 switch (input)
29 {
30 case "1":
31 style = "BlueBuilder";
32 break;
33 case "2":
34 style = "WhiteBuilder";
35 break;
36 default:
37 style = "WhiteBuilder";
38 break;
39 }
40 //string currentPath = System.IO.Directory.GetCurrentDirectory();
41 //string path = currentPath.Substring(0, currentPath.LastIndexOf("bin"));
42 //获取需要生成的房子
43 builder = (Builder)System.Reflection.Assembly.Load("BuilderPatternSam").CreateInstance("BuilderPatternSam." + style);
44 //生成者 生产相应的房子
45 creator.Create(builder);
46
47 /* public void Create(Builder builder)
48 {
49 builder.CreateDoll();
50 builder.CreateFloor();
51 builder.CreateWall();
52 }
53 public abstract class Builder
54 {
55 public abstract void CreateWall();
56 public abstract void CreateDoll();
57 public abstract void CreateFloor();
58 public abstract House ReturnHouseModel();
59 }*/
60 //返回房子给客户
61 House house = builder.ReturnHouseModel();
62 house.Show();
63 input = Console.ReadLine();
64 }
65 }
66 }
67 }
68
其实就是将房子的组建过程进行封装,以适应多种变化。
图示如下:
![](https://images.cnblogs.com/cnblogs_com/jasenkin/buildersam.jpg)
*******************************************************************************************************
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 public abstract class Builder
9 {
10 public abstract void CreateWall();
11 public abstract void CreateDoll();
12 public abstract void CreateFloor();
13 public abstract House ReturnHouseModel();
14 }
15 }
16
17 public class BlueBuilder:Builder
18 {
19 House house = new House();
20 public override void CreateWall()
21 {
22 //throw new NotImplementedException();
23 house.Add("Create blue Wall");
24 }
25
26 public override void CreateDoll()
27 {
28 //throw new NotImplementedException();
29 house.Add("Create blue Doll");
30 }
31
32 public override void CreateFloor()
33 {
34 //throw new NotImplementedException();
35 house.Add("Create Blue Floor");
36 }
37
38 public override House ReturnHouseModel()
39 {
40 //throw new NotImplementedException();
41 return house;
42 }
43 }
44
45 public class WhiteBuilder : Builder
46 {
47 House house = new House();
48 public override void CreateWall()
49 {
50 //throw new NotImplementedException();
51 house.Add("Create White Wall");
52 }
53
54 public override void CreateDoll()
55 {
56 //throw new NotImplementedException();
57 house.Add("Create White Doll");
58 }
59
60 public override void CreateFloor()
61 {
62 //throw new NotImplementedException();
63 house.Add("Create White Floor");
64 }
65
66 public override House ReturnHouseModel()
67 {
68 //throw new NotImplementedException();
69 return house;
70 }
71 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 public abstract class Builder
9 {
10 public abstract void CreateWall();
11 public abstract void CreateDoll();
12 public abstract void CreateFloor();
13 public abstract House ReturnHouseModel();
14 }
15 }
16
17 public class BlueBuilder:Builder
18 {
19 House house = new House();
20 public override void CreateWall()
21 {
22 //throw new NotImplementedException();
23 house.Add("Create blue Wall");
24 }
25
26 public override void CreateDoll()
27 {
28 //throw new NotImplementedException();
29 house.Add("Create blue Doll");
30 }
31
32 public override void CreateFloor()
33 {
34 //throw new NotImplementedException();
35 house.Add("Create Blue Floor");
36 }
37
38 public override House ReturnHouseModel()
39 {
40 //throw new NotImplementedException();
41 return house;
42 }
43 }
44
45 public class WhiteBuilder : Builder
46 {
47 House house = new House();
48 public override void CreateWall()
49 {
50 //throw new NotImplementedException();
51 house.Add("Create White Wall");
52 }
53
54 public override void CreateDoll()
55 {
56 //throw new NotImplementedException();
57 house.Add("Create White Doll");
58 }
59
60 public override void CreateFloor()
61 {
62 //throw new NotImplementedException();
63 house.Add("Create White Floor");
64 }
65
66 public override House ReturnHouseModel()
67 {
68 //throw new NotImplementedException();
69 return house;
70 }
71 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 public class House
9 {
10 Queue<string> queue = new Queue<string>();
11 public void Add(string str)
12 {
13 queue.Enqueue(str);
14 }
15
16 public void Show()
17 {
18 if (queue != null)
19 {
20
21 Console.WriteLine("**************************House Building!****************************");
22 while (queue != null && queue.Count > 0)
23 {
24 Console.WriteLine(queue.Dequeue().ToString());
25 }
26 }
27 Console.WriteLine("---------------------------House Builded!----------------------------");
28 }
29 }
30 }
31
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 public class House
9 {
10 Queue<string> queue = new Queue<string>();
11 public void Add(string str)
12 {
13 queue.Enqueue(str);
14 }
15
16 public void Show()
17 {
18 if (queue != null)
19 {
20
21 Console.WriteLine("**************************House Building!****************************");
22 while (queue != null && queue.Count > 0)
23 {
24 Console.WriteLine(queue.Dequeue().ToString());
25 }
26 }
27 Console.WriteLine("---------------------------House Builded!----------------------------");
28 }
29 }
30 }
31
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 public class HouseCreator
9 {
10 public void Create(Builder builder)
11 {
12 builder.CreateDoll();
13 builder.CreateFloor();
14 builder.CreateWall();
15 }
16 }
17 }
18
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 public class HouseCreator
9 {
10 public void Create(Builder builder)
11 {
12 builder.CreateDoll();
13 builder.CreateFloor();
14 builder.CreateWall();
15 }
16 }
17 }
18
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 HouseCreator creator = new HouseCreator();
13 Builder builder;
14 Console.WriteLine("please select the house style!");
15 Console.WriteLine("blue ------1");
16 Console.WriteLine("white------2");
17 Console.WriteLine("Exit ------exit");
18 string input = Console.ReadLine();
19
20 while (input.Trim() != "")
21 {
22 if (input == "exit")
23 {
24 Environment.Exit(0);
25 return;
26 }
27 string style;
28 switch (input)
29 {
30 case "1":
31 style = "BlueBuilder";
32 break;
33 case "2":
34 style = "WhiteBuilder";
35 break;
36 default:
37 style = "WhiteBuilder";
38 break;
39 }
40 //string currentPath = System.IO.Directory.GetCurrentDirectory();
41 //string path = currentPath.Substring(0, currentPath.LastIndexOf("bin"));
42 //获取需要生成的房子
43 builder = (Builder)System.Reflection.Assembly.Load("BuilderPatternSam").CreateInstance("BuilderPatternSam." + style);
44 //生成者 生产相应的房子
45 creator.Create(builder);
46
47 /* public void Create(Builder builder)
48 {
49 builder.CreateDoll();
50 builder.CreateFloor();
51 builder.CreateWall();
52 }
53 public abstract class Builder
54 {
55 public abstract void CreateWall();
56 public abstract void CreateDoll();
57 public abstract void CreateFloor();
58 public abstract House ReturnHouseModel();
59 }*/
60 //返回房子给客户
61 House house = builder.ReturnHouseModel();
62 house.Show();
63 input = Console.ReadLine();
64 }
65 }
66 }
67 }
68
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace BuilderPatternSam
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 HouseCreator creator = new HouseCreator();
13 Builder builder;
14 Console.WriteLine("please select the house style!");
15 Console.WriteLine("blue ------1");
16 Console.WriteLine("white------2");
17 Console.WriteLine("Exit ------exit");
18 string input = Console.ReadLine();
19
20 while (input.Trim() != "")
21 {
22 if (input == "exit")
23 {
24 Environment.Exit(0);
25 return;
26 }
27 string style;
28 switch (input)
29 {
30 case "1":
31 style = "BlueBuilder";
32 break;
33 case "2":
34 style = "WhiteBuilder";
35 break;
36 default:
37 style = "WhiteBuilder";
38 break;
39 }
40 //string currentPath = System.IO.Directory.GetCurrentDirectory();
41 //string path = currentPath.Substring(0, currentPath.LastIndexOf("bin"));
42 //获取需要生成的房子
43 builder = (Builder)System.Reflection.Assembly.Load("BuilderPatternSam").CreateInstance("BuilderPatternSam." + style);
44 //生成者 生产相应的房子
45 creator.Create(builder);
46
47 /* public void Create(Builder builder)
48 {
49 builder.CreateDoll();
50 builder.CreateFloor();
51 builder.CreateWall();
52 }
53 public abstract class Builder
54 {
55 public abstract void CreateWall();
56 public abstract void CreateDoll();
57 public abstract void CreateFloor();
58 public abstract House ReturnHouseModel();
59 }*/
60 //返回房子给客户
61 House house = builder.ReturnHouseModel();
62 house.Show();
63 input = Console.ReadLine();
64 }
65 }
66 }
67 }
68
![](https://images.cnblogs.com/cnblogs_com/jasenkin/buildersam.jpg)