动机(Motivation)
在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将他们组合在一起的算法却相对稳定。
如何应对这种变化》如何提供一种“封装机制”来隔离出“复杂对象的各个部分”的变化,从而保持系统中的“稳定构建算法”不随着需求改变而改变?
意图(Intent)
将一个复杂复杂对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
结构(Structure)
代码实现
1 对应Builder部分 2 public abstract class Builder 3 { 4 public abstract Door BuildeDoor(); 5 public abstract Window BuildeWindow(); 6 public abstract Floor BuildeFloor(); 7 8 public abstract House GetHouse(); 9 } 10 public abstract class House 11 { 12 private Door door; 13 14 public Door Door 15 { 16 get { return door; } 17 set { door = value; } 18 } 19 private Window window; 20 21 public Window Window 22 { 23 get { return window; } 24 set { window = value; } 25 } 26 private Floor floor; 27 28 public Floor Floor 29 { 30 get { return floor; } 31 set { floor = value; } 32 } 33 public abstract void SayHello(); 34 } 35 public abstract class Door 36 { 37 private string _name; 38 39 public string Name 40 { 41 get { return _name; } 42 set { _name = value; } 43 } 44 } 45 public abstract class Window 46 { 47 private string _name; 48 49 public string Name 50 { 51 get { return _name; } 52 set { _name = value; } 53 } 54 } 55 public abstract class Floor 56 { 57 private string _name; 58 59 public string Name 60 { 61 get { return _name; } 62 set { _name = value; } 63 } 64 }
1 对应ConcreteBuilder部分 2 public class ModenBuilder : Builder 3 { 4 private Door door; 5 private Window window; 6 private Floor floor; 7 public override Door BuildeDoor() 8 { 9 door = new ClassicDoor("古典的门"); 10 return door; 11 } 12 13 public override Window BuildeWindow() 14 { 15 window = new ModenWindow("摩登的窗"); 16 return window; 17 } 18 19 public override Floor BuildeFloor() 20 { 21 floor = new ModenFloor("摩登的楼"); 22 return floor; 23 } 24 25 public override House GetHouse() 26 { 27 return new ModenHouse(door,window,floor); 28 } 29 } 30 public class ClassicDoor : Door 31 { 32 public ClassicDoor(string name) 33 { 34 this.Name = name; 35 } 36 } 37 public class ModenWindow : Window 38 { 39 public ModenWindow(string name) 40 { 41 this.Name = name; 42 } 43 } 44 public class ModenFloor : Floor 45 { 46 public ModenFloor(string name) 47 { 48 this.Name = name; 49 } 50 } 51 public class ModenHouse:House 52 { 53 public ModenHouse(Door door, Window window, Floor floor) 54 { 55 this.Door = door; 56 this.Window = window; 57 this.Floor = floor; 58 } 59 public override void SayHello() 60 { 61 Console.WriteLine("{0}-----{1}---------{2}",this.Door.Name,this.Window.Name,this.Floor.Name); 62 } 63 }
1 对应Director部分 2 public class Manager 3 { 4 public static House GetHouse(Builder builder) 5 { 6 builder.BuildeDoor(); 7 builder.BuildeWindow(); 8 builder.BuildeFloor(); 9 10 return builder.GetHouse(); 11 } 12 }
1 主函数的调用 2 static void Main(string[] args) 3 { 4 House house = Manager.GetHouse(new ModenBuilder()); 5 house.SayHello(); 6 Console.ReadKey(); 7 8 }
Builder模式的几个要点
Builder模式主要用于“分步骤构建一个复杂的对象”。在其中“分步骤”是一个稳定的算法,而复杂对象的各个部分则经常变化。
变化点在哪里,封装哪里---Builder模式主要在于应对“复杂对象各个部分”的频繁需求变动。其缺点在于难以应对“分步骤构建算法”的需求变动。
Abstract Factory模式解决“系列对象”的需求变化,Builder模式解决“对象部分”的需求变化。Builder模式通常个Composite模式组合使用。