- 浏览: 18305 次
- 性别:
- 来自: 西安
最新评论
1).下面以Boss和Clerk为例:
Clerk.java代码如下:
package com.flyingh.receiver; public class Clerk { public void action() { System.out.println("**************"); } }
Command.java代码如下:
package com.flyingh.icommand; public interface Command { void execute(); }
ConcreteCommand.java代码如下:
package com.flyingh.command; import com.flyingh.icommand.Command; import com.flyingh.receiver.Clerk; public class ConcreteCommand implements Command { private Clerk clerk; public ConcreteCommand(Clerk clerk) { super(); this.clerk = clerk; } @Override public void execute() { // TODO Auto-generated method s clerk.action(); } }
Boss.java代码如下:
package com.flyingh.invoker; import com.flyingh.icommand.Command; public class Boss { private Command command; public Command getCommand() { return command; } public void setCommand(Command command) { this.command = command; } public void action() { command.execute(); } }
Client.java代码如下:
package com.flyingh.client; import com.flyingh.command.ConcreteCommand; import com.flyingh.icommand.Command; import com.flyingh.invoker.Boss; import com.flyingh.receiver.Clerk; public class Client { public static void main(String[] args) { Boss boss = new Boss(); Clerk clerk = new Clerk(); Command command = new ConcreteCommand(clerk); boss.setCommand(command); boss.action(); } }
程序运行结果如下:
**************
2).下面以Invoker点击三个按钮(on,off,undo)来控制Light的on,off和undo为例,代码如下:
Command.java
package com.flyingh.icommand; public interface Command { void execute(); void undo(); }
Light.java
package com.flyingh.receiver; public class Light { public void on() { // TODO Auto-generated method stub System.out.println("turn on the light!"); } public void off() { // TODO Auto-generated method stub System.out.println("turn off the light!"); } }
LightOnCommand.java
package com.flyingh.command; import com.flyingh.icommand.Command; import com.flyingh.receiver.Light; public class LightOnCommand implements Command { private Light light; public LightOnCommand(Light light) { super(); this.light = light; } @Override public void execute() { // TODO Auto-generated method stub light.on(); } @Override public void undo() { // TODO Auto-generated method stub light.off(); } }
LightOffCommand.java
package com.flyingh.command; import com.flyingh.icommand.Command; import com.flyingh.receiver.Light; public class LightOffCommand implements Command { private Light light; public LightOffCommand(Light light) { super(); this.light = light; } @Override public void execute() { // TODO Auto-generated method stub light.off(); } @Override public void undo() { // TODO Auto-generated method stub light.on(); } }
Invoker.java
package com.flyingh.invoker; import com.flyingh.icommand.Command; public class Invoker { private Command lightOnCommand; private Command lightOffCommand; private Command undoCommand; public Invoker() { super(); // TODO Auto-generated constructor stub } public Invoker(Command lightOnCommand, Command lightOffCommand) { super(); this.lightOnCommand = lightOnCommand; this.lightOffCommand = lightOffCommand; } public void OnButtonClick() { lightOnCommand.execute(); undoCommand = lightOnCommand; } public void OffButtonClick() { lightOffCommand.execute(); undoCommand = lightOffCommand; } public void undoButtonClick() { undoCommand.undo(); } }
Client.java
package com.flyingh.client; import com.flyingh.command.LightOffCommand; import com.flyingh.command.LightOnCommand; import com.flyingh.invoker.Invoker; import com.flyingh.receiver.Light; public class Client { public static void main(String[] args) { Light light = new Light(); LightOnCommand lightOnCommand = new LightOnCommand(light); LightOffCommand lightOffCommand = new LightOffCommand(light); Invoker invoker = new Invoker(lightOnCommand, lightOffCommand); invoker.OnButtonClick(); invoker.OffButtonClick(); invoker.undoButtonClick(); } }
程序运行结果如下:
turn on the light! turn off the light! turn on the light!
3).一次打开或者关闭多个(宏命令):
Command.java
package com.flyingh.icommand; public interface Command { void execute(); void undo(); }
Receiver.java
package com.flyingh.ireceiver; public interface Receiver { void on(); void off(); }
MacroOnCommand.java
package com.flyingh.command; import java.util.List; import com.flyingh.icommand.Command; import com.flyingh.ireceiver.Receiver; public class MacroOnCommand implements Command { private List<Receiver> list; public MacroOnCommand(List<Receiver> list) { super(); this.list = list; } @Override public void execute() { // TODO Auto-generated method stub for (Receiver receiver : list) { receiver.on(); } } @Override public void undo() { // TODO Auto-generated method stub for (Receiver receiver : list) { receiver.off(); } } }
MacroOffCommand.java
package com.flyingh.command; import java.util.List; import com.flyingh.icommand.Command; import com.flyingh.ireceiver.Receiver; public class MacroOffCommand implements Command { private List<Receiver> list; public MacroOffCommand(List<Receiver> list) { super(); this.list = list; } @Override public void execute() { // TODO Auto-generated method stub for (Receiver receiver : list) { receiver.off(); } } @Override public void undo() { // TODO Auto-generated method stub for (Receiver receiver : list) { receiver.on(); } } }
Light.java
package com.flyingh.receiver; import com.flyingh.ireceiver.Receiver; public class Light implements Receiver { @Override public void on() { // TODO Auto-generated method stub System.out.println("turn on the light!"); } @Override public void off() { // TODO Auto-generated method stub System.out.println("turn off the light!"); } }
TV.java
package com.flyingh.receiver; import com.flyingh.ireceiver.Receiver; public class TV implements Receiver { @Override public void on() { // TODO Auto-generated method stub System.out.println("turn on the TV!"); } @Override public void off() { // TODO Auto-generated method stub System.out.println("turn off the TV!"); } }
Radio.java
package com.flyingh.receiver; import com.flyingh.ireceiver.Receiver; public class Radio implements Receiver { @Override public void on() { // TODO Auto-generated method stub System.out.println("turn on the radio!"); } @Override public void off() { // TODO Auto-generated method stub System.out.println("turn off the radio!"); } }
Invoker.java
package com.flyingh.invoker; import com.flyingh.icommand.Command; public class Invoker { private Command macroOnCommand; private Command macroOffCommand; private Command macroUndoCommand; public Invoker() { super(); // TODO Auto-generated constructor stub } public Invoker(Command lightOnCommand, Command lightOffCommand) { super(); this.macroOnCommand = lightOnCommand; this.macroOffCommand = lightOffCommand; } public void onButtonClick() { macroOnCommand.execute(); macroUndoCommand = macroOnCommand; } public void offButtonClick() { macroOffCommand.execute(); macroUndoCommand = macroOffCommand; } public void undoButtonClick() { macroUndoCommand.undo(); } }
Client.java
package com.flyingh.client; import java.util.ArrayList; import java.util.List; import com.flyingh.command.MacroOffCommand; import com.flyingh.command.MacroOnCommand; import com.flyingh.icommand.Command; import com.flyingh.invoker.Invoker; import com.flyingh.ireceiver.Receiver; import com.flyingh.receiver.Light; import com.flyingh.receiver.Radio; import com.flyingh.receiver.TV; public class Client { public static void main(String[] args) { Receiver light = new Light(); Receiver tv = new TV(); Receiver radio = new Radio(); List<Receiver> list = new ArrayList<Receiver>(); list.add(light); list.add(tv); list.add(radio); Command macroOnCommand = new MacroOnCommand(list); Command macroOffCommand = new MacroOffCommand(list); Invoker invoker = new Invoker(macroOnCommand, macroOffCommand); invoker.onButtonClick(); invoker.offButtonClick(); invoker.undoButtonClick(); } }
程序运行结果如下:
turn on the light! turn on the TV! turn on the radio! turn off the light! turn off the TV! turn off the radio! turn on the light! turn on the TV! turn on the radio!
发表评论
文章已被作者锁定,不允许评论。
-
Interpreter
2011-11-24 21:19 839Interpreter Pattern:其意图是给定一个语言, ... -
Visitor
2011-11-24 17:37 659Visitor Pattern:表示一个作用于某对象结构中的各 ... -
Mediator
2011-11-24 16:22 663Mediator Pattern: 用一 ... -
Prototype
2011-11-24 11:32 605Prototype Pattern:就是通过复制现在已经存在的 ... -
Memento
2011-11-23 21:56 685Memento Pattern:在不破坏封闭的前提下,捕获一个 ... -
Flyweight
2011-11-23 18:38 651享元模式以共享的方式高效地支持大量的细粒度对象. 1).Fl ... -
Builder
2011-11-22 16:38 763Builder Pattern:可以将一个产品的内部表象与产品 ... -
Bridge
2011-11-22 15:05 684桥梁模式的用意是"将抽象化(Abstraction) ... -
Chain Of Responsibility
2011-11-21 23:05 658Chain Of Responsibility Pattern ... -
Abstract Factory
2011-11-21 22:00 644下面以Mac和PC的CPU和RAM为例,代码如下: Cpu. ... -
Proxy
2011-11-18 18:47 661Proxy Pattern:对其他对象提供一种代理以控制对这个 ... -
State
2011-11-18 15:57 678State Pattern:当一个对象的内在状态改变时允许改变 ... -
Composite
2011-11-17 17:09 793Composite Pattern:意图是将对象组合成树形结构 ... -
Iterator
2011-11-16 16:56 890Iterator Pattern:其意图是提供一种方法顺序访问 ... -
Template Method
2011-11-16 11:43 659模板方法模式:在一个方法中定义一个算法的骨架,而将一些实 ... -
Facade
2011-11-15 23:00 596Facade Pattern:为子系统中的各类(或结构与方法) ... -
Adapter
2011-11-15 21:43 630Adapter Pattern:将一个类的接口,转换成客户所期 ... -
Factory Method
2011-11-13 20:44 595Simple Factory: 以Fruit为例,以下是实现 ... -
Decorator
2011-11-12 23:09 570装饰者可以在所委托被装饰者的行为之前与/或之后,加上自己的 ... -
Observer
2011-11-12 21:30 600观 察 者 模 式 定义了对象之间的一对多依赖,这样一来 ...
相关推荐
Fiery Command WorkStation 中文操作说明书 Fiery Command WorkStation 是一款功能强大的工作站软件,由 Electronics For Imaging, Inc. 公司开发,旨在帮助用户更好地管理和控制Fiery Server。下面是 Fiery ...
**命令模式(Command Pattern)详解** 命令模式是一种行为设计模式,它将请求封装为一个对象,使得你可以使用不同的请求、队列或者日志请求,也可以支持可撤销的操作。在C++中实现命令模式,可以有效地解耦调用者和...
DOSCommand 是一个 Delphi 组件,它允许 Delphi 应用程序执行 DOS 命令或 shell 命令。这个组件通常用于执行那些在 Windows 命令行或 DOS 提示符下可以运行的命令和程序。以下是 DOSCommand 组件的一些基本功能: ...
AutoLISP 调用 Command 命令详解 AutoLISP 是一种基于 Lisp 语言的脚本语言,广泛应用于 AutoCAD 等 CAD 软件中。通过 AutoLISP,可以调用 AutoCAD 的图形命令,实现自动化绘图、编辑和处理。下面将详细介绍 Auto...
Welcome to the Command Reference.This reference contains a complete dictionary of detailed command descriptions, arranged in alphabetical order. It is the definitive resource for correct command ...
然而,在MVVM中,我们常常遇到一个问题:并非所有的控件都内置了`Command`属性,这使得像按钮(Button)那样能够方便地通过`Command`绑定到ViewModel中的命令方法。为了处理这种情况,我们需要一些技巧来实现无`...
灰色的命令一般很少使用,所以往往在具体实现中不被支持,所以可能返回的信息是“500 'xx': command not understood”。 命令 描述 ABOR 中断数据连接程序 ACCT <account> 系统特权帐号 ALLO <bytes> 为服务器上的...
标题中的“惠普暗影精灵CommandCenter按键重新安装办法”是指惠普(HP)旗下的一款游戏笔记本系列——暗影精灵(OMEN)的Command Center软件的恢复或重新安装过程。Command Center是惠普为游戏玩家提供的一个工具,...
DosCommand 是一个在 Delphi 开发环境中用于执行操作系统命令并获取其输出的组件。在早期的 Delphi 版本如 D2006 中,它是一个常用的工具,但随着 Delphi 的更新, DosCommand 逐渐变得不兼容。然而,根据你的描述,...
Fiery Command WorkStation 5是EFI(Electronics for Imaging)公司推出的一款强大的打印工作流程管理软件,专为专业印刷和图形艺术行业设计。这个中文版本的发布,使得中国用户能够更加方便地操作和管理他们的印刷...
命令分为两种类型:依赖属性命令(DependencyProperty-based Commands,如ButtonBase.Command)和实现ICommand接口的自定义命令。 1. **ICommand接口**:这是定义命令行为的基础,包含Execute和CanExecute方法。...
Command Line Tools(commandline-tools-windows-x64-5.0.3.900.zip)适用于Windows系统,集合了HarmonyOS应用开发所用到的系列工具,包括代码检查codelinter、三方库包管理ohpm、命令行解析hstack、编译构建hvigorw...
public void setCommand(Command command) { this.command = command; } public void executeCommand() { command.execute(); } } // 接收者 public class Receiver { public void action() { // 执行具体...
《Allure Commandline工具详解与使用》 在软件测试领域,报告工具的使用至关重要,它能够帮助我们清晰地理解测试结果,找出问题所在。其中,Allure Framework是一款强大的、直观的测试报告工具,而Allure Command...
命令模式(Command Pattern)是一种行为设计模式,它将请求封装为一个对象,使得你可以使用不同的请求、队列请求,或者支持可撤销的操作。在Java中实现命令模式,我们可以利用面向对象编程的特性来构建系统,使得...
### MIPI Video Mode 与 Command Mode 的区别 #### 一、引言 MIPI (Mobile Industry Processor Interface) 是一种用于连接手机和其他移动设备中的处理器及其外围设备的标准接口。MIPI 接口支持多种协议,包括 DSI ...
MVVM模式鼓励将业务逻辑和UI逻辑分离,`Command`对象承载了这些逻辑,而`Command Binding`则负责将`Command`与UI元素的事件关联。这样,当指定的事件发生时,`Command`会被触发执行其对应的操作,无需在代码-behind...
**命令模式(Command模式)详解** 命令模式是一种行为设计模式,它将请求封装为一个对象,使得我们可以使用不同的请求、队列或者记录请求日志,还可以支持可撤销的操作。在命令模式中,我们创建表示各种操作的类...
解析主机为: 112.125.43.138 已连接. 正等待响应. 220 Microsoft FTP Service USER 123000 331 Password required for 123. ...500 Invalid PORT Command. PASV 227 Entering Passive Mode (10,247,80,183,117,51).