[/code]用命令模式解释命令
原由:
近日要做个实现,对不同的信息内容前缀采用不同的处理方式。主要类似命令模式。例如信息内容是:Test1111,要得到的算方式是打印出:1111
由于信息内容前缀已经定,不过却有三四十个之多。
于是想起用命令模式来解决。
以下是小弟的实现,如各位有好的建议,欢迎拍砖。
Control.java
组装类:
[code="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 block
e.printStackTrace();
} catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e)
{
// TODO Auto-generated catch block
e.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;
}
@Override
public 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);
}
}
@Override
public String getHelp()
{
return null;
}
@Override
public 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;
}
@Override
public 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;
}
@Override
public 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;
}
@Override
public 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;
}
@Override
public 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");
}
}