Building父類別
1: class Building
2: {3: public Building() { }
4: private string name;
5: public Building(string name) { this.name = name; }
6: 7: public virtual void Show()
8: {9: Console.WriteLine(" {0} 附贈家具", name);
10: }11: protected Building component;
12: public void Decortate(Building component)
13: {14: this.component = component;
15: } 16: }Furniture類別繼承Building
1: class Furniture : Building
2: {3: //protected Building component;
4: //public void Decortate(Building component)
5: //{
6: // this.component = component;
7: //}
8: public override void Show()
9: {10: if(component!=null)
11: { 12: component.Show(); 13: } 14: } 15: }
各類家具分別繼承Furniture
1: class Sofa:Furniture
2: {3: public override void Show()
4: {5: Console.Write("+沙發 ");
6: base.Show();
7: } 8: }9: class HighClassSofa : Furniture
10: {11: public override void Show()
12: {13: Console.Write("+高級沙發");
14: base.Show();
15: } 16: }17: class BulbLight: Furniture
18: {19: public override void Show()
20: {21: Console.Write("+電燈泡 ");
22: base.Show();
23: } 24: }25: class CrystalLight: Furniture
26: {27: public override void Show()
28: {29: Console.Write("+水晶燈 ");
30: base.Show();
31: } 32: }33: class WoodenChair : Furniture
34: {35: public override void Show()
36: {37: Console.Write("+木製椅 ");
38: base.Show();
39: } 40: }41: class BossChair : Furniture
42: {43: public override void Show()
44: {45: Console.Write("+董事長椅 ");
46: base.Show();
47: } 48: } 49: 50: class Kitchenware : Furniture
51: {52: public override void Show()
53: {54: Console.Write("+廚具 ");
55: base.Show();
56: } 57: }58: class SystemKitchenware: Furniture
59: {60: public override void Show()
61: {62: Console.Write("+系統廚具 ");
63: base.Show();
64: } 65: }66: class SpringBed : Furniture
67: {68: public override void Show()
69: {70: Console.Write("+彈簧床 ");
71: base.Show();
72: } 73: }----------------------------------------------------------------------------------------------------
客戶端測試1
1. Line 6: 高級沙發 裝飾 帝寶 => sofa1.Decorate(bd1)
2. Line7: 水晶燈 裝飾 高級沙發(帝寶) => light1.Decorate(sofa1)
3. Line8: 董事長椅 裝飾 水晶燈(高級沙發、帝寶) => chair1.Decorate(light1)
4. Line9: 系統廚具 裝飾 董事長椅(水晶燈、高級沙發、帝寶) => kitchenware1.Decorate(chair1)
如果以Boxing示意,外層包裝內層, 發動順序由最外面開始Unboxing。
如果以Block Diagram來看,包裝順序是從索引N-1開始包裝索引N,包裝完成後由最後一個(最外層)裝飾者(kitchenware1)發動
5. Line10: 列印結果如下
kitchware1.Show() => chair1.Show() => light1.Show() => sofa1.Show() => bd1.Show()
1: Building bd1 = new Building("帝寶"); // 建築
2: HighClassSofa sofa1 = new HighClassSofa(); // 高級沙發
3: CrystalLight light1 = new CrystalLight(); // 水晶燈
4: BossChair chair1 = new BossChair(); // 董事長椅
5: SystemKitchenware kitchenware1 = new SystemKitchenware(); // 系統廚具
6: sofa1.Decortate(bd1); 7: light1.Decortate(sofa1); 8: chair1.Decortate(light1); 9: kitchenware1.Decortate(chair1); 10: kitchenware1.Show();客戶端測試2
1. Line 6: 沙發 裝飾 民宅 => sofa2.Decorate(bd2)
2. Line7: 電燈泡 裝飾 沙發(民宅) => light2.Decorate(sofa2)
3. Line8: 木製椅 裝飾 電燈泡(沙發、民宅) => chair2.Decorate(light2)
4. Line9: 廚具 裝飾 木製椅(電燈泡、沙發、民宅) => kitchenware1.Decorate(chair1)
5. Line10: 列印結果如下
kitchware2.Show() => chair2.Show() => light2.Show() => sofa2.Show() => bd2.Show()
1: Building bd2 = new Building("民宅"); // 建築
2: Sofa sofa2 = new Sofa(); // 沙發
3: BulbLight light2 = new BulbLight(); // 電燈泡
4: WoodenChair chair2 = new WoodenChair(); // 木製椅
5: Kitchenware kitchenware2 = new Kitchenware(); // 廚具
6: sofa2.Decortate(bd2); 7: light2.Decortate(sofa2); 8: chair2.Decortate(light2); 9: kitchenware2.Decortate(chair2); 10: kitchenware2.Show();客戶端測試3
1. Line4~Line8 :將裝飾物預先(依序)置放於陣列中
2. Line9~Line12: bdList[i]裝飾bdList[i-1]
3. 列印最後一個物件bdList[bdList.Count-1],即列印廚具,而廚具包含(木製椅),木製椅又包含(電燈泡),電燈泡又包含(沙發),沙發又包含(Ryan’s House),因此物件鏈可以一路執行列印動作…。
1: Console.WriteLine("(1)儲存序列於陣列型態...");
2: Building[] bdList = new Building[5];
3: 4: bdList[0] = new Building("Ryan's House");
5: bdList[1] = new Sofa();
6: bdList[2] = new BulbLight();
7: bdList[3] = new WoodenChair();
8: bdList[4] = new Kitchenware();
9: for(int i=bdList.Length-1; i>=1; i-- )
10: { 11: bdList[i].Decortate(bdList[i - 1]); 12: } 13: bdList[bdList.Length - 1].Show();
改成下列排列方式,對於裝飾順序較為直覺(裝飾順序和執行順序一致)。
1. 最外層放在第1個索引,往內一層放在第2個索引,以此類推…。
2. Line 10~13: bdList[i]裝飾bdList[i+1],與方塊流程執行順序一致(直覺)。
3. bdList[0].Show()開始物件鏈執行,也是執行第一個方塊圖。
1: Building[] bdList = new Building[5];
2: bdList[0] = new Kitchenware();
3: bdList[1] = new WoodenChair();
4: bdList[2] = new BulbLight();
5: bdList[3] = new Sofa();
6: bdList[4] = new Building("Ryan's House");
7: 8: 9: 10: for (int i = 0; i <bdList.Length-1; i++)
11: { 12: bdList[i].Decortate(bdList[i+1]); 13: } 14: bdList[0].Show();
客戶端測試4
1. Line3~Line7 :將裝飾物預先(依序)置放於List中
2. Line8~Line11: bdList2[i]裝飾bdList2[i-1]
3. 列印最後一個物件bdList2[bdList.Length-1],即列印廚具,而廚具包含(木製椅),木製椅又包含(電燈泡),電燈泡又包含(沙發),沙發又包含(Ryan’s House),因此物件鏈可以一路直行列印動作…。
1: Console.WriteLine("(2)儲存序列於List型態...");
2: List<Building> bdList2 = new List<Building>();
3: bdList2.Add(new Building("Ryan's House"));
4: bdList2.Add(new Sofa());
5: bdList2.Add(new BulbLight());
6: bdList2.Add(new WoodenChair());
7: bdList2.Add(new Kitchenware());
8: for(int i=bdList2.Count-1; i>=1; i--)
9: { 10: bdList2[i].Decortate(bdList2[i - 1]); 11: } 12: bdList2[bdList.Length - 1].Show();客戶端測試5
1. Line3 :將裝飾物 bdList2[1]~bdList2[ bdList2.Count-1 ]儲存序列顛倒
2. Line4~Line7: bdList2[i]裝飾bdList2[i-1]
3. 列印最後一個物件bdList2[bdList.Length-1],即列印沙發,而沙發包含(電燈泡),電燈泡又包含(木製椅),木製椅又包含(廚具),廚具又包含(Ryan’s House),因此物件鏈可以一路直行列印動作…。
1: Console.WriteLine("(3) 顛倒List序列...後");
2: 3: bdList2.Reverse(1, bdList2.Count-1);4: for (int i = bdList2.Count - 1; i >= 1; i--)
5: { 6: bdList2[i].Decortate(bdList2[i - 1]); 7: } 8: bdList2[bdList.Length - 1].Show();









留言列表
