前端JavaScript设计模式-命令模式:
命令模式指执行命令时,发布者与执行者分开,中间加入命令对象进行中转
// 接收者-执行命令
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 archer = new Receiver()
// 旗手传递射箭命令
let standardBearer = new Command(archer)
// 将军发布射箭命令
let general = new Invoker(standardBearer)
general.invoke()
本文暂时没有评论,来添加一个吧(●'◡'●)