命令模式很好理解,举个例子,司令员下令让士兵去干件事情,从整个事情的角度来考虑,司令员的作用是,发出口令,口令经过传递,传到了士兵耳朵里,士兵去执行。这个过程好在,三者相互解耦,任何一方都不用去依赖其他人,只需要做好自己的事儿就行,司令员要的是结果,不会去关注到底士兵是怎么实现的。我们看看关系图:
Invoker是调用者(司令员),Receiver是被调用者(士兵),MyCommand是命令,实现了Command接口,持有接收对象,看实现代码:
[java]
view plain
copy
- public interface Command {
- public void exe();
- }
[java]
view plain
copy
- public class MyCommand implements Command {
- private Receiver receiver;
- public MyCommand(Receiver receiver) {
- this.receiver = receiver;
- }
- @Override
- public void exe() {
- receiver.action();
- }
- }
[java]
view plain
copy
- public class Receiver {
- public void action(){
- System.out.println("command received!");
- }
- }
[java]
view plain
copy
- public class Invoker {
- private Command command;
- public Invoker(Command command) {
- this.command = command;
- }
- public void action(){
- command.exe();
- }
- }
[java]
view plain
copy
- public class Test {
- public static void main(String[] args) {
- Receiver receiver = new Receiver();
- Command cmd = new MyCommand(receiver);
- Invoker invoker = new Invoker(cmd);
- invoker.action();
- }
- }
输出:command received!
=========================================================
下面是command的另外一个例子:
public class test {
public static void main(String[] args) {
Player player = new Player();
TurnOnCommand on = new TurnOnCommand(player);
TurnOffCommand off = new TurnOffCommand(player);
NextCommand next = new NextCommand(player);
PlayerInvoker invoker = new PlayerInvoker(on, off, next);
invoker.turnOn();
invoker.turnOff();
invoker.next();
}
}
class Player {
public void turnOn() {
System.out.println("打开");
}
public void turnOff() {
System.out.println("关闭");
}
public void next() {
System.out.println("下一曲");
}
}
interface Command {
public void execute();
}
class TurnOnCommand implements Command {
private Player player;
public TurnOnCommand(Player player) {
this.player = player;
}
public void execute() {
this.player.turnOn();
}
}
class TurnOffCommand implements Command {
private Player player;
public TurnOffCommand(Player player) {
this.player = player;
}
public void execute() {
this.player.turnOff();
}
}
// 下一曲命令类
class NextCommand implements Command {
private Player player;
public NextCommand(Player player) {
this.player = player;
}
public void execute() {
this.player.next();
}
}
class PlayerInvoker {
private TurnOnCommand on;
private TurnOffCommand off;
private NextCommand next;
public PlayerInvoker(TurnOnCommand on, TurnOffCommand off, NextCommand next) {
this.on = on;
this.off = off;
this.next = next;
}
public void turnOn() {
this.on.execute();
}
public void turnOff() {
this.off.execute();
}
public void next() {
this.next.execute();
}
}
======================================================
2、
3.代码
public interface Command { public void execute(); } public class ConcreteCommand implements Command { private Receiver receiver = null; private String state; public ConcreteCommand(Receiver receiver){ this.receiver = receiver; } public void execute() { receiver.action(); } } public class Receiver { public void action(){ //真正执行命令操作的功能代码 } } public class Invoker { private Command command = null; public void setCommand(Command command) { this.command = command; } public void runCommand() { command.execute(); } } public class Client { public void assemble(){ //创建接收者 Receiver receiver = new Receiver(); //创建命令对象,设定它的接收者 Command command = new ConcreteCommand(receiver); //创建Invoker,把命令对象设置进去 Invoker invoker = new Invoker(); invoker.setCommand(command); } }
下面给个例子,是模拟对电视机的操作有开机、关机、换台命令。代码如下 //命令接收者 public class Tv { 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); } } //执行命令的接口 public interface Command { void execute(); } //开机命令 public class CommandOn implements Command { private Tv myTv; public CommandOn(Tv tv) { myTv = tv; } public void execute() { myTv.turnOn(); } } //关机命令 public class CommandOff implements Command { private Tv myTv; public CommandOff(Tv tv) { myTv = tv; } public void execute() { myTv.turnOff(); } } //频道切换命令 public class CommandChange implements Command { private Tv myTv; private int channel; public CommandChange(Tv tv, int channel) { myTv = tv; this.channel = channel; } public void execute() { myTv.changeChannel(channel); } } //可以看作是遥控器吧 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(); } } //测试类 public class Client { public static void main(String[] args) { // 命令接收者 Tv myTv = new Tv(); // 开机命令 CommandOn on = new CommandOn(myTv); // 关机命令 CommandOff off = new CommandOff(myTv); // 频道切换命令 CommandChange channel = new CommandChange(myTv, 2); // 命令控制对象 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.
4.应用场景