IronMan之組合
在上個篇幅中講到怎麼把“武器”裝飾到“部件”上,這個篇幅呢,還是要講到“武器”,不過呢是關於“武器”使用的。
本篇介紹"武器"的合理使用方式,不說廢話,直接來看起初使用遇到的問題:
一起來看一下“武器”的定義:
1 public abstract class WeaponUpgradeLevel1
2 {
3 protected List<WeaponUpgradeLevel1> weaponUpgrades=new List<WeaponUpgradeLevel1>();
4
5 public List<WeaponUpgradeLevel1> WeaponUpgrades
6 {
7 get
8 {
9 return weaponUpgrades;
10 }
11
12 }
13 /// <summary>
14 /// 判斷是“部分”對象 還是 “整體”對象
15 /// </summary>
16 public bool IsSingle
17 {
18 get
19 {
20 if (weaponUpgrades.Count > 0)
21 {
22 return false;
23 }
24 else
25 {
26 return true;
27 }
28 }
29 }
30 /// <summary>
31 /// 攻擊
32 /// </summary>
33 public abstract void Attack();
34 /// <summary>
35 /// 添加 “部分”對象或者是“整體”對象 到“整體”對象
36 /// </summary>
37 /// <param name="weaponupgrade"></param>
38 /// <returns></returns>
39 public virtual bool Add(WeaponUpgradeLevel1 weaponupgrade)
40 {
41 if (weaponupgrade != null)
42 {
43 WeaponUpgrades.Add(weaponupgrade);
44 return true;
45 }
46 else { return false; }
47
48 }
49 /// <summary>
50 /// 從“整體”對象中移除 “部分”對象或者是“整體”對象
51 /// </summary>
52 /// <param name="weaponupgrade"></param>
53 /// <returns></returns>
54 public virtual bool Remove(WeaponUpgradeLevel1 weaponupgrade)
55 {
56 if (weaponupgrade != null)
57 {
58 if (WeaponUpgrades.Contains(weaponupgrade))
59 {
60 WeaponUpgrades.Remove(weaponupgrade);
61 return true;
62 }
63 else
64 {
65 return false;
66 }
67 }
68 else { return false; }
69 }
70 }
71 public class Rocket : WeaponUpgradeLevel1
72 {
73 private string _name = "火箭炮";
74
75 public override void Attack()
76 {
77 Console.WriteLine("使用" + _name + "進行攻擊");
78 }
79 }
80 public class RocketLevel1 : WeaponUpgradeLevel1
81 {
82 private string _name = "火箭炮(EX.2X2)";//由四個火箭炮組成
83
84 public override void Attack()
85 {
86 Console.WriteLine("使用"+_name+"進行攻擊");
87 }
88 }
上面定義了三種類型,WeaponUpgradeLevel1是“武器”的抽象,並且在其中定義了一些屬性和方法,
用於表示實現了此“武器”抽象的類型是否是核心武器(部分)還是核心武器的外殼(整體),
並且也在後面實現了“武器”抽象,分別是Rocket核心武器(部分)和RocketLevel1核心武器的外殼(整體),這樣的結構定義好了過後,我們來看一下子,怎麼使用它們:
1 WeaponUpgradeLevel1 weaRocket1 = new Rocket(); 2 WeaponUpgradeLevel1 weaRocket2 = new Rocket(); 3 WeaponUpgradeLevel1 weaRocket3 = new Rocket(); 4 WeaponUpgradeLevel1 weaRocket4 = new Rocket(); 5 6 WeaponUpgradeLevel1 weaRocketlevel1 = new RocketLevel1(); 7 weaRocketlevel1.Add(weaRocket1); 8 weaRocketlevel1.Add(weaRocket2); 9 weaRocketlevel1.Add(weaRocket3); 10 weaRocketlevel1.Add(weaRocket4);
這時候 weaRocketlevel1示例是像圖1這樣的:
圖1

圖2

要是使用weaRocketlevel1,真正的目的不是使用它本身,而是使用它裡面的小火箭炮(也就是weaRocket1……)。 如果想要使用裡面的小火箭炮,並不是簡簡單單的遍歷一下就可以了,從現在的情況來看,確實是很簡單的遍歷
,獲取到每個火箭炮,並且使用它們, 但是如果這個"火箭炮(EX.2X2)"改成了"火箭炮(EX.8X8)"呢? 並且"火箭炮(EX.8X8)"是由 4個"火箭炮(EX.4X4)"組成,每個"火箭炮(EX.4X4)"是由4個"火箭炮(EX.2X2)"組成的。 在這樣的情況下怎麼辦? 沒錯了,是使用遞歸來遍歷,然後的情況就是如圖3所示:
圖3

