js设计模式-命令模式
命令模式是一种组织型模式,主要用在把调用对象(用户界面、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设计模式-命令模式的更多相关文章
- 浅谈js设计模式 — 命令模式
命令模式最常见的应用场景是:有时候需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是什么.此时希望用一种松耦合的方式来设计程序,使得请求发送者和请求接收者能够消除彼此之间的耦 ...
- linkin大话设计模式--命令模式
linkin大话设计模式--命令模式 首先考虑一种应用情况,某个方法需要完成某一个功能,这个功能的大部分功能已经确定了,但是有可能少量的步骤没法确定,必须等到执行这个方法才可以确定. 也就是说,我们写 ...
- 【设计模式】Java设计模式 - 命令模式
Java设计模式 - 命令模式 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 目录 Ja ...
- [Head First设计模式]餐馆中的设计模式——命令模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
- JAVA 设计模式 命令模式
用途 命令模式 (Command) 将一个请求封装为一个对象,从而使你可以用不同的请求对客户进行参数化:对请求排队或请求日志,以及支持可撤销的操作. 命令模式是一种行为型模式. 结构
- 深入浅出设计模式——命令模式(Command Pattern)
模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使得请 ...
- Java设计模式-命令模式(Command)
命令模式很好理解,举个例子,司令员下令让士兵去干件事情,从整个事情的角度来考虑,司令员的作用是,发出口令,口令经过传递,传到了士兵耳朵里,士兵去执行.这个过程好在,三者相互解耦,任何一方都不用去依赖其 ...
- 设计模式--命令模式(Command)
基本概念: Command模式也叫命令模式 ,是行为设计模式的一种.Command模式通过被称为Command的类封装了对目标对象的调用行为以及调用参数,命令模式将方法调用给封装起来了. 命令模式的 ...
- javascript设计模式——命令模式
前面的话 假设有一个快餐店,而我是该餐厅的点餐服务员,那么我一天的工作应该是这样的:当某位客人点餐或者打来订餐电话后,我会把他的需求都写在清单上,然后交给厨房,客人不用关心是哪些厨师帮他炒菜.餐厅还可 ...
随机推荐
- 《深入理解Android虚拟机内存管理》示例程序编译阶段生成的各种语法树完整版
1.tokens "int" "int" <SPACES> " &quo ...
- Hadoop2.6.5高可用集群搭建
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3, m4, m5这五部机, 每部主机的用户名 ...
- 【Oracle】体系结构
1. 理解实例和数据库 ☞ 实例是一组后台进程和共享内存 ☞ 数据库是磁盘上存储的数据集合 ☞ 实例“一生”只能装载并打开一个数据库 ☞ 数据库可以由一个或多个实例(RAC)装载和打开 [oracle ...
- linux 清空文件的几种方案
之前要清理文件,都是简单粗暴的rm -rf log文件,最近,发现在某些环境下,是不能删除文件本省的,又必须要清理文件的内容信息,经过亲自实验,目测以下的几种方案是可行的,方案如下: 1.采用vi命令 ...
- POJ 1000
#include <iostream> int main() { using std::cin; using std::cout; using std::endl; int a,b; ci ...
- 使用replace pioneer批量修改文件名
shell的正则表达式还是很难记忆的,也没有沉静的心情看文档,于是使用了replace pioneer. 1. 启动replace pioneer,Tools->batch runner , ...
- redis与其可视化工具在win7上的安装
步骤 1.下载安装Redis服务. 2.调用执行文件创建服务器以及测试缓存. 3.使用可视化工具redis-desktop-manager管理查询缓存. 1.下载安装Redis服务. 下载地址:htt ...
- 单链表每k个节点为一组进行反转(最后不满k个时不反转)
public class LinkReverse2 { public static Node mhead=null; public static Node mtail=null; public sta ...
- res对象json,redirect
1.res.json() var express=require('express'); var app=express(); app.get('/',function(req,res){ //返回j ...
- py西游公关之模块
Py西游攻关之模块 模块&包(* * * * *) 模块(modue)的概念: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可 ...