打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
[设计]图说设计模式-策略模式
设计模式之策略模式和状态模式
策略模式Lua实现
用 Python 写了个简单的股票量化交易框架
从华为跳槽来的工程师,繁琐的if else都处理得这么优雅
python---策略模式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服