在软件系统,“行为请求者”与“行为实施者”通常存在一个“紧耦合”。但在某些场合,比方要对行为进行“记录、撤销/重做、事务”等处理,这样的无法抵御变化的紧耦合是不合适的。在这样的情况下。怎样将“行为请求者”与“行为实现者”解耦?将一组行为抽象为对象实现二者之间的松耦合。这就是命令模式(Command
Pattern)————题记

设计模式
命令模式:将“请求”封装成对象,以便使用不同的请求、队列或者日志来參数化其它对象。

命令模式也支持可撤销的操作。


模式分析
1. 命令模式的本质是对命令进行封装,将发出命令的责任和运行命令的责任切割开
2. 每个命令都是一个操作:请求的一方发出请求,要求运行一个操作;接收的一方收到请求,并运行操作
3. 命令模式同意请求的一方和接收的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否被运行、何时被运行,以及是怎么被运行的。
4. 命令模式使请求本身成为一个对象,这个对象和其它对象一样能够被存储和传递。
5. 命令模式的关键在于引入了抽象命令接口,且发送者针对抽象命令接口编程,仅仅有实现了抽象命令接口的详细命令才干与接收者相关联

// Invoker:要求命令对象运行请求,一般会持有命令对象,能够持有非常多的命令对象。这个是client真正触发命令并要求命令运行对应操作的地方,也就是说相当于使用命令对象的入口。
public class RemoteControl {
//这时候。遥控器要处理7个开与关的命令,使用对应数组记录这些命令
Command[] onCommands;
Command[] offCommands; public RemoteControl() {
//在构造器中。仅仅须要实例化并初始化两个开与关的数组
onCommands = new Command[7];
offCommands = new Command[7];
//NoCommand对象是一个空对象(null obeject)的样例。当你不想返回一个有意义的对象时,空对象就非常实用,客户也能够将处理null的责任转义给空对象。 //举例来说。遥控器不可能一出厂就设置了有意义的命令对象,所以提供了NoCommand对象作为替代品。当调用它的execute()
//方法时。这样的对象什么也不做。
Command noCommand = new NoCommand();
for (int i = 0; i < 7; i++) {
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
} //setCommand()方法须有三个參数,各自是插槽的位置、开的命令、关的命令。
public void setCommand(int slot, Command onCommand, Command offCommand) {
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
} //当按下开或关的button。硬件就会负责调用对应的方法
public void onButtonWasPushed(int slot) {
onCommands[slot].execute();
} public void offButtonWasPushed(int slot) {
offCommands[slot].execute();
} //覆盖toString(),打印出每一个插槽和它对应的命令。
public String toString() {
StringBuffer stringBuff = new StringBuffer();
stringBuff.append("\n------ Remote Control -------\n");
for (int i = 0; i < onCommands.length; i++) {
stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
+ " " + offCommands[i].getClass().getName() + "\n");
}
return stringBuff.toString();
}
} //定义命令的接口。声明运行的方法。
public interface Command {
public void execute();
} //命令接口实现对象,是“虚”的实现。一般会持有接收者,并调用接收者的功能来完毕命令要运行的操作。
public class StereoOnWithCDCommand implements Command {
Stereo stereo; public StereoOnWithCDCommand(Stereo stereo) {
this.stereo = stereo;
} public void execute() {
stereo.on();
stereo.setCD();
stereo.setVolume(11);
}
} //接收者,真正运行命令的对象。 不论什么类都可能成为一个接收者。仅仅要它能够实现命令要求实现的对应功能。
public class Stereo {
String location; public Stereo(String location) {
this.location = location;
} public void on() {
System.out.println(location + " stereo is on");
} public void off() {
System.out.println(location + " stereo is off");
} public void setCD() {
System.out.println(location + " stereo is set for CD input");
} public void setDVD() {
System.out.println(location + " stereo is set for DVD input");
} public void setRadio() {
System.out.println(location + " stereo is set for Radio");
} public void setVolume(int volume) {
// code to set the volume
// valid range: 1-11 (after all 11 is better than 10, right?)
System.out.println(location + " Stereo volume set to " + volume);
}
} //測试
public class RemoteLoader { public static void main(String[] args) {
RemoteControl remoteControl = new RemoteControl(); //将所有的装置创建在合适的位置
Light livingRoomLight = new Light("Living Room");
Light kitchenLight = new Light("Kitchen");
CeilingFan ceilingFan= new CeilingFan("Living Room");
GarageDoor garageDoor = new GarageDoor("");
Stereo stereo = new Stereo("Living Room"); LightOnCommand livingRoomLightOn =
new LightOnCommand(livingRoomLight);
LightOffCommand livingRoomLightOff =
new LightOffCommand(livingRoomLight);
LightOnCommand kitchenLightOn =
new LightOnCommand(kitchenLight);
LightOffCommand kitchenLightOff =
new LightOffCommand(kitchenLight); CeilingFanOnCommand ceilingFanOn =
new CeilingFanOnCommand(ceilingFan);
CeilingFanOffCommand ceilingFanOff =
new CeilingFanOffCommand(ceilingFan); GarageDoorUpCommand garageDoorUp =
new GarageDoorUpCommand(garageDoor);
GarageDoorDownCommand garageDoorDown =
new GarageDoorDownCommand(garageDoor); StereoOnWithCDCommand stereoOnWithCD =
new StereoOnWithCDCommand(stereo);
StereoOffCommand stereoOff =
new StereoOffCommand(stereo); //如今已经有了所有的命令。能够将他们载入到遥控器插槽中。
remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);
remoteControl.setCommand(3, stereoOnWithCD, stereoOff); //在这里,使用toString()方法打印出每一个遥控器的插槽和它被指定的命令
System.out.println(remoteControl); //一切就绪,逐步按下每一个插槽的开与关button。 remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(1);
remoteControl.offButtonWasPushed(1);
remoteControl.onButtonWasPushed(2);
remoteControl.offButtonWasPushed(2);
remoteControl.onButtonWasPushed(3);
remoteControl.offButtonWasPushed(3);
}
}


