[译]Java 设计模式之命令
(文章翻译自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 设计模式之命令的更多相关文章
- 折腾Java设计模式之命令模式
博客原文地址 折腾Java设计模式之命令模式 命令模式 wiki上的描述 Encapsulate a request as an object, thereby allowing for the pa ...
- Java设计模式 之 命令模式
1 从属模式分类 行为性模式 2 命令模式意图 命令模式可将动作的请求者和动作的执行者对象中解耦. 该模式将一个行为操作发起者的请求封装到对象中,该请求由另外一个对象执行. 将动作 ...
- [译]Java 设计模式之单例
(文章翻译自Java Design Pattern: Singleton) 单例是在Java最经常被用到的设计模式.它通过阻止其他的实例化和修改来用于控制创建对象的数目.这一特性可应用于那些当只有一个 ...
- [译]Java 设计模式之桥接
(文章翻译自Java Design Pattern: Bridge) 简单来说,桥梁设计模式是一个两层的抽象. 桥接模式就是从一个抽象中实现中解耦以便两个都可以独立的改变.桥接使用封装聚合而且使用继承 ...
- [译]Java 设计模式之装饰器
(文章翻译自Java Design Pattern: Decorator – Decorate your girlfriend) 1.装饰模式的来历 让我们假设你在寻找一个女朋友.有来自像没美国中国日 ...
- [译]Java 设计模式 之模板方法
(文章翻译自Java Design Pattern: Template Method) 模板方法设计模式定义了归档特定操作的工作流.它允许子类去修改特定的步奏而不用改变工作流的结构. 下面的例子表示模 ...
- [译]Java 设计模式之工厂
(文章翻译自Java Design Pattern: Factory) 1.Java工厂模式的来历 工厂设计模式用于创建基于不同参数的对象.下面的例子就是在一个工厂里创建一个人.如果我们向工厂要一个b ...
- [译]Java 设计模式之外观
(文章翻译自Java Design Pattern: Facade) 外观设计模式隐藏了任务的复杂性而只是提供了一个简单的接口.一个非常好的例子就是计算机的启动.当一个计算机启动的时候,它涉及CUP. ...
- [译]Java 设计模式之中介者
(文章翻译自Java Design Pattern: Mediator) 中介者设计模式被用于一组的同事进行协作.这些同事不彼此进行直接的交流联系,但是是通过中介者. 在下面的例子中,A同事想去说话, ...
随机推荐
- RH253读书笔记(9)-Lab 9 Account Management Methods
Lab 9 Account Management Methods Goal: To build skills with PAM configuration Sequence 1: Track Fail ...
- springmvc 接收对象 滴灌摘要
js 对象 该阵列看起来像 我明白http://blog.csdn.net/baicp3/article/details/12752255本文 我们指示样品棒 data3一个js对象.遗嘱java当代 ...
- “NET网络”进行中,多管齐下的人才力挫“”粗俗
随着互联网的迅猛发展,一些不太干净.低俗的甚至色情的内容不断浮现.不仅严重影响了我们的上网体验,也成为扰乱互联网正常秩序的罪魁祸首,部分不法内容甚至给网民造成了一定的財产损失.在这样的 ...
- jsoup分解HTML DOM
采用jsoup 分解HTML .使用和JS操作DOM分类似. 示例代码: import java.io.IOException; import org.jsoup.Jsoup; import org. ...
- Android GPS获取当前经纬度坐标
APP中可能会遇到一种需求,就是将当前所在位置的坐标传到server上,今天我提供三种途径去获取经纬度坐标信息,第一种是通过Android API来实现,另外一种通过百度地图API来实现,第三种通过天 ...
- Writing your first Django app, part 1(转)
Let’s learn by example. Throughout this tutorial, we’ll walk you through the creation of a basic pol ...
- Spark里边:到底是什么RDD
RDD它是Spark基,它是最根本的数据抽象.http://www.cs.berkeley.edu/~matei/papers/2012/nsdi_spark.pdf 它开着RDD文件.假设英语阅读太 ...
- spring框架内置笔记本
◆基本介绍 目的:解决企业应用开发的复杂性 特征:使用主JavaBean更换EJB,它提供了许多其他的企业应用 范围:随你Java应用 Spring 框架是一个分层架构.由 7 个定义良好的模块组成. ...
- 独立博客网站FansUnion.cn操作2多年的经验和教训以及未来计划
今天,我把运营了2年的独立博客站点FansUnion给"归零"了. 2012年6月.我成功搭建了自己的博客站点FansUnion.cn,这是因为自己的不懈努力和时代发展成就的 ...
- IEnumerable,IQueryable的区别
IEnumerable,IQueryable之前世今生 IEnumerable<T>在.Net2.0中我们已经很熟悉了.你想要利用Foreach迭代吗?实现IEnumerable<T ...