Builder模式的缘起:
假设创建游戏中的一个房屋House设施,该房屋的构建由几部分组成,且各个部分富于变化。如果使用最直观的设计方法,每一个房屋部分的变化,都将导致房屋构建的重新修正.....
动机(Motivation):
在软件系统中,有时候面临 一个"复杂对象"的创建工作,其通常由 各个部分的子对象用一定算法构成;由于需求的变化,这个 复杂对象的各个部分经常面临着剧烈的变化,但是将它们 组合到一起的算法却相对稳定。
如何应对种变化呢?如何提供一种"封装机制"来隔离出"复杂对象的各个部分"的变化,从而保持系统中的"稳定构建算法"不随需求的改变而改变?
意图(Intent):
将一个 复杂对象的构建与其表示相分离,使得 同样的构建过程可以创建不同的表示。
-------《设计模式》GOF
结构图(Struct):
协作(Collaborations):
生活中的例子:
适用性:
1.当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。
2.当构造过程必须允许被构造的对象有不同的表示时。
实例代码:
Builder类:
假设创建游戏中的一个房屋House设施,该房屋的构建由几部分组成,且各个部分富于变化。如果使用最直观的设计方法,每一个房屋部分的变化,都将导致房屋构建的重新修正.....
动机(Motivation):
在软件系统中,有时候面临 一个"复杂对象"的创建工作,其通常由 各个部分的子对象用一定算法构成;由于需求的变化,这个 复杂对象的各个部分经常面临着剧烈的变化,但是将它们 组合到一起的算法却相对稳定。
如何应对种变化呢?如何提供一种"封装机制"来隔离出"复杂对象的各个部分"的变化,从而保持系统中的"稳定构建算法"不随需求的改变而改变?
意图(Intent):
将一个 复杂对象的构建与其表示相分离,使得 同样的构建过程可以创建不同的表示。
-------《设计模式》GOF
结构图(Struct):

协作(Collaborations):

生活中的例子:

