一个用命令驱动的java小程序

z3000 贡献于2015-05-30

作者 Administrator  创建于2015-05-28 23:51:00   修改者admin  修改于2015-05-28 23:52:00字数3178

文档摘要:一个用命令驱动的java小程序
关键词:

命令类: import java.lang.reflect.Method; public class Command { private String id; private T t; private String executeMethod; private boolean success; private M m; private String prior; private String next; private Class tc; private Class[] paramTypes; public Command(String id, T t, String executeMethod) { this.id=id; this.t = t; this.executeMethod = executeMethod; this.tc = t.getClass(); //获取成员方法 Method[] methods = tc.getDeclaredMethods(); for (Method method: methods) { if(method.getName().equals(executeMethod)){ this.paramTypes = method.getParameterTypes(); } } } @SuppressWarnings("unchecked") public M execute(Object...param) throws Exception{ if(paramTypes!=null){ Method method = tc.getDeclaredMethod(executeMethod, paramTypes); method.setAccessible(true); this.m=(M) method.invoke(t, param); if(m!=null){ this.success=true; } return this.m; } return null; } public String getId() { return id; } public void setId(String id) { this.id = id; } public T getT() { return t; } public void setT(T t) { this.t = t; } public String getExecuteMethod() { return executeMethod; } public void setExecuteMethod(String executeMethod) { this.executeMethod = executeMethod; } public Boolean getSuccess() { return success; } public M getM() { return m; } public String getPrior() { return prior; } public void setPrior(String prior) { this.prior = prior; } public String getNext() { return next; } public void setNext(String next) { this.next = next; } @Override public String toString() { return "Command [id=" + id + ", t=" + t + ", executeMethod=" + executeMethod + ", success=" + success + ", m=" + m + ", prior=" + prior + ", next=" + next + "]"; } } 配置类: import java.util.HashMap; import java.util.Map; public class OperationConfig { public Map getCommands(String operation){ Map map=null; if ("operation_a".equals(operation)) { map=new HashMap(); ComputeService computeService=new ComputeService(); Command mul=new Command("mul", computeService, "mul"); mul.setNext("min"); map.put(mul.getId(), mul); Command min=new Command("min", computeService, "min"); min.setPrior("mul"); min.setNext("add"); map.put(min.getId(),min); Command add=new Command("add", computeService, "add"); add.setPrior("min"); map.put(add.getId(),add); } return map; } } 事务类: public class ComputeService { private Double add(Double a,Double b){ return (a+b); } private Double min(Double a,Double b){ return (a-b); } private Double mul(Double a,Double b){ return (a*b); } private Double div(Double a,Double b){ return (a/b); } } 执行类: import java.util.Map; import java.util.Set; public class Test { private static OperationConfig operationConfig=new OperationConfig(); public static String run(double a,double b,double c,double d) throws Exception{ //operation_a:a-b*c+d Map runCommands=operationConfig.getCommands("operation_a"); if(runCommands==null){ return ""; } Set ksySet=runCommands.keySet(); boolean isok=false; do{ isok=false; for (String string : ksySet) { Command runCommand=runCommands.get(string); if(!isok&&!runCommand.getSuccess()){ isok=true; } if(runCommand.getSuccess()){ continue; } String runCommandPrior = runCommand.getPrior(); if(null==runCommandPrior){ runCommand.execute(a,c); System.out.println(runCommand); continue; } Command runCommandPriorCommand=runCommands.get(runCommandPrior); if(!runCommandPriorCommand.getSuccess()){ continue; } runCommand.execute(b,c); System.out.println(runCommand); } }while(isok); return null; } public static void main(String[] args) throws Exception { run(1,2,3,4); } }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档