命令模式
命令模式「Command Pattern」:是一种数据驱动的设计模式,它属于行为型模式。 请求以命令的形式包裹在对象中,并传给调用对象。 调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。
模式动机
实现
const TV = {
//
}
// 将命令抽象出来
const Command = {
turnOff: () => {
console.info('关闭电视')
},
turnOn: () => {
console.info('打开电视')
},
volumeIncrease: () => {
console.info('音量增加')
}
}
const bindCommand = (obj, command) => {
command.call(obj)
}
const CemoteControl = {
key1: bindCommand(Command.turnOff),
key2: bindCommand(Command.trunOn),
key3: bindCommand(Command.volumeIncrease)
}小结
Last updated
Was this helpful?