版权声明:本文博主原创文章,博客,未经同意不得转载。

《Head First 设计模式》学习笔记——命令模式的更多相关文章

  1. Java-马士兵设计模式学习笔记-命令模式

    一.概述 命令模式 二.代码 1.Client.java public class Client { public void request(Server server){ server.addCom ...

  2. Java设计模式学习记录-命令模式

    前言 这次要介绍的是命令模式,这也是一种行为型模式.最近反正没有面试机会我就写博客呗,该投的简历都投了.然后就继续看书,其实看书也会给自己带来成就感,原来以前不明白的东西,书上已经给彻底的介绍清楚了, ...

  3. 设计模式学习之命令模式(Command,行为型模式)(12)

    一.命令模式的定义 命令模式属于对象的行为型模式.命令模式是把一个操作或者行为抽象为一个对象中,通过对命令的抽象化来使得发出命令的责任和执行命令的责任分隔开.命令模式的实现可以提供命令的撤销和恢复功能 ...

  4. 学习笔记——命令模式Command

    命令模式,将具体操作Receiver封在Command中,调用类只需要知道Command即可.

  5. 设计模式之笔记--命令模式(Command)

    命令模式(Command) 定义 命令模式(Command),将一个请求封闭为一个对象,从而使你可以用不同的请求对客户进行参数化:对请求排除或记录请求日志,以及支持可撤销的操作. 类图 描述 Comm ...

  6. Java-马士兵设计模式学习笔记-桥接模式

    一.概述 1.桥接模式的应用情况:(1)两个维度扩展(2)排列组合 二.代码 1.Gift.java public class Gift { protected GiftImpl giftImpl; ...

  7. Java-马士兵设计模式学习笔记-工厂模式-抽象工厂模式

    一.概述 1.抽象工厂:当情况是需要产生一系列产品,若需更换产品,则要求一系列产品一起换,且要控制一系列产品的产生过程,此时可考虑抽象工厂模式.例:小明装修屋子,把电视.冰箱都替换掉,他这次需要把电视 ...

  8. Java-马士兵设计模式学习笔记-工厂模式-简单工厂

    一.概述 1.目标:要控制任意类型交通工具的生产模式 2.目标有两层意思(1)任意类型 (2)生产模式,所以对应的,要这两个层面上抽象(Movable,VehicleFactory),利用接口,实现多 ...

  9. Java-马士兵设计模式学习笔记-策略模式-模拟 Comparator接口

    续上一篇  <Java 模拟 Comparable接口> 一.Teacher类及Student类的比较大小方式是不固定的,比如老师除了比较职称外,还可比较工龄大小,年龄大小等.则定义Com ...

随机推荐

  1. netty reactor线程模型分析

    netty4线程模型 ServerBootstrap http示例 // Configure the server. EventLoopGroup bossGroup = new EpollEvent ...

  2. 清除浮动.md

    清除浮动的三种方法 1 加空div层(.clear) 2 overflow属性设置(.clearo) 3 :after伪元素(.clearfix) <!DOCTYPE html> < ...

  3. LA 3882 - And Then There Was One(约瑟夫 递归)

    看题传送门 题目大意: N个数排成一圈,第一次删除m,以后每k个数删除一次,求最后一被删除的数. 如果这题用链表或者数组模拟整个过程的话,时间复杂度都将高达O(nk),而n<=10000,k&l ...

  4. Shiro的Web项目配置(转)

    Shiro的Web项目配置 一 shiro的学习 二 shiro的java客户端配置 三 关于权限的一些问题 一 shiro的学习 官网和张开涛博客 二 shiro的java客户端配置 1.在web. ...

  5. 安装spark1.3.1单机环境 分类: B8_SPARK 2015-04-27 14:52 1873人阅读 评论(0) 收藏

    本文介绍安装spark单机环境的方法,可用于测试及开发.主要分成以下4部分: (1)环境准备 (2)安装scala (3)安装spark (4)验证安装情况 1.环境准备 (1)配套软件版本要求:Sp ...

  6. [array] leetCode-15. 3Sum-Medium

    leetCode-15. 3Sum-Medium descrition Given an array S of n integers, are there elements a, b, c in S ...

  7. 解密Arm中国:全球最具影响力的芯片公司中国布局浮出水面

    经济观察报 记者 陈伊凡 沈怡然 李华清 对于Arm与中国合资公司事宜,5月4日下午,Arm授权的代表邮件回复<经济观察报>称:“合资公司目前刚开始运营”,“我们的重点是让这个新的合资公司 ...

  8. 《SPA设计与架构》之认识SPA

    原文 简书原文:https://www.jianshu.com/p/84323f530223 大纲 前言 1.什么是单页面应用程序(SPA) 2.SPA与传统Web应用的区别 3.关于SPA的使用 4 ...

  9. HTTP请求头与响应头

    http://m.blog.csdn.net/article/details?id=48918857 本篇文章中,将学习一下HTTP请求头与响应头的知识. 一.HTTP头引入: 正确的设置HTTP头部 ...

  10. 使用Perl批量读取文件最后行

    使用Perl批量读取文件最后行 面对成百上千个文件,有时我们需要查看它的最后行,单个文件打开将耗费大量时间,而通过Perl提取出最后行,将快速的帮助我们处理繁琐的事务. 特性 整个目录完全遍历,自动提 ...