没有合适的资源?快使用搜索试试~ 我知道了~
职责链模式 // 定义一个公共类 let Leader = function () { this.nextLeader = null; }; Leader.prototype.setNext = function (next) { this.nextLeader = next; return next; // 这样可以链式子调用 } // 定义职责链的节点 let GroupLeader = new Leader(); GroupLeader.handle = function (duration)
资源推荐
资源详情
资源评论
学习设计模式学习设计模式——职责链模式职责链模式
职责链模式职责链模式
// 定义一个公共类
let Leader = function () {
this.nextLeader = null;
};
Leader.prototype.setNext = function (next) {
this.nextLeader = next;
return next; // 这样可以链式子调用
}
// 定义职责链的节点
let GroupLeader = new Leader();
GroupLeader.handle = function (duration) {
if (duration <= 0.5) {
console.log('<=0.5天 小组领导可以批,批准了')
} else {
this.nextLeader.handle(duration)
}
}
let DepartmentLeader = new Leader();
DepartmentLeader.handle = function (duration) {
if (duration <= 1) {
console.log('<=1天 部门领导可以批,批准了')
} else {
this.nextLeader.handle(duration)
}
}
let GeneralLeader = new Leader();
GeneralLeader.handle = function (duration) {
if (duration <= 2) {
console.log('<=2天 总经理可以批,批准了')
} else {
console.log('想离职是吧?')
}
}
// 排列职责链
// GroupLeader.setNext(DepartmentLeader)
// DepartmentLeader.setNext(GeneralLeader)
GroupLeader.setNext(DepartmentLeader).setNext(GeneralLeader)
// 执行场景,请假,组长批不了,递交给上一级部门领导,部门领导批不了递交给上一级总经理
GroupLeader.handle(0.5)
GroupLeader.handle(1)
GroupLeader.handle(2)
GroupLeader.handle(3)
// ES6
// 领导基类
class Leader2 {
constructor() {
this.nextLeader = null;
}
setNext(next) {
this.nextLeader = next
return next;
}
}
class GroupLeader2 extends Leader2 {
handle(duration) {
if (duration <= 0.5) {
console.log('<=0.5天 小组领导可以批,批准了')
} else {
this.nextLeader.handle(duration)
}
资源评论
weixin_38625048
- 粉丝: 3
- 资源: 946
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功