4. 建造者模式
将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
1 //门
2 public abstract class Door
3 {
4 public abstract void ShowDoor();
5 }
6 //时尚的门
7 public class ModernDoor:Door
8 {
9 public override void ShowDoor()
10 {
11 Console.WriteLine("创建了一个时尚的门");
12 }
13 }
14 //墙
15 public abstract class Wall
16 {
17 public abstract void ShowWall();
18 }
19 //时尚的墙
20 public class ModernWall:Wall
21 {
22 public override void ShowWall()
23 {
24 Console.WriteLine("创建一个时尚在墙");
25 }
26 }
27 //窗
28 public abstract class Window
29 {
30 public abstract void ShowWindow();
31 }
32 //时尚的窗
33 public class ModernWindow:Window
34 {
35 public override void ShowWindow()
36 {
37 Console.WriteLine("创建一个时尚在窗");
38 }
39 }
40 //古典的窗
41 public class OldWindow:Window
42 {
43 public override void ShowWindow()
44 {
45 Console.WriteLine("创建一古典的窗");
46 }
47 }
48 //房子
49 public class House
50 {
51 public List<Door> doorList=new List<Door>();
52 public List<Wall> wallList=new List<Wall>();
53 public List<Window> windowList=new List<Window>();
54
55 public void ShowHouse()
56 {
57 foreach (Door door in doorList)
58 {
59 door.ShowDoor();
60 }
61 foreach (Wall wall in wallList)
62 {
63 wall.ShowWall();
64 }
65 foreach (Window window in windowList)
66 {
67 window.ShowWindow();
68 }
69 }
70 }
71 //房子建造者
72 public abstract class HouseBuilder
73 {
74 protected House house = new House();
75
76 public abstract void BuildWall();
77
78 public abstract void BuildDoor();
79
80 public abstract void BuildWindow();
81
82 public House GetHouse()
83 {
84 return house;
85 }
86 }
87 //时尚的房子建造者
88 public class ModernHouseBuilder:HouseBuilder
89 {
90 public ModernHouseBuilder()
91 {
92 BuildWall();
93 BuildWall();
94 BuildDoor();
95 BuildWindow();
96 }
97 public override void BuildWall()
98 {
99 house.wallList.Add(new ModernWall());
100 }
101
102 public override void BuildDoor()
103 {
104 house.doorList.Add(new ModernDoor());
105 }
106
107 public override void BuildWindow()
108 {
109 house.windowList.Add(new ModernWindow());
110 house.windowList.Add(new OldWindow());
111 }
112 }
113 //古典的房子建造者
114 public class OldHouseBuilder:HouseBuilder
115 {
116 public ModernHouseBuilder()
117 {
118
119 BuildWall();
120 BuildDoor();
121 BuildWindow();
122 }
123 public override void BuildWall()
124 {
125 house.wallList.Add(new ModernWall());
126 }
127
128 public override void BuildDoor()
129 {
130 house.doorList.Add(new ModernDoor());
131 }
132
133 public override void BuildWindow()
134 {
135 house.windowList.Add(new OldWindow());
136 house.windowList.Add(new OldWindow());
137 }
138 }
139 //客户端
140 class Program
141 {
142 static void Main(string[] args)
143 {
144 string assembly = ConfigurationManager.AppSettings["Assembly"];
145 string instance = ConfigurationManager.AppSettings["Instance"];
146 HouseBuilder build = (HouseBuilder)Assembly.Load(assembly).
147 CreateInstance(instance);
148 //HouseBuilder build = new ModernHouseBuilder();
149 House house = build.GetHouse();
150 house.ShowHouse();
151 Console.ReadLine();
152 }
153 }
154
155 <?xml version="1.0" encoding="utf-8" ?>
156 <configuration>
157 <appSettings>
158 <add key="Assembly" value="Builder"/>
159 <add key="Instance" value="Builder.ModernHouseBuilder"/>
160 </appSettings>
161 </configuration>
2 public abstract class Door
3 {
4 public abstract void ShowDoor();
5 }
6 //时尚的门
7 public class ModernDoor:Door
8 {
9 public override void ShowDoor()
10 {
11 Console.WriteLine("创建了一个时尚的门");
12 }
13 }
14 //墙
15 public abstract class Wall
16 {
17 public abstract void ShowWall();
18 }
19 //时尚的墙
20 public class ModernWall:Wall
21 {
22 public override void ShowWall()
23 {
24 Console.WriteLine("创建一个时尚在墙");
25 }
26 }
27 //窗
28 public abstract class Window
29 {
30 public abstract void ShowWindow();
31 }
32 //时尚的窗
33 public class ModernWindow:Window
34 {
35 public override void ShowWindow()
36 {
37 Console.WriteLine("创建一个时尚在窗");
38 }
39 }
40 //古典的窗
41 public class OldWindow:Window
42 {
43 public override void ShowWindow()
44 {
45 Console.WriteLine("创建一古典的窗");
46 }
47 }
48 //房子
49 public class House
50 {
51 public List<Door> doorList=new List<Door>();
52 public List<Wall> wallList=new List<Wall>();
53 public List<Window> windowList=new List<Window>();
54
55 public void ShowHouse()
56 {
57 foreach (Door door in doorList)
58 {
59 door.ShowDoor();
60 }
61 foreach (Wall wall in wallList)
62 {
63 wall.ShowWall();
64 }
65 foreach (Window window in windowList)
66 {
67 window.ShowWindow();
68 }
69 }
70 }
71 //房子建造者
72 public abstract class HouseBuilder
73 {
74 protected House house = new House();
75
76 public abstract void BuildWall();
77
78 public abstract void BuildDoor();
79
80 public abstract void BuildWindow();
81
82 public House GetHouse()
83 {
84 return house;
85 }
86 }
87 //时尚的房子建造者
88 public class ModernHouseBuilder:HouseBuilder
89 {
90 public ModernHouseBuilder()
91 {
92 BuildWall();
93 BuildWall();
94 BuildDoor();
95 BuildWindow();
96 }
97 public override void BuildWall()
98 {
99 house.wallList.Add(new ModernWall());
100 }
101
102 public override void BuildDoor()
103 {
104 house.doorList.Add(new ModernDoor());
105 }
106
107 public override void BuildWindow()
108 {
109 house.windowList.Add(new ModernWindow());
110 house.windowList.Add(new OldWindow());
111 }
112 }
113 //古典的房子建造者
114 public class OldHouseBuilder:HouseBuilder
115 {
116 public ModernHouseBuilder()
117 {
118
119 BuildWall();
120 BuildDoor();
121 BuildWindow();
122 }
123 public override void BuildWall()
124 {
125 house.wallList.Add(new ModernWall());
126 }
127
128 public override void BuildDoor()
129 {
130 house.doorList.Add(new ModernDoor());
131 }
132
133 public override void BuildWindow()
134 {
135 house.windowList.Add(new OldWindow());
136 house.windowList.Add(new OldWindow());
137 }
138 }
139 //客户端
140 class Program
141 {
142 static void Main(string[] args)
143 {
144 string assembly = ConfigurationManager.AppSettings["Assembly"];
145 string instance = ConfigurationManager.AppSettings["Instance"];
146 HouseBuilder build = (HouseBuilder)Assembly.Load(assembly).
147 CreateInstance(instance);
148 //HouseBuilder build = new ModernHouseBuilder();
149 House house = build.GetHouse();
150 house.ShowHouse();
151 Console.ReadLine();
152 }
153 }
154
155 <?xml version="1.0" encoding="utf-8" ?>
156 <configuration>
157 <appSettings>
158 <add key="Assembly" value="Builder"/>
159 <add key="Instance" value="Builder.ModernHouseBuilder"/>
160 </appSettings>
161 </configuration>
房子的建造过程是固定不变的,都是先做墙,后是门窗。 但是墙可以根据情况,选择对应的子库。
最后,都是返回一个对象:房子。