JavaScript命令模式实战:构建松耦合的菜单操作
作者:闭包旅人时间:2026-10-30 15:07:38浏览:455
本文通过一个具体的菜单程序示例,深入解析JavaScript中的命令模式。重点阐述如何利用该模式在请求发送者与接收者之间建立松耦合关系,即使不知道具体接收者是谁也能执行操作。文章包含HTML与JS代码实现,并分享了通过调试错误深入理解命令模式执行机制的学习心得。
命令模式的应用场景
命令模式主要适用于以下场景:
当需要向某些对象发送请求,但无法确定请求的具体接收者是谁,也不清楚实际执行的操作是什么时,可以采用一种松耦合的设计方式。这种设计旨在让请求的发送方与接收方相互独立,消除彼此之间的直接依赖关系。
以下是相关的HTML结构代码:
HTML界面实现
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>js:命令模式</title>
<script type="text/javascript" src="command.js"></script>
</head>
<style type="text/css">
button{
margin: 5px;
border: 0;
width: 70px;
height: 35px;
background: #6B78BF;
color: white;
font-size: 14px;
font-family: "微软雅黑";
cursor: pointer;
}
#textarea{
margin: 5px;
width: 400px;
height: 200px;
resize: none;
color: #666;
font-size: 14px;
border: 2px solid #6B78BF;
}
</style>
<body>
<button id="button1">刷新</button>
<button id="button2">新增</button>
<button id="button3">删除</button>
<br/>
<textarea id="textarea">
这是预设的内容
</textarea>
</body>
</html>
接下来是核心的JavaScript实现代码:
JavaScript命令逻辑
// 在页面中使用例:setCommand( button1, refreshMenuBarCommand );来发送命令
// setCommand 函数负责往按钮上面安装命令,预留好安装命令的接口
var setCommand = function( button , command ){
button.onclick = function(){
command.execute();
}
}
// 编写点击按钮之后的具体行为:刷新菜单界面、增加子菜单和删除子菜单
var MenuBar = {
refresh: function(){
var cur_date = new Date();
document.getElementById("textarea").value+=cur_date.toLocaleString()+" 刷新菜单目录\r";
}
}
var SubMenu = {
add: function(){
var cur_date = new Date();
document.getElementById("textarea").value+=cur_date.toLocaleString()+" 新增菜单目录\r";
},
del: function(){
var cur_date = new Date();
document.getElementById("textarea").value+=cur_date.toLocaleString()+" 删除子菜单\r";
}
}
//封装行为在命令类中
var RefreshMenuBarCommand = function( receiver ){
this.receiver = receiver;
}
RefreshMenuBarCommand.prototype.execute = function(){
this.receiver.refresh();
}
var AddSubMenuCommand = function( receiver ){
this.receiver = receiver;
}
AddSubMenuCommand.prototype.execute = function(){
this.receiver.add();
}
var DelSubMenuCommand = function( receiver ){
this.receiver =receiver
}
DelSubMenuCommand.prototype.execute = function(){
this.receiver.del();
}
//命令接收者传入到 command 对象
var refreshMenuBarCommand = new RefreshMenuBarCommand( MenuBar );
var addSubMenuCommand = new AddSubMenuCommand( SubMenu );
var delSubMenuCommand = new DelSubMenuCommand( SubMenu );
window.onload = function(){
//把 command 对象安装到 button 上面
var button1 = document.getElementById("button1");
var button2 = document.getElementById("button2");
var button3 = document.getElementById("button3");
setCommand( button1, refreshMenuBarCommand );
setCommand( button2, addSubMenuCommand );
setCommand( button3, delSubMenuCommand );
}
总结:
实践心得与总结
在抄写书籍代码进行练习的过程中,难免会出现各种错误。其中最严重的一次是将“receiver”一词拼写错误,导致调试过程持续了数天,期间甚至暂停了对该练习的关注。然而,正是这个出错的过程促使我重新审视代码细节,逐行进行理解与调试。尽管目前对命令模式的理解仍不够透彻,但通过这部分内容的实践以及对MySQL的学习,心中已隐约建立起对命令模式的认知框架。
参考:
《JavaScript设计模式与开发实践》第9章9.2节
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。
作者最新文章
什么时候该用JS构造器:与工厂模式、字面量的选择对比
2026-11-05 16:18:22
推荐几款轻量级JS照片墙开源库及对比
2026-11-05 15:56:19
JavaScript递归写法有哪些常见陷阱及避免方法
2026-11-05 14:53:38
JS数组浅拷贝与深拷贝怎么选?场景与方法总结
2026-11-05 14:41:02
p5.js 中 setup() 和 draw() 函数的执行机制详解
2026-11-05 12:32:38
