Java设计模式-命令模式

来源:互联网 发布:c语言主函数参数 编辑:程序博客网 时间:2024/06/01 09:13

1、概念

(1)定义

将来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化。用于“行为请求者”与“行为实现者”解耦,可实现二者之间的松耦合,以便适应变化。

(2)名词

(1)Command:命令接口,声明执行的方法。

(2)SubClassCommand:命令接口实现对象(即命令对象),通常会持有执行者,并调用执行者的功能来完成命令要执行的操作。

(3)Executor执行者:真正执行命令的对象,任何类都可能成为一个接收者,只要它能够实现命令要求实现的相应功能。

(4)Controller控制者:要求命令对象执行请求,通常会持有命令对象,可以持有很多的命令对象。这个是客户端真正触发命令并要求命令执行相应操作的地方,也就是说相当于使用命令对象的入口。

(5)Client客户端:创建具体的命令对象,真正使用命令的客户端是从Controller控制者来触发执行。

(3)优缺点

(1)系统需要将请求调用者和请求接收者解耦,使得调用者和接收者不直接交互,降低对象之间的耦合度。

(2)使用命令模式可能会导致某些系统有过多的具体命令类,因为针对每一个命令都需要设计一个具体命令类,因此某些系统可能需要大量具体命令类,这将影响命令模式的使用。

2、代码实现

//命令接口public interface Command {    void execute();  }  
/*** SubClassCommand命令对象*///开机命令CommandOnpublic class CommandOn implements Command {    private Executor mExecutor;    public CommandOn(Executor executor) {       mExecutor= executor;    }    public void execute() {       mExecutor.turnOn();    }  }  //关机命令CommandOffpublic class CommandOff implements Command {    private Executor mExecutor;    public CommandOff(Executor executor) {       mExecutor= executor;    }    public void execute() {       mExecutor.turnOff();    }  }//频道切换命令CommandChange  public class CommandChange implements Command {    private Executor mExecutor;    private int channel;    public CommandChange(Executor executor, int channel) {       mExecutor = executor;       this.channel = channel;    }    public void execute() {       mExecutor.changeChannel(channel);    }  }  
//Executor执行者 public class Executor {    public int currentChannel = 0;    public void turnOn() {       System.out.println("The televisino is on.");    }    public void turnOff() {       System.out.println("The television is off.");    }    public void changeChannel(int channel) {       this.currentChannel = channel;       System.out.println("Now TV channel is " + channel);    }  } 
//Controller控制者public class Control {    private Command onCommand, offCommand, changeChannel;    public Control(Command on, Command off, Command channel) {       onCommand = on;       offCommand = off;       changeChannel = channel;    }    public void turnOn() {       onCommand.execute();    }    public void turnOff() {       offCommand.execute();    }    public void changeChannel() {       changeChannel.execute();    }  } 
//测试类Client  public class Client {    public static void main(String[] args) {       // Executor执行者     Executor executor = new Executor();       // 开机命令CommondOn       CommandOn on = new CommandOn(executor);       // 关机命令CommondOff     CommandOff off = new CommandOff(executor);       // 频道切换命令CommondChange     CommandChange channel = new CommandChange(executor, 2);       // Controller控制者       Control control = new Control(on, off, channel);       // 开机       control.turnOn();       // 切换频道       control.changeChannel();       // 关机       control.turnOff();    }  }  
//执行结果The televisino is on.  Now TV channel is 2  The television is off.  

3、总结

(1)命令模式的本质是对命令进行封装,将发出命令的责任和执行命令的责任分割开。

(2)每一个命令都是一个操作:请求的一方发出请求,要求执行一个操作;接收的一方收到请求,并执行操作。

(3)命令模式允许请求的一方和接收的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否被执行、何时被执行,以及是怎么被执行的。

(4)命令模式使请求本身成为一个对象,这个对象和其他对象一样可以被存储和传递。

(5)命令模式的关键在于引入了抽象命令接口,且发送者针对抽象命令接口编程,只有实现了抽象命令接口的具体命令才能与接收者相关联。

原创粉丝点击