Unity【话大】设计模式之策略模式

前言:笔者在最开始写程序的时候经常会遇到一种情况,例如更改一个字段、或者添加一种小功能,就要把原来写过的东西几乎废弃掉,或者更改大量以前写过的代码。又或者自己写的东西时间久了再去回顾,完全找不到到时为什么这么写的头绪,如果遇到了Bug更是无法快速定位在哪里小范围出现的问题。如果你也经常遇到这种问题,就说明你现阶段非常需要学习下设计模式了

在网上经常说的设计模式有23种,也有一些更多的设计模式,无非也是从这些设计模式中变种而来。如果让笔者来形容什么是设计模式,我认为设计模式是:一种思想,一种模式,一种套路,一种解决问题的高效策略



有说的不正确或者不准确的地方欢迎留言指正


有什么有趣的写作技巧或者想法欢迎大家给我留言,大家的帮助是我写下去最有效的动力



Unity【话大】设计模式之策略模式

策略模式

在生活中我们会遇到这一类情况

  • 去某个地方,打开导航软件,会有多条路线供人选择
  • 商场促销活动的时候多种的打折模式
  • 人民币对外汇的各种转换
  • 旅游去某地是坐船?火车?或者飞机?

如果你遇到这一类问题,就可以考略使用策略模式了。策略模式:定义一系列算法,将每一个算法封装起来,并让它们可以相互替换。 策略模式让算法独立于使用它的客户而变化,也称为政策模式(Policy)。
在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。

示例代码如下

public abstract class BaseStrategy
{
   public abstract string GetRoute(string from, string to);
}

public class StrategyWalk : BaseStrategy
{
    public override string GetRoute(string from, string to)
    {
        return "步行到达";
    }
}

public class StrategyShip : BaseStrategy
{
    public override string GetRoute(string from, string to)
    {
        return "轮船到达";
    }
}

public class StrategyTrain : BaseStrategy
{
    public override string GetRoute(string from, string to)
    {
        return "火车到达";
    }
}

public class StrategyAirplane : BaseStrategy
{
    public override string GetRoute(string from, string to)
    {
        return "飞机到达";
    }
}
public class Context 
{
    private BaseStrategy baseStrategy;

    public Context(BaseStrategy baseStrategy)
    {
        this.baseStrategy = baseStrategy;
    }
    public string GetResult(string side1,string side2)
    {
        return baseStrategy.GetRoute(side1, side2);
    }
}

执行

    void Start()
    {
        BaseStrategy strategy = new StrategyWalk();
        Context context = new Context(strategy);
        string route = context.GetResult("地点一", "地点二");
        Debug.Log(route);
    }

但是根据迪米特法则我们知道,对应类与类之间的关系,尽量越少越好,如果我们的策略越多,那么上层业务逻辑需要知道的策略类也会随之增加,所以我们需要再次转移封装。

public class Context 
{
    private BaseStrategy baseStrategy;

    public Context(string strategy)
    {

        switch (strategy)
        {
            case "步行":
                this.baseStrategy = new StrategyWalk();
                break;
            case "轮船":
                this.baseStrategy = new StrategyShip();
                break;
            case "火车":
                this.baseStrategy = new StrategyTrain();
                break;
            case "飞机":
                this.baseStrategy = new StrategyAirplane();
                break;
            default:
                break;

        }
    }
    public string GetResult(string side1,string side2)
    {
        return baseStrategy.GetRoute(side1, side2);
    }
}

执行

    void Start()
    {
        Context context = new Context("飞机");
        string route = context.GetResult("地点一", "地点二");
        Debug.Log(route);
    }

这样上层只需要知道Context一个类就可以了,其他的都转移到下层执行,所以说在以后的开发中,如果遇到这种行为模式或计算策略不同的选择,使用策略模式是一个不错的选择~

策略模式(strategy):她定义了算法家族,分别封装起来,让他们直接可以相互替换,此模式让算法的变化,不会影响到使用算法的客户

上一篇:Kun


下一篇:设计模式---状态模式(DesignPattern_State)