专业编程教程与实战项目分享平台

网站首页 > 技术文章 正文

高级前端必会设计模式之职责链模式

ins518 2024-09-11 09:28:47 技术文章 31 ℃ 0 评论

前端JavaScript设计模式-职责链模式:

职责链模式是一步操作可能分为多个角色来完成,将这些角色分开,然后用一个链再串起来,将发起者和各个处理者进行隔离

例如公司请假审批流业务场景

class Action{
  constructor(name){
    this.name = name
    this.nextAction = null
  }
  setNextAction(action){
    this.nextAction = action
  }
  handle(){
    console.log(`${this.name}审批`)
    if(this.nextAction != null){
      this.nextAction.handle()
    }
  }
}

let a1 = new Action('组长')
let a2 = new Action('经理')
let a3 = new Action('BOSS')

a1.setNextAction(a2)
a2.setNextAction(a3)
a1.handle()

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表