命令模式,解释命令
用命令模式解释命令原由:
近日要做个实现,对不同的信息内容前缀采用不同的处理方式。主要类似命令模式。例如信息内容是:Test1111,要得到的算方式是打印出:1111
由于信息内容前缀已经定,不过却有三四十个之多。
于是想起用命令模式来解决。
以下是小弟的实现,如各位有好的建议,欢迎拍砖。
Control.java
组装类:
package command.control;import java.lang.reflect.InvocationTargetException;import command.Command;public class Control{private Command command;public Control(Command command){this.command = command;}public void execute(String message){try{command.execute(message);} catch (IllegalArgumentException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e){// TODO Auto-generated catch blocke.printStackTrace();}}}
Command.java
命令接口类:
package command;import java.lang.reflect.InvocationTargetException;public interface Command{// 命令解释public void execute(String message) throws IllegalArgumentException,IllegalAccessException, InvocationTargetException;// 命令出错时返回帮助public String getHelp();// 添加下一个命令执行器public void addCommand(Command command);}
Actuator.java
命令执行器:
package command.actuator;import command.Command;public class Actuator{public void executeAAA(String message, Command command){System.out.println("命令内容为:" + message + ",解释命令器为:" + command.getClass()+ ",命令帮助:" + command.getHelp());}public void executeBBBB(String message, Command command){System.out.println("命令内容为:" + message + ",解释命令器为:" + command.getClass()+ ",命令帮助:" + command.getHelp());}public void executeTest(String message, Command command){System.out.println("命令内容为:" + message + ",解释命令器为:" + command.getClass()+ ",命令帮助:" + command.getHelp());}public void executeComeon(String message, Command command){System.out.println("命令内容为:" + message + ",解释命令器为:" + command.getClass()+ ",命令帮助:" + command.getHelp());}}
CommandImpl.java
命令实现类:
package command.impl;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import command.Command;import command.actuator.Actuator;public class CommandImpl implements Command{protected String command = ""; // 此命令执行器所要执行的命令的 总写protected String commandString = ""; // 此命令执行器所要执行的命令 ,多个时以逗号分开private Command nextCommand;// 下一个命令执行器private String methodPre = "execute";// 执行器方法前缀private Actuator actuator;// 执行器public CommandImpl(Actuator actuator){this.actuator = actuator;}@Overridepublic void execute(String message) throws IllegalArgumentException,IllegalAccessException, InvocationTargetException{if (!"".equals(commandString) && message != null && !"".equals(message)){String[] arrCom = null;if (commandString.indexOf(",") != -1){arrCom = commandString.split(",");} else{arrCom = new String[]{ commandString };}for (String strCom : arrCom){if (message.length() >= strCom.length()&& strCom.equalsIgnoreCase(message.substring(0, strCom.length()))){Method[] methods = actuator.getClass().getMethods();for (Method method : methods){if ((methodPre + command).equalsIgnoreCase(method.getName())){method.invoke(actuator, message, this);return;}}}}// 命令未被执行,调用下一个执行器if (nextCommand != null)nextCommand.execute(message);}}@Overridepublic String getHelp(){return null;}@Overridepublic void addCommand(Command command){this.nextCommand = command;}}
CommandAAA.java
AAA命令解释器:
package command.impl;import command.actuator.Actuator;public class CommandAAA extends CommandImpl{protected String command = "AAA"; // 此命令执行器所要执行的命令的 总写protected String commandString = "aaa,AAA,12,ttb"; // 此命令执行器所要执行的命令,多个时以逗号分开public CommandAAA(Actuator actuator){super(actuator);super.command = this.command;super.commandString = this.commandString;}@Overridepublic String getHelp(){return "命令格式为:"+command;}}
CommandBBBB.java
BBBB命令执行器:
package command.impl;import command.actuator.Actuator;public class CommandBBBB extends CommandImpl{protected String command = "BBBB"; // 此命令执行器所要执行的命令的 总写protected String commandString = "BBBB,11"; // 此命令执行器所要执行的命令 ,多个时以逗号分开public CommandBBBB(Actuator actuator){super(actuator);super.command = this.command;super.commandString = this.commandString;}@Overridepublic String getHelp(){return "命令格式为:" + command;}}
CommandComeon.java
Comeon命令执行器:
package command.impl;import command.actuator.Actuator;public class CommandComeon extends CommandImpl{protected String command = "ComeOn"; // 此命令执行器所要执行的命令的 总写protected String commandString = "comeon,ComeOn,goon"; // 此命令执行器所要执行的命令// ,多个时以逗号分开public CommandComeon(Actuator actuator){super(actuator);super.command = this.command;super.commandString = this.commandString;}@Overridepublic String getHelp(){return "命令格式为:" + command;}}
CommandTest.java
Test命令执行器:
package command.impl;import command.actuator.Actuator;public class CommandTest extends CommandImpl{protected String command = "Test"; // 此命令执行器所要执行的命令的 总写protected String commandString = "Test,12"; // 此命令执行器所要执行的命令// ,多个时以逗号分开public CommandTest(Actuator actuator){super(actuator);super.command = this.command;super.commandString = this.commandString;}@Overridepublic String getHelp(){return "命令格式为:" + command;}}
测试类:
package command;
import command.actuator.Actuator;
import command.control.Control;
import command.impl.CommandAAA;
import command.impl.CommandBBBB;
import command.impl.CommandComeon;
import command.impl.CommandTest;
public class Test
{
/**
* @param args
*/
public static void main(String[] args)
{
Actuator actuator = new Actuator();
Command comeon = new CommandComeon(actuator);
Command bbbb = new CommandBBBB(actuator);
Command test = new CommandTest(actuator);
Command aaa = new CommandAAA(actuator);
comeon.addCommand(bbbb);
bbbb.addCommand(test);
test.addCommand(aaa);//可以随意去除命令
Control control = new Control(comeon);
control.execute("AAACCAFE");
}
}
页:
[1]