這樣來看,也確實沒什麼大問題,只是耦合度比較高。
使用設計模式可以在特定的情況下解耦,這裡的情況比較適合Composite模式。
將對象組合成樹形結構以表示“部分-整體”的層次結構。Composite模式使得用戶對單個對象和組合
對象的使用具有一致性。
[GOF 《設計模式》]
根據設計的中心思想,看一下修改後的結構:
1 public abstract class WeaponUpgradeLevel1
2 {
3 protected List<WeaponUpgradeLevel1> weaponUpgrades=new List<WeaponUpgradeLevel1>();
4
5 public List<WeaponUpgradeLevel1> WeaponUpgrades
6 {
7 get
8 {
9 return weaponUpgrades;
10 }
11
12 }
13 /// <summary>
14 /// 判斷是“部分”對象 還是 “整體”對象。
15 /// true為“部分”對象 反之相對
16 /// </summary>
17 public bool IsSingle
18 {
19 get
20 {
21 if (weaponUpgrades.Count > 0)
22 {
23 return false;
24 }
25 else
26 {
27 return true;
28 }
29 }
30 }
31 /// <summary>
32 /// 攻擊
33 /// </summary>
34 public virtual void Attack()
35 {
36 ActionAttack(this);
37 }
38 private void ActionAttack(WeaponUpgradeLevel1 weaponupgrade)
39 {
40 if (weaponupgrade.IsSingle)
41 {
42 weaponupgrade.Attack();
43 }
44 else
45 {
46 foreach (WeaponUpgradeLevel1 weapon in weaponupgrade.WeaponUpgrades)
47 {
48 ActionAttack(weapon);
49 }
50 }
51 }
52 /// <summary>
53 /// 添加 “部分”對象或者是“整體”對象 到“整體”對象
54 /// </summary>
55 /// <param name="weaponupgrade"></param>
56 /// <returns></returns>
57 public virtual bool Add(WeaponUpgradeLevel1 weaponupgrade)
58 {
59 if (weaponupgrade != null)
60 {
61 WeaponUpgrades.Add(weaponupgrade);
62 return true;
63 }
64 else { return false; }
65
66 }
67 /// <summary>
68 /// 從“整體”對象中移除 “部分”對象或者是“整體”對象
69 /// </summary>
70 /// <param name="weaponupgrade"></param>
71 /// <returns></returns>
72 public virtual bool Remove(WeaponUpgradeLevel1 weaponupgrade)
73 {
74 if (weaponupgrade != null)
75 {
76 if (WeaponUpgrades.Contains(weaponupgrade))
77 {
78 WeaponUpgrades.Remove(weaponupgrade);
79 return true;
80 }
81 else
82 {
83 return false;
84 }
85 }
86 else { return false; }
87 }
88 }
89 public class Rocket : WeaponUpgradeLevel1
90 {
91 private string _name = "火箭炮";
92
93 public override void Attack()
94 {
95 Console.WriteLine("使用" + _name + "進行攻擊");
96 }
97 }
98 public class RocketLevel1 : WeaponUpgradeLevel1
99 {
100 private string _name = "火箭炮(EX.2X2)";//由四個火箭炮組成
101
102 public override void Attack()
103 {
104 base.Attack();
105 Console.WriteLine("使用"+_name+"進行攻擊");
106 }
107 }
看一下現在的客戶端怎麼使用“火箭炮”:
1 WeaponUpgradeLevel1 weaRocket1 = new Rocket(); 2 WeaponUpgradeLevel1 weaRocket2 = new Rocket(); 3 WeaponUpgradeLevel1 weaRocket3 = new Rocket(); 4 WeaponUpgradeLevel1 weaRocket4 = new Rocket(); 5 WeaponUpgradeLevel1 weaRocketlevel1 = new RocketLevel1(); 6 weaRocketlevel1.Add(weaRocket1); 7 weaRocketlevel1.Add(weaRocket2); 8 weaRocketlevel1.Add(weaRocket3); 9 weaRocketlevel1.Add(weaRocket4); 10 weaRocketlevel1.Attack();
所示結果如圖4
圖4

根據設計模式的思想修改代碼的區別是在“武器”抽象中把對“核心武器”的使用做了統一的使用。意思就不做闡述了可以自己理解理解。
作者:金源
出處:http://www.cnblogs.com/jin-yuan/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面