在这里我们再给出另外一个关于建造房子的例子。客户程序通过调用指导者 (CDirector class)的BuildHouse()方法来创建一个房子。该方法有一个布尔型的参数blnBackyard,当blnBackyard为假时指导者将创建一个Apartment(Concrete Builder),当它为真时将创建一个Single Family Home(Concrete Builder)。这两种房子都实现了接口Ihouse。
程序实现:
1//关于建造房屋的例子 2using System; 3using System.Collections; 4 5/**//// <summary> 6/// 抽象建造者 7/// </summary> 8public interface IHouse 9{ 10 bool GetBackyard(); 11 long NoOfRooms(); 12 string Description(); 13} 14 15/**//// <summary> 16/// 具体建造者 17/// </summary> 18public class CApt:IHouse 19{ 20 private bool mblnBackyard; 21 private Hashtable Rooms; 22 public CApt() 23 { 24 CRoom room; 25 Rooms = new Hashtable(); 26 room = new CRoom(); 27 room.RoomName = "Master Bedroom"; 28 Rooms.Add ("room1",room); 29 30 room = new CRoom(); 31 room.RoomName = "Second Bedroom"; 32 Rooms.Add ("room2",room); 33 34 room = new CRoom(); 35 room.RoomName = "Living Room"; 36 Rooms.Add ("room3",room); 37 38 mblnBackyard = false; 39 } 40 41 public bool GetBackyard() 42 { 43 return mblnBackyard; 44 } 45 public long NoOfRooms() 46 { 47 return Rooms.Count; 48 } 49 public string Description() 50 { 51 IDictionaryEnumerator myEnumerator = Rooms.GetEnumerator(); 52 string strDescription; 53 strDescription = "This is an Apartment with " + Rooms.Count + " Rooms \n"; 54 strDescription = strDescription + "This Apartment doesn't have a backyard \n"; 55 while (myEnumerator.MoveNext()) 56 { 57 strDescription = strDescription + "\n" + myEnumerator.Key + "\t" + ((CRoom)myEnumerator.Value).RoomName; 58 } 59 return strDescription; 60 } 61} 62 63/**//// <summary> 64/// 具体建造者 65/// </summary> 66public class CSFH:IHouse 67{ 68 private bool mblnBackyard; 69 private Hashtable Rooms; 70 public CSFH() 71 { 72 CRoom room; 73 Rooms = new Hashtable(); 74 75 room = new CRoom(); 76 room.RoomName = "Master Bedroom"; 77 Rooms.Add ("room1",room); 78 79 room = new CRoom(); 80 room.RoomName = "Second Bedroom"; 81 Rooms.Add ("room2",room); 82 83 room = new CRoom(); 84 room.RoomName = "Third Room"; 85 Rooms.Add ("room3",room); 86 87 room = new CRoom(); 88 room.RoomName = "Living Room"; 89 Rooms.Add ("room4",room); 90 91 room = new CRoom(); 92 room.RoomName = "Guest Room"; 93 Rooms.Add ("room5",room); 94 95 mblnBackyard = true; 96 97 } 98 99 public bool GetBackyard() 100 { 101 return mblnBackyard; 102 } 103 public long NoOfRooms() 104 { 105 return Rooms.Count; 106 } 107 public string Description() 108 { 109 IDictionaryEnumerator myEnumerator = Rooms.GetEnumerator(); 110 string strDescription; 111 strDescription = "This is an Single Family Home with " + Rooms.Count + " Rooms \n"; 112 strDescription = strDescription + "This house has a backyard \n"; 113 while (myEnumerator.MoveNext()) 114 { 115 strDescription = strDescription + "\n" + myEnumerator.Key + "\t" + ((CRoom)myEnumerator.Value).RoomName; 116 } 117 return strDescription; 118 } 119} 120 121public interface IRoom 122{ 123 string RoomName{get;set;} 124} 125 126public class CRoom:IRoom 127{ 128 private string mstrRoomName; 129 public string RoomName 130 { 131 get 132 { 133 return mstrRoomName; 134 } 135 set 136 { 137 mstrRoomName = value; 138 } 139 } 140} 141 142/**//// <summary> 143/// 指导者 144/// </summary> 145public class CDirector 146{ 147 public IHouse BuildHouse(bool blnBackyard) 148 { 149 if (blnBackyard) 150 { 151 return new CSFH(); 152 } 153 else 154 { 155 return new CApt(); 156 } 157 } 158} 159 160/**//// <summary> 161/// 客户程序 162/// </summary> 163public class Client 164{ 165 static void Main(string[] args) 166 { 167 CDirector objDirector = new CDirector(); 168 IHouse objHouse; 169 170 string Input = Console.ReadLine(); 171 objHouse = objDirector.BuildHouse(bool.Parse(Input)); 172 173 Console.WriteLine(objHouse.Description()); 174 Console.ReadLine(); 175 } 176} 177 178