应用场景:
将一个请求封装成一个对象,从而使你很容易的添加命令,命令队列,撤销,记录日志等操作。把请求者和处理者分开了。
这里一我们用过的MP3播放器来说明:
/**
* 命令的接受者
* @author ZhangSheng
*
*/
public class Mp3 {
public void play(){
System.out.println("播放");
}
public void stop(){
System.out.println("停止");
}
public void rePaly(){
System.out.println("回放");
}
}
---------------------------------------------------------------------------
/**
* 抽象命令,命令的抽象对象
* @author ZhangSheng
*
*/
public interface Command {
public void execute();
}
------------------------------------------------------------------------------
/**
* 播放命令
* @author ZhangSheng
*
*/
public class PlayCommand implements Command {
private Mp3 mp3;
public PlayCommand(Mp3 mp3) {
super();
this.mp3 = mp3;
}
@Override
public void execute() {
mp3.play();
}
}
-------------------------------------------------------------------------------------
/**
* 回放命令
* @author ZhangSheng
*
*/
public class RePlayCommand implements Command {
private Mp3 mp3;
public RePlayCommand(Mp3 mp3) {
super();
this.mp3 = mp3;
}
@Override
public void execute() {
mp3.rePaly();
}
}
-------------------------------------------------------------------------------------------
/**
* 停止命令
* @author ZhangSheng
*
*/
public class StopCommand implements Command {
private Mp3 mp3;
public StopCommand(Mp3 mp3) {
super();
this.mp3 = mp3;
}
@Override
public void execute() {
mp3.stop();
}
}
----------------------------------------------------------------------------------------------------
/**
* 命令管理者,命令发起者
* @author ZhangSheng
*
*/
public class CommandManager {
private Command command;
//宏命令
private List<Command> list = new ArrayList<Command>();
//执行宏命令
public void executeAll(){
if(list.size()!=0){
System.out.println("执行宏命令");
for (Command command : list) {
command.execute();
}
}else{
System.out.println("还未加入任何命令");
}
}
public void addCommand(Command command){
if(command!=null){
list.add(command);
}else{
System.out.println("加入的命令不能为空");
}
}
public void removeCommand(Command command){
if(command!=null){
list.remove(command);
}else{
System.out.println("移除的命令不能为空");
}
}
//执行单一命令
public void execute(){
if(command!=null){
System.out.println("执行单一命令");
command.execute();
}else{
System.out.println("还未初始化单一命令");
}
}
public void setCommand(Command command) {
this.command = command;
}
private static CommandManager manager = new CommandManager();
public static CommandManager getInstance(){
return manager;
}
}
----------------------------------------------------------------------------------------------------------------
public class Test {
/**
* 测试类
*/
public static void main(String[] args) {
//命令管理者,发起者,请求者
CommandManager commandManager = CommandManager.getInstance();
//命令接受者
Mp3 mp3 = new Mp3();
//具体的命令
PlayCommand playCommand = new PlayCommand(mp3);
StopCommand stopCommand = new StopCommand(mp3);
RePlayCommand rePlayCommand = new RePlayCommand(mp3);
//请求单一播放命令
commandManager.setCommand(playCommand);
commandManager.execute();
//请求宏命令
commandManager.addCommand(stopCommand);
commandManager.addCommand(rePlayCommand);
commandManager.addCommand(stopCommand);
commandManager.addCommand(playCommand);
commandManager.executeAll();
commandManager.removeCommand(stopCommand);
commandManager.executeAll();
}
}