命令模式是一种组织型模式,主要用在把调用对象(用户界面、API和代理等)与实现操作的对象隔离开。也就是说 ,凡是两个对象间的互动方式需要更高的模块化程度时都可以用到这种模式。

命令模式的好处:1、提高程序的模块化程度和灵活性。(运用得当);2、实现取消和状态恢复等复杂的有用特性非常容易。

例子:菜单组合对象

 /**
* 菜单组合对象
*/ /*command,Composite and MenuObject interfaces*/
var Command = new Interface("Compand",["execute"]);
var Composite = new Interface("Composite",["add","remove","getChild","getElement"]);
var MenuObject = new Interface("MenuObject",["show","hide"]); /*MenuBar class, a composite*/
var MenuBar = function(){
this.menus = {};
this.element = document.createElement("ul");
this.element.className = "menu-bar";
this.element.style.display = "none";
}
MenuBar.prototype = {
add:function(menuObject){
Interface.ensureImplements(menuObject,Composite,MenuObject);
this.menus[menuObject.name] = menuObject;
this.element.appendChild(this.menus[menuObject.name].getElement());
},
remove:function(name){
delete this.menus[name];
},
getChild:function(name){
return this.menus[name];
},
getElement:function(){
return this.element;
},
show:function(){
this.element.style.display = "block";
for(name in this.menus){
this.menus[name].show();
}
},
hide:function(){
this.element.style.display = "none";
}
} /*Menu class a composite*/
//备注:这里为了获取Menu对象的父对象,所以多传了一个parent,同时在实现的接口下,又多加了两个方法:showContainer和hideContainer
var Menu = function(name,parent){
this.name = name;
this.items = {};
this.parent = parent;
this.element = document.createElement("li");
this.element.className = "menu";
this.element.innerHTML = this.name;
this.element.style.display = "none";
this.container = document.createElement("ul");
this.element.appendChild(this.container); var that = this;
this.element.addEventListener("click",function(e){
e.preventDefault();
e.stopPropagation();
//这里在显示当前菜单选项的时候,把其他的选项全部隐藏掉
var menus = that.parent.menus;
if(menus){
for(var name in menus){
menus[name].hideContainer();
}
}
that.showContainer();
},false);
document.body.addEventListener("click",function(e){
e.preventDefault();
e.stopPropagation();
that.hideContainer();
},false);
}; Menu.prototype = {
add:function(menuItemObject){
Interface.ensureImplements(menuItemObject,Composite,MenuObject);
this.items[menuItemObject.name] = menuItemObject;
this.container.appendChild(this.items[menuItemObject.name].getElement());
},
remove:function(name){
delete this.items[name];
},
getChild:function(name){
return this.items[name];
},
getElement:function(){
return this.element;
},
show:function(){
this.element.style.display = "block";
for(name in this.items){
this.items[name].show();
}
},
hideContainer:function(){
this.container.style.display = "none";
},
showContainer:function(){
this.container.style.display = "block";
},
hide:function(){
this.element.style.display = "none";
}
} /*MenuItem class, a leaf*/
var MenuItem = function(name,command){
Interface.ensureImplements(command,Command);
this.name = name;
this.element = document.createElement("li");
this.element.className = "menu-item";
this.element.style.display = "none";
this.anchor = document.createElement("a");
this.anchor.href = "#";
this.anchor.innerHTML = this.name;
this.element.appendChild(this.anchor); this.anchor.addEventListener("click",function(e){
e.preventDefault();
command.execute();
});
}; MenuItem.prototype = {
add:function(){},
remove:function(){},
getChild:function(){},
getElement:function(){return this.element;},
show:function(){
this.element.style.display = "block";
},
hide:function(){
this.element.style.display = "none";
}
}

界面:

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>菜单对象</title>
<style type="text/css">
#container{margin:0 auto;padding:0; border:1px solid #ccc; width:500px; height:50px; font-family: "微软雅黑"; font-size: 14px; text-align: center;}
.menu-bar{list-style: none;padding:0;width:100%;height:50px; margin-left:50px;}
.menu-bar .menu{float:left; width:80px; text-align: center; cursor: pointer;height:30px; line-height:30px;}
.menu-bar .menu:hover{background:#0000DD; color:#fff;}
.menu-bar .menu ul{list-style:none;width:80px; margin:0;padding:0; background:#ddd; display: none; margin-top:5px; border: 1px solid black;}
/*.menu-bar .menu:nth-of-type(1) ul{display: block;}*/
.menu-bar .menu .menu-item{width:70px;height:30px; line-height: 30px; border-bottom: 1px dashed #ccc;}
.menu-bar .menu .menu-item a{text-decoration: none;} #result{
margin:150px auto; height:50px; line-height: 50px; border: 2px solid red; width:300px; text-align: center; font-family: "微软雅黑"; font-size: 16px;
}
</style>
</head>
<body>
<div id="container"> </div> <div id="result"></div>
</body>
</html>
<script type="text/javascript" src="Interface.js"></script>
<script type="text/javascript" src="Menu.js"></script>
<script type="text/javascript">
var oResult = document.getElementById("result");
//定义每个菜单的action
function FileActions(){}
FileActions.prototype = {
open:function(){
oResult.innerHTML = "open command is executed";
},
close:function(){
oResult.innerHTML = "close command is executed";
},
save:function(){
oResult.innerHTML = "save command is executed";
},
saveAs:function(){
oResult.innerHTML = "saveAs command is executed";
}
} function EditActions(){}
EditActions.prototype = {
cut:function(){
oResult.innerHTML = "cut command is executed";
},
copy:function(){
oResult.innerHTML = "copy command is executed";
},
paste:function(){
oResult.innerHTML = "paste command is executed";
},
delete:function(){
oResult.innerHTML = "delete command is executed";
}
} function InsertActions(){}
InsertActions.prototype.insert = function(){
oResult.innerHTML = "insert command is executed";
} function HelpActions(){}
HelpActions.prototype.help = function(){
oResult.innerHTML = "help command is executed";
} /*MenuCommand class. a command object*/
var MenuCommand = function(action){
this.action = action;
}
MenuCommand.prototype.execute = function(){
this.action();
} var fileActions = new FileActions();
var editActions = new EditActions();
var insertActions = new InsertActions();
var helpActions = new HelpActions(); var appMenuBar = new MenuBar(); /*fileMenu*/
var fileMenu = new Menu("File",appMenuBar); fileMenu.add(new MenuItem("Open",new MenuCommand(fileActions.open)));
fileMenu.add(new MenuItem("Close",new MenuCommand(fileActions.close)));
fileMenu.add(new MenuItem("Save",new MenuCommand(fileActions.save)));
fileMenu.add(new MenuItem("Save As",new MenuCommand(fileActions.saveAs))); appMenuBar.add(fileMenu); /*editMenu*/
var editMenu = new Menu("Edit",appMenuBar); editMenu.add(new MenuItem("Cut",new MenuCommand(editActions.cut)));
editMenu.add(new MenuItem("Copy",new MenuCommand(editActions.copy)));
editMenu.add(new MenuItem("Paste",new MenuCommand(editActions.paste)));
editMenu.add(new MenuItem("Delete",new MenuCommand(editActions.delete))); appMenuBar.add(editMenu); /*the insert menu*/ var insertMenu = new Menu("Insert",appMenuBar);
insertMenu.add(new MenuItem("The Block",new MenuCommand(insertActions.insert)));
appMenuBar.add(insertMenu); /*The help menu*/
var helpMenu = new Menu("Help",appMenuBar);
helpMenu.add(new MenuItem("TheHelp",new MenuCommand(helpActions.help))); appMenuBar.add(helpMenu); /*Build the menu bar*/
document.getElementById("container").appendChild(appMenuBar.getElement());
appMenuBar.show(); </script>

js设计模式-命令模式的更多相关文章

  1. 浅谈js设计模式 — 命令模式

    命令模式最常见的应用场景是:有时候需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是什么.此时希望用一种松耦合的方式来设计程序,使得请求发送者和请求接收者能够消除彼此之间的耦 ...

  2. linkin大话设计模式--命令模式

    linkin大话设计模式--命令模式 首先考虑一种应用情况,某个方法需要完成某一个功能,这个功能的大部分功能已经确定了,但是有可能少量的步骤没法确定,必须等到执行这个方法才可以确定. 也就是说,我们写 ...

  3. 【设计模式】Java设计模式 - 命令模式

    Java设计模式 - 命令模式 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 目录 Ja ...

  4. [Head First设计模式]餐馆中的设计模式——命令模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  5. JAVA 设计模式 命令模式

    用途 命令模式 (Command) 将一个请求封装为一个对象,从而使你可以用不同的请求对客户进行参数化:对请求排队或请求日志,以及支持可撤销的操作. 命令模式是一种行为型模式. 结构

  6. 深入浅出设计模式——命令模式(Command Pattern)

    模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使得请 ...

  7. Java设计模式-命令模式(Command)

    命令模式很好理解,举个例子,司令员下令让士兵去干件事情,从整个事情的角度来考虑,司令员的作用是,发出口令,口令经过传递,传到了士兵耳朵里,士兵去执行.这个过程好在,三者相互解耦,任何一方都不用去依赖其 ...

  8. 设计模式--命令模式(Command)

    基本概念:  Command模式也叫命令模式 ,是行为设计模式的一种.Command模式通过被称为Command的类封装了对目标对象的调用行为以及调用参数,命令模式将方法调用给封装起来了. 命令模式的 ...

  9. javascript设计模式——命令模式

    前面的话 假设有一个快餐店,而我是该餐厅的点餐服务员,那么我一天的工作应该是这样的:当某位客人点餐或者打来订餐电话后,我会把他的需求都写在清单上,然后交给厨房,客人不用关心是哪些厨师帮他炒菜.餐厅还可 ...

随机推荐

  1. OpenCL C

    OpenCL C OpenCL  简介 opencl C是ISO C99的一个扩展,主要区别如下: 去除了C99的一些特性,如:标准C99头文件,函数指针,递归,变长数组,和位域 增加了一些特性用于并 ...

  2. T-SQL查询高级--理解SQL SERVER中非聚集索引的覆盖,连接,交叉和过滤

      写在前面:这是第一篇T-SQL查询高级系列文章.但是T-SQL查询进阶系列还远远没有写完.这个主题放到高级我想是因为这个主题需要一些进阶的知识作为基础..如果文章中有错误的地方请不吝指正.本篇文章 ...

  3. 复习java的例子(第一天)

    1. 编写程序:从键盘上读入一个学生成绩, 存放在变量score中,根据score的值输出其对应的成绩等级: score>=90 等级: A 70=<score<90 等级: B 6 ...

  4. 单链表每k个节点为一组进行反转(最后不满k个时不反转)

    public class LinkReverse2 { public static Node mhead=null; public static Node mtail=null; public sta ...

  5. TensorFlow技术解析与实战学习笔记(15)-----MNIST识别(LSTM)

    一.任务:采用基本的LSTM识别MNIST图片,将其分类成10个数字. 为了使用RNN来分类图片,将每张图片的行看成一个像素序列,因为MNIST图片的大小是28*28像素,所以我们把每一个图像样本看成 ...

  6. 目录-Linux

    Linux文件系统: Linux: glibc 程序编译方式: 动态链接 静态编译 进程的类型: 终端:硬件设备,关联一个用户接口 与终端相关:通过终端启动 与终端无关:操作引导启动过程当中自动启动 ...

  7. Python-Pandas数据处理

  8. Git 基础教程 之 删除文件

    ① 手动或命令 rm删除工作区的问价:       git checkout -- readme.txt 可恢复       checkout 实际上是用版本库里的替换工作区的版本 ② 删除了工作区文 ...

  9. mysql查询昨天 一周前 一月前 一年前的数据

    mysql 昨天 一周前 一月前 一年前的数据 这里主要用到了DATE_SUB, 参考如下 代码如下: SELECT * FROM yh_contentwhere inputtime>DATE_ ...

  10. 将会改变未来IT世界的十种编程语言

    这里要说的都是革新,说这些的目的就是要保持关注最新技术.如果你是一个程序员,想要探寻未来技术,那这篇文章就是你的必读之选.我们这里列出了10种编程语言,10种将会改变IT世界工作方式的编程语言.这些语 ...