命令模式把一个请求或者操作封装到一个对象中。他把发出命令的责任和执行命令的责任分割开,委派给不同的对象
每一个命令都是一个操作:请求的一方发出请求要求执行一个操作;接收的一方收到请求,并执行操作。命令模式允许请求的一方和接收的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否被执行、何时被执行,以及是怎么被执行的
/**
* 请求者角色
*/
public class Keypad {
private Command playCmd;
private Command rewindCmd;
private Command stopCmd;
public Keypad(Command playCmd, Command rewindCmd, Command stopCmd) {
this.playCmd = playCmd;
this.rewindCmd = rewindCmd;
this.stopCmd = stopCmd;
}
/**
* 行动方法
*/
public void play() {
playCmd.execute();
}
/**
* 行动方法
*/
public void stop() {
stopCmd.execute();
}
/**
* 行动方法
*/
public void rewind() {
rewindCmd.execute();
}
}
/**
* 抽象命令角色
*/
public interface Command {
/**执行方法*/
public void execute();
}
/**
* 具体命令角色
*/
public class PlayCommand implements Command {
private AudioPlayer audioPlayer;
public PlayCommand(AudioPlayer audioPlayer) {
this.audioPlayer = audioPlayer;
}
public void execute() {
audioPlayer.play();
}
}
/**
* 具体命令角色
*/
public class RewindCommand implements Command {
private AudioPlayer audioPlayer;
public RewindCommand(AudioPlayer audioPlayer) {
this.audioPlayer = audioPlayer;
}
public void execute() {
audioPlayer.rewind();
}
}
/**
* 具体命令角色
*/
public class StopCommand implements Command {
private AudioPlayer audioPlayer;
public StopCommand(AudioPlayer audioPlayer) {
this.audioPlayer = audioPlayer;
}
public void execute() {
audioPlayer.stop();
}
}
/**
* 接受者角色
*/
public class AudioPlayer {
/**行动方法*/
public void play() {
System.out.println("Playing ... ");
}
/**行动方法*/
public void rewind() {
System.out.println("Rewinding ... ");
}
/**行动方法*/
public void stop() {
System.out.println("Stopped ... ");
}
}
public interface MacroCommand extends Command {
/**
* 执行方法
*/
/**
public void execute();
* 聚集管理方法,可以删除一个成员命令
*/
public void remove(Command toRemove);
/**
* 聚集管理方法,可以添加一个成员命令
*/
public void add(Command toAdd);
}
import java.util.Vector;
public class MacroAudioCommand implements MacroCommand {
private Vector<Command> commandVector = new Vector<Command>();
public void add(Command toAdd) {
commandVector.addElement(toAdd);
}
public void remove(Command toRemove) {
commandVector.removeElement(toRemove);
}
public void execute() {
System.out.println("Automated playback of stored commands...");
for (Command com : commandVector) {
com.execute();
}
System.out.println("Finished automated playback of stored commands.");
}
}
public class Client {
public static void main(String[] args) {
test2();
}
public static void test1(){
AudioPlayer audioPlayer = new AudioPlayer();
Command playCommand = new PlayCommand(audioPlayer);
Command rewindCommand = new RewindCommand(audioPlayer);
Command stopCommand = new StopCommand(audioPlayer);
Keypad keyPad = new Keypad(playCommand, rewindCommand, stopCommand);
keyPad.play();
keyPad.rewind();
keyPad.stop();
keyPad.play();
keyPad.stop();
}
public static void test2(){
AudioPlayer audioPlayer = new AudioPlayer();
Command playCommand = new PlayCommand(audioPlayer);
Command rewindCommand = new RewindCommand(audioPlayer);
Command stopCommand = new StopCommand(audioPlayer);
MacroCommand macro = new MacroAudioCommand();
macro.add(playCommand);
macro.add(rewindCommand);
macro.add(stopCommand);
macro.add(playCommand);
macro.add(stopCommand);
macro.execute();
}
}