大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C#技巧 > C#设计模式之组合

C#设计模式之组合

关键词:组合设计模式C#  阅读(666) 赞(17)

[摘要]本文是对C#设计模式之组合的讲解,对学习C#编程技术有所帮助,与大家分享。

本篇介绍"武器"的合理使用方式,不说废话,直接来看起初使用遇到的问题:

一起来看一下“武器”的定义:

     public abstract class WeaponUpgradeLevel1
     {
         protected List<WeaponUpgradeLevel1> weaponUpgrades=new List<WeaponUpgradeLevel1>();
 
         public List<WeaponUpgradeLevel1> WeaponUpgrades
         {
             get
             {
                 return weaponUpgrades;
             }
 
         }
         /// <summary>
         /// 判断是“部分”对象 还是 “整体”对象
         /// </summary>
         public bool IsSingle
         {
             get
             {
                 if (weaponUpgrades.Count > 0)
                 {
                     return false;
                 }
                 else
                 {
                     return true;
                 }
             }
         }
         /// <summary>
         /// 攻击
         /// </summary>
         public abstract void Attack();
         /// <summary>
         /// 添加    “部分”对象或者是“整体”对象      到“整体”对象
         /// </summary>
         /// <param name="weaponupgrade"></param>
         /// <returns></returns>
         public virtual bool Add(WeaponUpgradeLevel1 weaponupgrade)
         {
             if (weaponupgrade != null)
             {
                 WeaponUpgrades.Add(weaponupgrade);
                 return true;
             }
             else { return false; }
 
         }
         /// <summary>
         /// 从“整体”对象中移除 “部分”对象或者是“整体”对象
         /// </summary>
         /// <param name="weaponupgrade"></param>
         /// <returns></returns>
         public virtual bool Remove(WeaponUpgradeLevel1 weaponupgrade)
         {
             if (weaponupgrade != null)
             {
                 if (WeaponUpgrades.Contains(weaponupgrade))
                 {
                     WeaponUpgrades.Remove(weaponupgrade);
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
             else { return false; }
         }
     }
     public class Rocket : WeaponUpgradeLevel1
     {
         private string _name = "火箭炮";
 
         public override void Attack()
         {
             Console.WriteLine("使用" + _name + "进行攻击");
         }
     }
     public class RocketLevel1 : WeaponUpgradeLevel1
     {
         private string _name = "火箭炮(EX.2X2)";//由四个火箭炮组成
 
         public override void Attack()
         {
            Console.WriteLine("使用"+_name+"进行攻击");
         }
     }

上面定义了三种类型,WeaponUpgradeLevel1是“武器”的抽象,并且在其中定义了一些属性和方法,

用于表示实现了此“武器”抽象的类型是否是核心武器(部分)还是核心武器的外壳(整体),

并且也在后面实现了“武器”抽象,分别是Rocket核心武器(部分)和RocketLevel1核心武器的外壳(整体),这样的结构定义好了过后,我们来看一下子,怎么使用它们:

 WeaponUpgradeLevel1 weaRocket1 = new Rocket();
 WeaponUpgradeLevel1 weaRocket2 = new Rocket();            
 WeaponUpgradeLevel1 weaRocket3 = new Rocket();           
 WeaponUpgradeLevel1 weaRocket4 = new Rocket();
 
 WeaponUpgradeLevel1 weaRocketlevel1 = new RocketLevel1();
 weaRocketlevel1.Add(weaRocket1);
 weaRocketlevel1.Add(weaRocket2);
 weaRocketlevel1.Add(weaRocket3);
 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 《设计模式》]

根据设计的中心思想,看一下修改后的结构:

     public abstract class WeaponUpgradeLevel1
     {
         protected List<WeaponUpgradeLevel1> weaponUpgrades=new List<WeaponUpgradeLevel1>();
 
         public List<WeaponUpgradeLevel1> WeaponUpgrades
         {
             get
             {
                 return weaponUpgrades;
             }
 
         }
         /// <summary>
         /// 判断是“部分”对象 还是 “整体”对象。
         /// true为“部分”对象 反之相对
         /// </summary>
         public bool IsSingle
         {
             get
             {
                 if (weaponUpgrades.Count > 0)
                 {
                     return false;
                 }
                 else
                 {
                     return true;
                 }
             }
         }
         /// <summary>
         /// 攻击
         /// </summary>
         public virtual void Attack()
         {
             ActionAttack(this);
         }
         private void ActionAttack(WeaponUpgradeLevel1 weaponupgrade)
         {
             if (weaponupgrade.IsSingle)
             {
                 weaponupgrade.Attack();
             }
             else
             {
                 foreach (WeaponUpgradeLevel1 weapon in weaponupgrade.WeaponUpgrades)
                 {
                     ActionAttack(weapon);
                 }
             }
         }
         /// <summary>
         /// 添加    “部分”对象或者是“整体”对象      到“整体”对象
         /// </summary>
         /// <param name="weaponupgrade"></param>
         /// <returns></returns>
         public virtual bool Add(WeaponUpgradeLevel1 weaponupgrade)
         {
             if (weaponupgrade != null)
             {
                 WeaponUpgrades.Add(weaponupgrade);
                 return true;
             }
             else { return false; }
 
         }
         /// <summary>
         /// 从“整体”对象中移除 “部分”对象或者是“整体”对象
         /// </summary>
         /// <param name="weaponupgrade"></param>
         /// <returns></returns>
         public virtual bool Remove(WeaponUpgradeLevel1 weaponupgrade)
         {
             if (weaponupgrade != null)
             {
                 if (WeaponUpgrades.Contains(weaponupgrade))
                 {
                     WeaponUpgrades.Remove(weaponupgrade);
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
             else { return false; }
         }
     }
     public class Rocket : WeaponUpgradeLevel1
     {
         private string _name = "火箭炮";
 
         public override void Attack()
         {
             Console.WriteLine("使用" + _name + "进行攻击");
         }
     }
     public class RocketLevel1 : WeaponUpgradeLevel1
     {
         private string _name = "火箭炮(EX.2X2)";//由四个火箭炮组成
 
         public override void Attack()
         {
            base.Attack();
            Console.WriteLine("使用"+_name+"进行攻击");
         }
     }

看一下现在的客户端怎么使用“火箭炮”:

 WeaponUpgradeLevel1 weaRocket1 = new Rocket();
 WeaponUpgradeLevel1 weaRocket2 = new Rocket();
 WeaponUpgradeLevel1 weaRocket3 = new Rocket();
 WeaponUpgradeLevel1 weaRocket4 = new Rocket();
 WeaponUpgradeLevel1 weaRocketlevel1 = new RocketLevel1();
 weaRocketlevel1.Add(weaRocket1);
 weaRocketlevel1.Add(weaRocket2);
 weaRocketlevel1.Add(weaRocket3);
 weaRocketlevel1.Add(weaRocket4);
 weaRocketlevel1.Attack();

所示结果如图4

图4

根据设计模式的思想修改代码的区别是在“武器”抽象中把对“核心武器”的使用做了统一的使用。意思就不做阐述了可以自己理解理解。



相关评论