命令模式 为了降低耦合度,将请求的发送者和接收者解耦 发送请求的对象只需要哦知道如何发送请求,而不必知道如何完成请求 对请求排队 记录请求日志 支持撤销操作 核心在于引入命令类 角色 抽象命令类Command 具体命令类ConcreteCommand 调用者Invoker 接收者Receiver 请求的一方不必知道接收请求的一方,更不必知道请求如何被接收、执行、怎么执行、何时执行
看例子,电视遥控器,电视,人。
1.抽象命令类,或接口
package Command;
/**
* Created by Jiqing on 2016/10/26.
*/
public interface AbstractCommand {
public void execute();
}
2.命令接收者电视机
package Command;
/**
* Created by Jiqing on 2016/10/26.
*/
public class Television {
public void open() {
System.out.println("打开电视机");
}
public void close() {
System.out.println("关闭电视机");
}
public void changeChannel() {
System.out.println("切换电视频道");
}
}
3.具体命令
a.打开命令
package Command;
/**
* Created by Jiqing on 2016/10/26.
*/
public class TVOpenCommand implements AbstractCommand{
private Television tv;
public TVOpenCommand() {
tv = new Television();
}
public void execute() {
tv.open();
}
}
b.关闭命令
package Command;
/**
* Created by Jiqing on 2016/10/26.
*/
public class TVCloseCommand implements AbstractCommand{
private Television tv;
public TVCloseCommand() {
tv = new Television();
}
public void execute() {
tv.close();
}
}
c.更改频道命令
package Command;
/**
* Created by Jiqing on 2016/10/26.
*/
public class TVChangeCommand implements AbstractCommand{
private Television tv;
public TVChangeCommand() {
tv = new Television();
}
public void execute() {
tv.changeChannel();
}
}
4.命令发布者控制者,人
package Command;
/**
* Created by Jiqing on 2016/10/26.
*/
public class Controller {
private AbstractCommand openCommand,closeCommand,changeCommand;
public Controller(AbstractCommand openCommand,AbstractCommand closeCommand,AbstractCommand changeCommand) {
this.openCommand = openCommand;
this.closeCommand = closeCommand;
this.changeCommand = changeCommand;
}
public void open() {
openCommand.execute();
}
public void change() {
changeCommand.execute();
}
public void close() {
closeCommand.execute();
}
}
5.客户端
package Command;
/**
* Created by Jiqing on 2016/10/26.
*/
public class Client {
public static void main(String a[]) {
AbstractCommand openCommand,closeCommand,changeCommand;
openCommand = new TVOpenCommand();
closeCommand = new TVCloseCommand();
changeCommand = new TVChangeCommand();
Controller control = new Controller(openCommand,closeCommand,changeCommand);
control.open();
control.close();
control.change();
}
}
执行结果:
打开电视机
关闭电视机
切换电视频道
再看一个撤销案例
package Command.Undo;
abstract class AbstractCommand
{
public abstract int execute(int value);
public abstract int undo();
}
class ConcreteCommand extends AbstractCommand
{
private Adder adder = new Adder();
private int value;
public int execute(int value)
{
this.value=value;
return adder.add(value);
}
public int undo()
{
return adder.add(-value);
}
}
class CalculatorForm
{
private AbstractCommand command;
public void setCommand(AbstractCommand command)
{
this.command=command;
}
public void compute(int value)
{
int i = command.execute(value);
System.out.println("执行运算,运算结果为:" + i);
}
public void undo()
{
int i = command.undo();
System.out.println("执行撤销,运算结果为:" + i);
}
}
class Adder
{
private int num=0;
public int add(int value)
{
num+=value;
return num;
}
}
class Client
{
public static void main(String args[])
{
CalculatorForm form = new CalculatorForm();
ConcreteCommand command = new ConcreteCommand();
form.setCommand(command);
form.compute(10);
form.compute(5);
form.compute(10);
form.undo();
}
}
执行结果:
执行运算,运算结果为:10
执行运算,运算结果为:15
执行运算,运算结果为:25
执行撤销,运算结果为:15
看类图
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6002401.html,如需转载请自行联系原作者