策略模式

在现实生活中常常遇到实现某种目标存在多种策略可供选择的情况,例如,出行旅游可以乘坐飞机、乘坐火车、骑自行车或自己开私家车等,超市促销可以釆用打折、送商品、送积分等方法。
在软件开发中也常常遇到类似的情况,当实现某一个功能存在多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成该功能,如数据排序策略有冒泡排序、选择排序、插入排序、二叉树排序等。
如果使用多重条件转移语句实现(即硬编码),不但使条件语句变得很复杂,而且增加、删除或更换算法要修改原代码,不易维护,违背开闭原则。如果采用策略模式就能很好解决该问题。


策略模式的定义与特点

策略(Strategy)模式的定义:该模式定义了一系列算法,并将每个算法封装起来,使它们可以相互替换,且算法的变化不会影响使用算法的客户。策略模式属于对象行为模式,它通过对算法进行封装,把使用算法的责任和算法的实现分割开来,并委派给不同的对象对这些算法进行管理。
策略模式的主要优点如下。

  1. 多重条件语句不易维护,而使用策略模式可以避免使用多重条件语句,如 if...else 语句、switch...case 语句。
  2. 策略模式提供了一系列的可供重用的算法族,恰当使用继承可以把算法族的公共代码转移到父类里面,从而避免重复的代码。
  3. 策略模式可以提供相同行为的不同实现,客户可以根据不同时间或空间要求选择不同的。
  4. 策略模式提供了对开闭原则的完美支持,可以在不修改原代码的情况下,灵活增加新算法。
  5. 策略模式把算法的使用放到环境类中,而算法的实现移到具体策略类中,实现了二者的分离。

其主要缺点如下。

  1. 客户端必须理解所有策略算法的区别,以便适时选择恰当的算法类。
  2. 策略模式造成很多的策略类,增加维护难度。


使用策略模式计算等级

在游戏中,我们每玩完一局游戏都有对用户进行等级评价,比如S级4倍经验,A级3倍经验,B级2倍经验,其他1倍经验,用函数来表达如下:

    function getExperience(level, experience){
      if(level == 'S'){
        return 4*experience
      }
      if(level == 'A'){
        return 3*experience
      }
      if(level == 'B'){
        return 2*experience
      }
      return experience
    }

可知getExperience函数各种if条件判断,复用性差,我们根据策略模式封装复用的思想,进行改写。

    // 改为策略模式 分成两个函数来写
    const strategy = {
      'S' : function(experience){
        return 4*experience
      },
      'A' : function(experience){
        return 3*experience
      },
      'B' : function(experience){
        return 2*experience
      }
    }
    // getExperience可以复用
    function getExperience(strategy, level, experience){
      return (level in strategy) ? strategy[level](experience) : experience
    }
    var s = getExperience(strategy, 'S', 100)
    var a = getExperience(strategy, 'A', 100)
    console.log(s, a) // 400 300

分为两个函数之后,strategy对象解耦,拓展性强。在vue数据驱动视图更新的更新器updater使用,就使用了策略模式。想要进一步了解vue底层原理,可以参考可以参考github上的一篇文章 ☛ MVVM实现

// 指令处理集合
var compileUtil = {
    // v-text更新视图原理
    text: function(node, vm, exp) {
        this.bind(node, vm, exp, 'text');
    },
    // v-html更新视图原理
    html: function(node, vm, exp) {
        this.bind(node, vm, exp, 'html');
    },
    // v-class绑定原理
    class: function(node, vm, exp) {
        this.bind(node, vm, exp, 'class');
    },
    bind: function(node, vm, exp, dir) {
        // 不同指令触发视图更新
        var updaterFn = updater[dir + 'Updater'];
        updaterFn && updaterFn(node, this._getVMVal(vm, exp));
        new Watcher(vm, exp, function(value, oldValue) {
            updaterFn && updaterFn(node, value, oldValue);
        });
    }
    ......
}


使用策略模式验证表单

常见表单验证用if、else流程语句判断用户输入数据是否符合验证规则,而在Elementui中,基于async-validator库,只需要通过rule属性传入约定的验证规则,即可校验。方便快捷,可复用。现在我们根据策略模式仿写一个校验方式。

    // 我们写一个form表单
    <form action="/" class="form">
      <input type="text" name="username">
      <input type="password" name="password"> 
      <button>submit</button>
    </form>
    <div id="tip"></div>
  • 首先定义校验规则
    const strategies = {
      // 非空
      noEmpty: function(value, errMsg){
        if(value === ''){
          return errMsg
        }
      },
      // 最小长度
      minLength: function(value, length, errMsg){
        if(!value || value.length < length){
          return errMsg
        }
      },
      // 最大长度
      maxLength: function(value, length, errMsg){
        if(value.length > length){
          return errMsg
        }
      }
    }
  • 接着设置验证器
    // 创建验证器
    var Validator = function(strategies){
      this.strategies = strategies
      this.cache = [] // 存储校验规则
    }
    // 添加校验规则
    Validator.prototype.add = function(dom, rules){
      rules.forEach(item => {
        this.cache.push(() => {
          let value = dom.value
          let arr = item.rule.split(':')
          let name = arr.shift()
          let params = [value, ...arr, item.errMsg]
          // apply保证上下文一致
          return this.strategies[name].apply(dom, params)
        })
      })
    }
    // 校验结果
    Validator.prototype.validate = function(dom, rules, errMsg){
      // 遍历cache里面的校验函数
      for(let i = 0, validateFun; validateFun = this.cache[i++];){
        const message = validateFun()
        // 返回报错信息,终止验证并抛出异常
        if(message) return message
      }
    }
  • 最后进行校验
    var form = document.querySelector("form")
    // 提交表单
    form.onsubmit = function(event){
      event.preventDefault() 
      // 判断验证结果
      const message = validate()
      const tip = document.getElementById('tip')
      if(message){
        tip.innerHTML = message
        tip.style.color = 'red'
      }else{
        tip.innerHTML = '验证通过!'
        tip.style.color = 'green'
      }
    }
    // 校验函数
    function validate(){
      // 实例验证器
      const validator = new Validator(strategies)
      // 添加验证规则
      validator.add(form.username, [
        {
          rule: 'noEmpty',
          errMsg: '用户名不能为空!'
        },
        {
          rule: 'minLength:3',
          errMsg: '用户名长度大于3!'
        }
      ])
      validator.add(form.password, [
        {
          rule: 'minLength:6',
          errMsg: '密码长度大于6!'
        },
        {
          rule: 'maxLength:10',
          errMsg: '密码最大长度为10!'
        }
      ])
      // 进行校验,并返回结果
      return validator.validate()
    }

如上所示,我们只要添加strategies对象的属性,就能自定义自己的验证规则,并且可以复用,大大方便了日常开发!


链接: https://www.fly63.com/course/27_1272