使用发布订阅模式处理多维度复杂融合场景

文章介绍了如何使用策略模式将发布/订阅模式封装在类`Strategy`中,利用Map保持条件的顺序,并通过实例展示了如何添加和执行不同条件的回调。
摘要由CSDN通过智能技术生成

场景再现:
请添加图片描述
模式正确封装方法:

class Strategy {
  map = new Map();

  constructor({ defaultCbs, errorCbs }) {
    // 默认
    this.map.set("default", defaultCbs ?? []);
    // 错误
    this.map.set("error", errorCbs ?? []);
  }

  // 获取条件key
  getCondition(condition) {
    const conditionMap = new Map();
    Object.keys(condition)
      .sort()
      .forEach((key) => {
        conditionMap.set(key, condition[key]);
      });
    return JSON.stringify(Object.fromEntries(conditionMap));
  }

  // 增加条件情况
  add(condition, conditionCbs) {
    const currentCondition = this.getCondition(condition);
    let cbs = this.map.get(currentCondition);
    if (!cbs) {
      this.map.set(currentCondition, []);
      cbs = this.map.get(currentCondition);
    }
    cbs.push(...conditionCbs);
  }

  // 执行条件情况
  do(condition) {
    const currentCondition = this.getCondition(condition);
    try {
      const cbs = this.map.get(currentCondition);
      if (cbs) {
        cbs.forEach((cb) => cb(JSON.parse(currentCondition)));
      } else {
        // 匹配不到则执行默认函数
        const defaultCbs = this.map.get("default");
        defaultCbs.forEach((cb) => cb(JSON.parse(currentCondition)));
      }
    } catch (e) {
      // 报错执行报错函数
      const errorCbs = this.map.get("error");
      errorCbs.forEach((cb) => cb(e));
    }
  }
}

const strategy = new Strategy({
  defaultCbs: [
    (v) => {
      console.log("这是默认情况", v);
    },
  ],
  errorCbs: [
    (e) => {
      console.log("这是错误情况", e);
    },
  ],
});
const condition = {
  name: "sunshine_lin",
  weight: 160,
};

// 此时还没有注册条件事件,所以输出默认事件
strategy.do(condition); 

// 添加条件函数
strategy.add(condition, [
  (v) => {
    console.log("事件1", v);
  },
  (v) => {
    console.log("事件2", v);
  },
]);

// 此时有条件事件了,输入:事件1 事件2
strategy.do(condition);

const condition2 = {
  name: "error_lin",
  weight: 1000000,
};

// 可以增加报错条件
strategy.add(condition2, [
  (v) => {
    throw new Error("我超重啦!!!!");
  },
]);

// 报错,输出:我超重啦!!!!
strategy.do(condition2)
	要点注释:
	1. 使用Map 这个数据结构而非对象,是因为Map 的 key 是有顺序
	2. 策略模式=》发布订阅模式的转变

·······················本文原链接 前端之神-林三心 微信公众号文章,如有侵权,可以随时联系删除······················

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值