`
Flyingh
  • 浏览: 18305 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Command

 
阅读更多

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!
 
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Command_WorkStation中文操作说明书

    Fiery Command WorkStation 中文操作说明书 Fiery Command WorkStation 是一款功能强大的工作站软件,由 Electronics For Imaging, Inc. 公司开发,旨在帮助用户更好地管理和控制Fiery Server。下面是 Fiery ...

    command 模式的c++实现

    **命令模式(Command Pattern)详解** 命令模式是一种行为设计模式,它将请求封装为一个对象,使得你可以使用不同的请求、队列或者日志请求,也可以支持可撤销的操作。在C++中实现命令模式,可以有效地解耦调用者和...

    DOSCommand for Delphi11.zip

    DOSCommand 是一个 Delphi 组件,它允许 Delphi 应用程序执行 DOS 命令或 shell 命令。这个组件通常用于执行那些在 Windows 命令行或 DOS 提示符下可以运行的命令和程序。以下是 DOSCommand 组件的一些基本功能: ...

    AutoLISP调用command命令.doc

    AutoLISP 调用 Command 命令详解 AutoLISP 是一种基于 Lisp 语言的脚本语言,广泛应用于 AutoCAD 等 CAD 软件中。通过 AutoLISP,可以调用 AutoCAD 的图形命令,实现自动化绘图、编辑和处理。下面将详细介绍 Auto...

    ANSYS Mechanical APDL Command Reference.pdf

    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 ...

    WPF MVVM无Command属性的控件绑定事件

    然而,在MVVM中,我们常常遇到一个问题:并非所有的控件都内置了`Command`属性,这使得像按钮(Button)那样能够方便地通过`Command`绑定到ViewModel中的命令方法。为了处理这种情况,我们需要一些技巧来实现无`...

    FTP协议的命令command与返回码

    灰色的命令一般很少使用,所以往往在具体实现中不被支持,所以可能返回的信息是“500 'xx': command not understood”。 命令 描述 ABOR 中断数据连接程序 ACCT &lt;account&gt; 系统特权帐号 ALLO &lt;bytes&gt; 为服务器上的...

    惠普暗影精灵CommandCenter按键重新安装办法

    标题中的“惠普暗影精灵CommandCenter按键重新安装办法”是指惠普(HP)旗下的一款游戏笔记本系列——暗影精灵(OMEN)的Command Center软件的恢复或重新安装过程。Command Center是惠普为游戏玩家提供的一个工具,...

    可以在XE下使用的DosCommand,捕获控制台输出(XE-XE5)

    DosCommand 是一个在 Delphi 开发环境中用于执行操作系统命令并获取其输出的组件。在早期的 Delphi 版本如 D2006 中,它是一个常用的工具,但随着 Delphi 的更新, DosCommand 逐渐变得不兼容。然而,根据你的描述,...

    Fiery Command WorkStation 5中文版.rar

    Fiery Command WorkStation 5是EFI(Electronics for Imaging)公司推出的一款强大的打印工作流程管理软件,专为专业印刷和图形艺术行业设计。这个中文版本的发布,使得中国用户能够更加方便地操作和管理他们的印刷...

    WPF Binding绑定 Command命令基础学习源码

    命令分为两种类型:依赖属性命令(DependencyProperty-based Commands,如ButtonBase.Command)和实现ICommand接口的自定义命令。 1. **ICommand接口**:这是定义命令行为的基础,包含Execute和CanExecute方法。...

    commandline-tools-windows-x64-5.0.3.900.zip.003

    Command Line Tools(commandline-tools-windows-x64-5.0.3.900.zip)适用于Windows系统,集合了HarmonyOS应用开发所用到的系列工具,包括代码检查codelinter、三方库包管理ohpm、命令行解析hstack、编译构建hvigorw...

    命令模式_Command

    public void setCommand(Command command) { this.command = command; } public void executeCommand() { command.execute(); } } // 接收者 public class Receiver { public void action() { // 执行具体...

    allure-commandline压缩文件

    《Allure Commandline工具详解与使用》 在软件测试领域,报告工具的使用至关重要,它能够帮助我们清晰地理解测试结果,找出问题所在。其中,Allure Framework是一款强大的、直观的测试报告工具,而Allure Command...

    命令模式command pattern

    命令模式(Command Pattern)是一种行为设计模式,它将请求封装为一个对象,使得你可以使用不同的请求、队列请求,或者支持可撤销的操作。在Java中实现命令模式,我们可以利用面向对象编程的特性来构建系统,使得...

    MIPI_video_mode_和_command_mode_的区别

    ### MIPI Video Mode 与 Command Mode 的区别 #### 一、引言 MIPI (Mobile Industry Processor Interface) 是一种用于连接手机和其他移动设备中的处理器及其外围设备的标准接口。MIPI 接口支持多种协议,包括 DSI ...

    WPF ComboBox Command Binding

    MVVM模式鼓励将业务逻辑和UI逻辑分离,`Command`对象承载了这些逻辑,而`Command Binding`则负责将`Command`与UI元素的事件关联。这样,当指定的事件发生时,`Command`会被触发执行其对应的操作,无需在代码-behind...

    命令模式(Command模式)

    **命令模式(Command模式)详解** 命令模式是一种行为设计模式,它将请求封装为一个对象,使得我们可以使用不同的请求、队列或者记录请求日志,还可以支持可撤销的操作。在命令模式中,我们创建表示各种操作的类...

    ftp连接不上出现500 Invalid PORT 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).

Global site tag (gtag.js) - Google Analytics