Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript 设计模式(命令模式) #116

Open
wl05 opened this issue Apr 22, 2020 · 0 comments
Open

javascript 设计模式(命令模式) #116

wl05 opened this issue Apr 22, 2020 · 0 comments

Comments

@wl05
Copy link
Owner

wl05 commented Apr 22, 2020

概念

命令是指一个执行某些特定任务的指令

实现

var MenuBar = {
    refresh: function () {
        console.log('刷新菜单界面')
    }
}
var RefreshMenuBarCommand = function (receiver) {
    return {
        execute: function () {
            receiver.refresh()
        }
    }
}
var setCommand = function (button, command) {
    button.onclick = function () {
        command.execute()
    }
}

refreshMenuBarCommand = RefreshMenuBarCommand(MenuBar)
setCommand(button1, refreshMenuBarCommand)


var refreshMenuBarCommand = RefreshMenuBarCommand(MenuBar)
setCommand(button1, refreshMenuBarCommand)

参考资料

  1. JavaScript设计模式与开发实践-第五章-策略模式
  2. JS设计模式——命令模式
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant