命令模式是一种组织型模式,主要用在把调用对象(用户界面、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. openMSP430之io_test

    openMSP430: IO functionality test with interupt #include "omsp_system.h" volatile char shi ...

  2. 编译Caffe-Win错误集锦

    Caffe在Windows下编译还是遇到不少麻烦的... 1.visual studio 2013 error C2371: 'int8_t' : redefinition; 引入的unistd.h文 ...

  3. 【JSP】上传图片到数据库中

    第一步:建立数据库 create table test_img(id number(4),name varchar(20),img long raw); 第二步:(NewImg.html) <h ...

  4. mysql 是如何保证在高并发的情况下autoincrement关键字修饰的列不会出现重复

    转载自 https://juejin.im/book/5bffcbc9f265da614b11b731/section/5c42cf94e51d45524861122d#heading-8 mysql ...

  5. java HttpURLConnection 登录网站 完整代码

    import java.io.*; import java.util.*; import java.net.*; public class WebTest { public static void m ...

  6. PAT_A1076#Forwards on Weibo

    Source: PAT A1076 Forwards on Weibo (30 分) Description: Weibo is known as the Chinese version of Twi ...

  7. Codeforces Round #550 (Div. 3)E. Median String

    把字符串看作是26进制的数,从后往前翻译,那么就可以把两个串变成对应的26进制的数字,那么只要把两个数加起来除以二就得到中间的串对应的数了,同理再转化回来就行了.但是这样会有一个问题就是串的长度有2e ...

  8. GDI 画笔(9)

    使用现有画笔 Windows 提供三种备用画笔(Stock Pen):BLACK_PEN(黑色画笔).WHITE_PEN(白色画笔).NULL_PEN(不绘制任何图形的画笔). 调用 GetStock ...

  9. Linux循环

    echo 'for循环' do echo $i done

  10. VMware无法安装故障总结

    暂时遇到如下问题,并按此步骤可以解决,后续如有其它相关问题将继续更新: 1.无法获得 VMCI 驱动程序的版本: 句柄无效.如下图: 找到.vmx后缀配置文件 将如下白标""TRU ...