IronMan之組合
成都創(chuàng)新互聯(lián)公司專(zhuān)業(yè)為企業(yè)提供新民網(wǎng)站建設(shè)、新民做網(wǎng)站、新民網(wǎng)站設(shè)計(jì)、新民網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、新民企業(yè)網(wǎng)站模板建站服務(wù),10年新民做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
在上個(gè)篇幅中講到怎么把“武器”裝飾到“部件”上,這個(gè)篇幅呢,還是要講到“武器”,不過(guò)呢是關(guān)于“武器”使用的。
本篇介紹"武器"的合理使用方式,不說(shuō)廢話,直接來(lái)看起初使用遇到的問(wèn)題:
一起來(lái)看一下“武器”的定義:
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 /// 判斷是“部分”對(duì)象 還是 “整體”對(duì)象 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 /// 添加 “部分”對(duì)象或者是“整體”對(duì)象 到“整體”對(duì)象 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 /// 從“整體”對(duì)象中移除 “部分”對(duì)象或者是“整體”對(duì)象 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 + "進(jìn)行***"); 78 } 79 } 80 public class RocketLevel1 : WeaponUpgradeLevel1 81 { 82 private string _name = "火箭炮(EX.2X2)";//由四個(gè)火箭炮組成 83 84 public override void Attack() 85 { 86 Console.WriteLine("使用"+_name+"進(jìn)行***"); 87 } 88 }
上面定義了三種類(lèi)型,WeaponUpgradeLevel1是“武器”的抽象,并且在其中定義了一些屬性和方法,
用于表示實(shí)現(xiàn)了此“武器”抽象的類(lèi)型是否是核心武器(部分)還是核心武器的外殼(整體),
并且也在后面實(shí)現(xiàn)了“武器”抽象,分別是Rocket核心武器(部分)和RocketLevel1核心武器的外殼(整體),這樣的結(jié)構(gòu)定義好了過(guò)后,我們來(lái)看一下子,怎么使用它們:
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);
這時(shí)候 weaRocketlevel1示例是像圖1這樣的:
圖1
圖2
要是使用weaRocketlevel1,真正的目的不是使用它本身,而是使用它里面的小火箭炮(也就是weaRocket1……)。 如果想要使用里面的小火箭炮,并不是簡(jiǎn)簡(jiǎn)單單的遍歷一下就可以了,從現(xiàn)在的情況來(lái)看,確實(shí)是很簡(jiǎn)單的遍歷
,獲取到每個(gè)火箭炮,并且使用它們, 但是如果這個(gè)"火箭炮(EX.2X2)"改成了"火箭炮(EX.8X8)"呢? 并且"火箭炮(EX.8X8)"是由 4個(gè)"火箭炮(EX.4X4)"組成,每個(gè)"火箭炮(EX.4X4)"是由4個(gè)"火箭炮(EX.2X2)"組成的。 在這樣的情況下怎么辦? 沒(méi)錯(cuò)了,是使用遞歸來(lái)遍歷,然后的情況就是如圖3所示:
圖3
這樣來(lái)看,也確實(shí)沒(méi)什么大問(wèn)題,只是耦合度比較高。
使用設(shè)計(jì)模式可以在特定的情況下解耦,這里的情況比較適合Composite模式。
將對(duì)象組合成樹(shù)形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。Composite模式使得用戶(hù)對(duì)單個(gè)對(duì)象和組合
對(duì)象的使用具有一致性。
[GOF 《設(shè)計(jì)模式》]
根據(jù)設(shè)計(jì)的中心思想,看一下修改后的結(jié)構(gòu):
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 /// 判斷是“部分”對(duì)象 還是 “整體”對(duì)象。 15 /// true為“部分”對(duì)象 反之相對(duì) 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 /// 添加 “部分”對(duì)象或者是“整體”對(duì)象 到“整體”對(duì)象 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 /// 從“整體”對(duì)象中移除 “部分”對(duì)象或者是“整體”對(duì)象 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 + "進(jìn)行***"); 96 } 97 } 98 public class RocketLevel1 : WeaponUpgradeLevel1 99 { 100 private string _name = "火箭炮(EX.2X2)";//由四個(gè)火箭炮組成 101 102 public override void Attack() 103 { 104 base.Attack(); 105 Console.WriteLine("使用"+_name+"進(jìn)行***"); 106 } 107 }
看一下現(xiàn)在的客戶(hù)端怎么使用“火箭炮”:
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();
所示結(jié)果如圖4
圖4
根據(jù)設(shè)計(jì)模式的思想修改代碼的區(qū)別是在“武器”抽象中把對(duì)“核心武器”的使用做了統(tǒng)一的使用。意思就不做闡述了可以自己理解理解。
本文題目:C#設(shè)計(jì)模式之組合
文章起源:http://m.newbst.com/article20/pgcico.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、定制開(kāi)發(fā)、網(wǎng)站收錄、品牌網(wǎng)站制作、搜索引擎優(yōu)化、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)