[Unity 代码写法整理]嵌套判断问题(二)

4.前面三种都是需要if else switch case去判断的,现在写一种不需要条件判断的。思想是以多数服从少数,多数包含少数的实现,少数调用。文字上难以理解,直接上代码比较好。还是防具和武器播放不同动画的栗子,这里假如武器种类多,防具数目比较少,也就是武器是多数,防具是少数

public class ArmorBase
{
    public virtual void PlayAnimation()
    {
        //for override
    }
}

public interface IWeapon
{
    void PlayLeatherAnimation();
    void PlayChainAnimation();
    void PlayPlateAnimation();
}

//装备系统,作为一个单例,
public class EquipmentSystem : Singleton<EquipmentSystem>
{
    private IWeapon currentWeapon;
    private ArmorBase currentArmor;

    public void SetWeapon(IWeapon weapon)
    {
        currentWeapon = weapon;
    }

    public void SetArmor(ArmorBase armor)
    {
        currentArmor = armor;
    }

    public void PlayAnimation()
    {
        currentArmor.PlayAnimation();
    }
}

//只写Leather,Chain和Plate同理
public class Leather : ArmorBase
{
    public override void PlayAnimation()
    {
        EquipmentSystem.Instance.currentWeapon.PlayLeatherAnimation();
    }
}

//只写个Sword,其他武器同理
public class Sword : IWeapon
{
    public void PlayLeatherAnimation()
    {
        //拿剑穿皮甲的动画
    }

    public void PlayChainAnimation()
    {
        //拿剑穿锁甲的动画
    }

    public void PlayPlateAnimation()
    {
        //拿剑穿板甲的动画
    }
}

int Main()
{
    EquipmentSystem.Instance.SetWeapon(new Sword());
    EquipmentSystem.Instance.SetArmor(new Leather());
    EquipmentSystem.Instance.PlayAnimation();
}

其实这个跟第一种方法还是有点像的,只不过武器类一个函数转化为三个函数,不过还是有区别的。第一种方法实现和调用是同一个函数,而这种方式,实现和调用的函数分离开来,而且摆脱了条件判断对代码可读性的影响,更加容易定位出现的问题。

==============================

5.如果嵌套判断不止两层,有多层怎么办。其实这个问题就跟拧魔方一样,是一个降阶问题。最理想的情况,是要将嵌套判断的维度下降到一维。

if(X1)
{
    if(Y1)
    {
        if(Z1)
        {

        }
    }
}

这种判断如果多了,会很痛苦。

if(X1 && Y1 && Z1)
{
    
}

这样就将三维的判断降低到一维。

==============================

6.嵌套判断降低维度的一些方法

一种是用&&和||去细化判断条件,不过判断条件越多就越长越乱。更合理的办法是通过将判断条件封装成对象,然后写一个Utils类或者重写运算符的方式,把判断的条件创建一个模板对象。栗子如下:

public class StudentConditionClass
{
    public int age;
    public float height;
    
    public StudentConditionClass(int ageArg,float heightArg)

    //重载运算符
    public static bool operator ==(StudentConditionClass studen1, StudentConditionClass student2)
    {
        return student1.age == student2.age && student1.height == student2.height;
    }   

    //重载运算符
    public static bool operator !=(StudentConditionClass studen1, StudentConditionClass student2)
    {
        return !(student1.age == student2.age && student1.height == student2.height);
    }  
}

public class JudgeUtils
{
    //写一个Utils类去判断
    public static bool IsStudentEqual(StudentConditionClass studen1,StudentConditionClass student2)
    {
        return student1.age == student2.age && student1.height == student2.height;
    }
}

int Main()
{
    StudentConditionClass student = new StudentConditionClass(18,183.1f);
    //判断模板1
    StudentConditionClass studentTemplete1 = new StudentConditionClass(18,183.1f);
    //判断模板2
    StudentConditionClass studentTemplete2 = new StudentConditionClass(19,190f);

    //重载运算符判断
    if(student == studentTemplete1)
    {
        //XXX
    }
    //Utils判断
    else if(JudgeUtils.IsStudentEqual(studen,studentTemplete2))
    {
        //XXX
    }
}

这样的话判断条件会简化,也更易读。不过会有多余内存占用,毕竟每一个用于判断的模板也是对象。所以最终使用的时候还是要根据情况选择最优的方法判断。

==============================

OK嵌套判断的理解就告一段落了,欢迎讨论。下一周开始写一些跟设计模式有关的代码写法了~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值