适用性:
1.当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。
2.当构造过程必须允许被构造的对象有不同的表示时。
实例代码:
Builder类:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 public abstract class Builder
2 {
3 public abstract void BuildDoor();
4 public abstract void BuildWall();
5 public abstract void BuildWindows();
6 public abstract void BuildFloor();
7 public abstract void BuildHouseCeiling();
8
9 public abstract House GetHouse();
10 }
Director类:这一部分是
组合到一起的算法(相对稳定)。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 public abstract class Builder
2 {
3 public abstract void BuildDoor();
4 public abstract void BuildWall();
5 public abstract void BuildWindows();
6 public abstract void BuildFloor();
7 public abstract void BuildHouseCeiling();
8
9 public abstract House GetHouse();
10 }
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 public class Director
2 {
3 public void Construct(Builder builder)
4 {
5 builder.BuildWall();
6 builder.BuildHouseCeiling();
7 builder.BuildDoor();
8 builder.BuildWindows();
9 builder.BuildFloor();
10 }
11 }
ChineseBuilder类
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 public class Director
2 {
3 public void Construct(Builder builder)
4 {
5 builder.BuildWall();
6 builder.BuildHouseCeiling();
7 builder.BuildDoor();
8 builder.BuildWindows();
9 builder.BuildFloor();
10 }
11 }
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 public class ChineseBuilder:Builder
2 {
3 private House ChineseHouse = new House();
4 public override void BuildDoor()
5 {
6 Console.WriteLine( " this Door 's style of Chinese " );
7 }
8 public override void BuildWall()
9 {
10 Console.WriteLine( " this Wall 's style of Chinese " );
11 }
12 public override void BuildWindows()
13 {
14 Console.WriteLine( " this Windows 's style of Chinese " );
15 }
16 public override void BuildFloor()
17 {
18 Console.WriteLine( " this Floor 's style of Chinese " );
19 }
20 public override void BuildHouseCeiling()
21 {
22 Console.WriteLine( " this Ceiling 's style of Chinese " );
23 }
24 public override House GetHouse()
25 {
26 return ChineseHouse;
27 }
28 }
RomanBuilder类:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 public class ChineseBuilder:Builder
2 {
3 private House ChineseHouse = new House();
4 public override void BuildDoor()
5 {
6 Console.WriteLine( " this Door 's style of Chinese " );
7 }
8 public override void BuildWall()
9 {
10 Console.WriteLine( " this Wall 's style of Chinese " );
11 }
12 public override void BuildWindows()
13 {
14 Console.WriteLine( " this Windows 's style of Chinese " );
15 }
16 public override void BuildFloor()
17 {
18 Console.WriteLine( " this Floor 's style of Chinese " );
19 }
20 public override void BuildHouseCeiling()
21 {
22 Console.WriteLine( " this Ceiling 's style of Chinese " );
23 }
24 public override House GetHouse()
25 {
26 return ChineseHouse;
27 }
28 }
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 class RomanBuilder:Builder
2 {
3 private House RomanHouse = new House();
4 public override void BuildDoor()
5 {
6 Console.WriteLine( " this Door 's style of Roman " );
7 }
8 public override void BuildWall()
9 {
10 Console.WriteLine( " this Wall 's style of Roman " );
11 }
12 public override void BuildWindows()
13 {
14 Console.WriteLine( " this Windows 's style of Roman " );
15 }
16 public override void BuildFloor()
17 {
18 Console.WriteLine( " this Floor 's style of Roman " );
19 }
20 public override void BuildHouseCeiling()
21 {
22 Console.WriteLine( " this Ceiling 's style of Roman " );
23 }
24 public override House GetHouse()
25 {
26 return RomanHouse;
27 }
28 }
ChineseBuilder和RomanBuilder这两个是:
这个复杂对象的两个部分经常面临着剧烈的变化。Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 class RomanBuilder:Builder
2 {
3 private House RomanHouse = new House();
4 public override void BuildDoor()
5 {
6 Console.WriteLine( " this Door 's style of Roman " );
7 }
8 public override void BuildWall()
9 {
10 Console.WriteLine( " this Wall 's style of Roman " );
11 }
12 public override void BuildWindows()
13 {
14 Console.WriteLine( " this Windows 's style of Roman " );
15 }
16 public override void BuildFloor()
17 {
18 Console.WriteLine( " this Floor 's style of Roman " );
19 }
20 public override void BuildHouseCeiling()
21 {
22 Console.WriteLine( " this Ceiling 's style of Roman " );
23 }
24 public override House GetHouse()
25 {
26 return RomanHouse;
27 }
28 }
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 public class Client
2 {
3 public static void Main( string [] args)
4 {
5 Director director = new Director();
6
7 Builder instance;
8
9 Console.WriteLine( " Please Enter House No: " );
10
11 string No = Console.ReadLine();
12
13 string houseType = ConfigurationSettings.AppSettings[ " No " + No];
14
15 instance = (Builder)Assembly.Load( " House " ).CreateInstance( " House. " + houseType);
16
17 director.Construct(instance);
18
19 House house = instance.GetHouse();
20 house.Show();
21
22 Console.ReadLine();
23 }
24 }
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 public class Client
2 {
3 public static void Main( string [] args)
4 {
5 Director director = new Director();
6
7 Builder instance;
8
9 Console.WriteLine( " Please Enter House No: " );
10
11 string No = Console.ReadLine();
12
13 string houseType = ConfigurationSettings.AppSettings[ " No " + No];
14
15 instance = (Builder)Assembly.Load( " House " ).CreateInstance( " House. " + houseType);
16
17 director.Construct(instance);
18
19 House house = instance.GetHouse();
20 house.Show();
21
22 Console.ReadLine();
23 }
24 }
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 <? xml version = " 1.0 " encoding = " utf-8 " ?>
2 < configuration >
3 < appSettings >
4 < add key = " No1 " value = " RomanBuilder " ></ add >
5 < add key = " No2 " value = " ChineseBuilder " ></ add >
6 </ appSettings >
7 </ configuration >
Builder模式的几个要点:
Builder模式 主要用于“分步骤构建一个复杂的对象”。在这其中“分步骤”是一个稳定的乘法,而复杂对象的各个部分则经常变化。
Builder模式主要在于应对“复杂对象各个部分”的频繁需求变动。其缺点在于难以应对“分步骤构建算法”的需求变动。
Abstract Factory模式解决“系列对象”的需求变化,Builder模式解决“对象部分”的需求变化。Builder械通常和Composite模式组合使用。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> 1 <? xml version = " 1.0 " encoding = " utf-8 " ?>
2 < configuration >
3 < appSettings >
4 < add key = " No1 " value = " RomanBuilder " ></ add >
5 < add key = " No2 " value = " ChineseBuilder " ></ add >
6 </ appSettings >
7 </ configuration >
Builder模式的几个要点:
Builder模式 主要用于“分步骤构建一个复杂的对象”。在这其中“分步骤”是一个稳定的乘法,而复杂对象的各个部分则经常变化。
Builder模式主要在于应对“复杂对象各个部分”的频繁需求变动。其缺点在于难以应对“分步骤构建算法”的需求变动。
Abstract Factory模式解决“系列对象”的需求变化,Builder模式解决“对象部分”的需求变化。Builder械通常和Composite模式组合使用。