
<!–more–
中介者模式
- 代码演示
// 执行者(前线士兵)
class Receiver {
exec() {
console.log(‘前线士兵 执行命令’)
}
}
// 命令对象(由鼓手传递 开打开打!!!)
class Command {
constructor(receiver) {
this.receiver = receiver
}
cmd() {
console.log(‘命令对象开始传递 “开打开打!!!”‘)
this.receiver.exec()
}
}
// 发布命令者(将军)
class Invoker {
constructor(command) {
this.command = command
}
invoke() {
console.log(‘发布命令: 开打开打!!!’)
this.command.cmd()
}
}
// 士兵
let soldier = new Receiver()
// 鼓手
let drummer = new Command(soldier)
// 将军
let general = new Invoker(drummer)
// 将军发布命令
general.invoke()
`
有事您Q我👇



近期评论