设计模式之建造者模式
1 /// <summary> 2 /// 指导者 指导建造者 建造事物
// 比方说 设计师 施工的工人 在设计师的指导下,工人按照设计师的设计图纸 进行实行工作 3 /// </summary> 4 class Program 5 { 6 static void Main(string[] args) 7 { 8 AbstractBuilder absBuilder = new Builder(); 9 Director director = new Director(); 10 director.Constract(absBuilder); 11 House house = absBuilder.GetResult(); 12 house.Show(); 13 14 Console.Read(); 16 } 17 } 18 19 //指导者 20 public class Director 21 { 22 public void Constract(AbstractBuilder builder) 23 { 24 builder.BuilderDoor(); 25 builder.BuilderWall(); 26 builder.BuilderWindows(); 27 } 28 } 29 30 //抽象的建造者 31 public abstract class AbstractBuilder 32 { 33 public abstract void BuilderDoor(); 34 public abstract void BuilderWindows(); 35 public abstract void BuilderWall(); 36 public abstract House GetResult(); 37 38 } 39 // 建造者 40 public class Builder:AbstractBuilder 41 { 42 House house = new House(); 43 public override void BuilderDoor() 44 { 45 house.Add("Builder Door"); 46 } 47 48 public override void BuilderWindows() 49 { 50 house.Add("Builder Windows"); 51 } 52 53 public override void BuilderWall() 54 { 55 house.Add("Builder Wall"); 56 } 57 58 public override House GetResult() 59 { 60 return house; 61 } 62 } 63 64 //要建造的事物 65 public class House 66 { 67 List<string> list = new List<string>(); 68 public void Add(string housePart) 69 { 70 list.Add(housePart); 71 } 72 public void Show() 73 { 74 foreach (var item in list) 75 { 76 Console.WriteLine(item); 77 } 78 } 79 }