職責鏈模式
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).setNext(GeneralLeader)
GroupLeader.handle(0.5)
GroupLeader.handle(1)
GroupLeader.handle(2)
GroupLeader.handle(3)
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)
}
}
}
class DepartmentLeader2 extends Leader2 {
handle(duration) {
if (duration <= 1) {
console.log('<=1天 部門領導可以批,批准了')
} else {
this.nextLeader.handle(duration)
}
}
}
class GeneralLeader2 extends Leader2 {
handle(duration) {
if (duration <= 2) {
console.log('<=2天 總經理可以批,批准了')
} else {
console.log('想離職是吧?')
}
}
}
const ZhangSan = new GroupLeader2()
const LiSi = new DepartmentLeader2()
const WangWu = new GeneralLeader2()
ZhangSan.setNext(LiSi).setNext(WangWu)
ZhangSan.handle(0.5)
ZhangSan.handle(1)
ZhangSan.handle(2)
ZhangSan.handle(3)