(文章翻译自Java Design Pattern: Command)

命令设计模式在进行执行和记录的时候需要一个操作及其参数和封装在一个对象里面。在下面的例子中,命令是一个操作,它的参数是一个Computer,而且他们被封装在一个Switch中。

从另外一个视角来看,命令模式有四个部分:command,recevier,invoker和client。在这个例子中,Switch是invoker,Computer是receiver。一个具体的Command需要一个receiver对象而且调用receiver的方法。invok儿使用不同的具体Command。Client决定对于receiver去使用哪个command.

命令设计模式类图

Java命令模式例子

package designpatterns.command;

import java.util.List;
import java.util.ArrayList; /* The Command interface */
interface Command {
void execute();
} // in this example, suppose you use a switch to control computer /* The Invoker class */
class Switch {
private List<Command> history = new ArrayList<Command>(); public Switch() {
} public void storeAndExecute(Command command) {
this.history.add(command); // optional, can do the execute only!
command.execute();
}
} /* The Receiver class */
class Computer { public void shutDown() {
System.out.println("computer is shut down");
} public void restart() {
System.out.println("computer is restarted");
}
} /* The Command for shutting down the computer*/
class ShutDownCommand implements Command {
private Computer computer; public ShutDownCommand(Computer computer) {
this.computer = computer;
} public void execute(){
computer.shutDown();
}
} /* The Command for restarting the computer */
class RestartCommand implements Command {
private Computer computer; public RestartCommand(Computer computer) {
this.computer = computer;
} public void execute() {
computer.restart();
}
} /* The client */
public class TestCommand {
public static void main(String[] args){
Computer computer = new Computer();
Command shutdown = new ShutDownCommand(computer);
Command restart = new RestartCommand(computer); Switch s = new Switch(); String str = "shutdown"; //get value based on real situation if(str == "shutdown"){
s.storeAndExecute(shutdown);
}else{
s.storeAndExecute(restart);
}
}
}

[译]Java 设计模式之命令的更多相关文章

  1. 折腾Java设计模式之命令模式

    博客原文地址 折腾Java设计模式之命令模式 命令模式 wiki上的描述 Encapsulate a request as an object, thereby allowing for the pa ...

  2. Java设计模式 之 命令模式

    1      从属模式分类 行为性模式 2      命令模式意图 命令模式可将动作的请求者和动作的执行者对象中解耦. 该模式将一个行为操作发起者的请求封装到对象中,该请求由另外一个对象执行. 将动作 ...

  3. [译]Java 设计模式之单例

    (文章翻译自Java Design Pattern: Singleton) 单例是在Java最经常被用到的设计模式.它通过阻止其他的实例化和修改来用于控制创建对象的数目.这一特性可应用于那些当只有一个 ...

  4. [译]Java 设计模式之桥接

    (文章翻译自Java Design Pattern: Bridge) 简单来说,桥梁设计模式是一个两层的抽象. 桥接模式就是从一个抽象中实现中解耦以便两个都可以独立的改变.桥接使用封装聚合而且使用继承 ...

  5. [译]Java 设计模式之装饰器

    (文章翻译自Java Design Pattern: Decorator – Decorate your girlfriend) 1.装饰模式的来历 让我们假设你在寻找一个女朋友.有来自像没美国中国日 ...

  6. [译]Java 设计模式 之模板方法

    (文章翻译自Java Design Pattern: Template Method) 模板方法设计模式定义了归档特定操作的工作流.它允许子类去修改特定的步奏而不用改变工作流的结构. 下面的例子表示模 ...

  7. [译]Java 设计模式之工厂

    (文章翻译自Java Design Pattern: Factory) 1.Java工厂模式的来历 工厂设计模式用于创建基于不同参数的对象.下面的例子就是在一个工厂里创建一个人.如果我们向工厂要一个b ...

  8. [译]Java 设计模式之外观

    (文章翻译自Java Design Pattern: Facade) 外观设计模式隐藏了任务的复杂性而只是提供了一个简单的接口.一个非常好的例子就是计算机的启动.当一个计算机启动的时候,它涉及CUP. ...

  9. [译]Java 设计模式之中介者

    (文章翻译自Java Design Pattern: Mediator) 中介者设计模式被用于一组的同事进行协作.这些同事不彼此进行直接的交流联系,但是是通过中介者. 在下面的例子中,A同事想去说话, ...

随机推荐

  1. css3 3d旋转动画

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. java.util.Timer demo good

    package timer; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import org ...

  3. printf交替使用

    今天附带printf一些替代实现. 转载请注明出处:http://blog.csdn.net/u010484477谢谢^_^ 我们总是用printf做各种输出语句: printf("%d&q ...

  4. Git 1.9.5.msysgit.1

    Git 1.9.5.msysgit.1 发布,现已提供下载:https://github.com/msysgit/git/archive/v1.9.5.msysgit.1.zip. Git是一个开源的 ...

  5. 谈论HashMap,HashSet,HashTableeasy被我们忽视

    在正常发育,HashMap,HashTable,HashSet 他们批准了经常使用的按键值地图数据结构.在这里,我主要写一些平时我们使用的这些数据结构easy忽略. HashMap HashMap的结 ...

  6. DevExpress XtraReports 入门六 控件以程序方式创建一个 交叉表 报表

    原文:DevExpress XtraReports 入门六 控件以程序方式创建一个 交叉表 报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助 ...

  7. sql 子查询stuff功能(同一个人的多任务,多领域成为字符串)

    USE [erp2015] GO /****** Object: StoredProcedure [dbo].[GetUser] Script Date: 03/14/2015 13:27:04 ** ...

  8. bigdata_Hadoop jps出现process information unavailable提示解决办法

    启动Hadoop之后,使用jps命令查看当前系统的java进程情况,显示: hduser@jack:/usr/local/hadoop$ jps 18470 SecondaryNameNode 190 ...

  9. 蓝桥杯 BASIC 27 矩阵乘法(矩阵、二维数组)

    [思路]:注意0次幂是单位矩阵. [AC代码]: #include <iostream> #include <algorithm> #include <iomanip&g ...

  10. HTML5多图片拖拽上传带进度条

    前言 昨天利用css2的clip属性实现了网页进度条觉得还不错,但是很多情况下,我们在那些时候用进度条呢,一般网页加载的时候如果有需要可以用,那么问题就来了,怎么才算整个加载完毕呢,是页面主要模块加载 ...