微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TypeScript实现设计模式——策略模式

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

——《大话设计模式》

策略模式主要用来解决当有多种相似算法的时,使用if...else产生的难以维护的问题。它主要由三部分组成:Strategy接口、具体的Strategy类以及用来改变和执行策略的Context类。

接下来将以一个超市选择优惠活动的例子实现策略模式。

Strategy接口

interface Strategy {
  /**
   * 优惠活动
   * @param money 原价
   * @returns 现价
   */
  discount(money: number): number;
}

具体的优惠策略

// 满减优惠
class FullAndReduceStrategy implements Strategy {
  // 满足条件的金额
  private conditionMoney: number;
  // 减少的金额
  private reduceMoney: number;

  constructor(money: number,returnMoney: number) {
    this.conditionMoney = money;
    this.reduceMoney = returnMoney;
  }

  public discount(money: number): number {
    let result: number;

    if (money >= this.conditionMoney) {
      result =
        money - Math.floor(money / this.conditionMoney) * this.reduceMoney;
    }

    return result;
  }
}

// 现金折扣
class CashRebateStrategy implements Strategy {
  // 折扣值
  private moneyRabte: number;

  constructor(moneyRabte: number) {
    this.moneyRabte = moneyRabte;
  }

  discount(money: number): number {
    return money * this.moneyRabte;
  }
}

Context类

setStrategy方法用来控制要使用的策略,execute方法用来执行策略。

class Context {
  private strategy: Strategy;
  private money: number;

  constructor(money: number) {
    this.money = money;
  }

  // 设置优惠策略
  public setStrategy(strategy: Strategy): void {
    this.strategy = strategy;
  }

  // 执行策略
  public execute(): number {
    return this.strategy.discount(this.money);
  }
}

测试

const context: Context = new Context(100);

context.setStrategy(new FullAndReduceStrategy(50,2));
console.log(context.execute()); // 96

context.setStrategy(new CashRebateStrategy(0.5));
console.log(context.execute()); // 50

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