命令模式: 命令模式,我的理解就是把一个命令封装成一个类,看例子把。
*************************************** 开始 缺点: if语句繁多,代码太长,不容易维护 ***************************************
public class CommandDemo{ public static void main(String args[]){ String commandName = args[0]; process(commandName); }
void process(String commandName){ if(commandName.equals("a")){ //do a1 //do a2 //do a3 } if(commandName.equals("b")){ //do b1 //do b2 //do b3 //do b4 //do b5 } if(commandName.equals("c")){ //do c1 //do c2 //do c3 //do c4 //do c5 //do c6 //do c7 //do c8 //do c9 } } }
*************************************** 改进一: 改进后的优点: 每个if分支交给了一个command类处理,清晰多了 改进后仍然的缺点: 还是一堆的if语句,看着就烦 *************************************** public class CommandDemo{
public static void main(String args[]){ String commandName = args[0]; if(commandName.equals("a")){ new CommandA().execute(); } if(commandName.equals("b")){ new CommandB().execute(); } if(commandName.equals("c")){ new CommandC().execute(); } }
}
//定义Command接口 inteface Command{ void execute(); }
class CommandA implements Command{ void execute(){ //do a1 //do a2 //do a3 } } class CommandB implements Command{ void execute(){ //do b1 //do b2 //do b3 //do b4 //do b5
} } class CommandC implements Command{ void execute(){ //do c1 //do c2 //do c3 //do c4 //do c5 //do c6 //do c7 //do c8 //do c9 } }
*************************************** 改进二: 改进后的优点: if分支少勒!!!清晰多了 改进后仍然的缺点: 还需要注册,恩,有点烦 *************************************** public class CommandDemo{ static Hashtable hashTable = new Hashtable(); static registCommand(String commandName,Commmand command){ if(hashTable.get(commandName)==null){ hashTable.add(commandName,command); } } static Command findCommand(String commandName){ return (Command)hashTable.get(commandName); }
public static void main(String args[]){ //注册 registCommand("a",new CommandA()); registCommand("b",new CommandB()); registCommand("c",new CommandC());
String commandName = args[0]; Command command = findCommand(commandName); command.execute(); } }
//定义Command接口 inteface Command{ void execute(); }
class CommandA implements Command{ void execute(){ //do a1 //do a2 //do a3 } } class CommandB implements Command{ void execute(){ //do b1 //do b2 //do b3 //do b4 //do b5
} } class CommandC implements Command{ void execute(){ //do c1 //do c2 //do c3 //do c4 //do c5 //do c6 //do c7 //do c8 //do c9 } }
*************************************** 改进三: 改进后的优点: // registCommand("a",new CommandA()); // registCommand("b",new CommandB()); // registCommand("c",new CommandC()); 这些东西不用写了
改进后仍然的缺点: 嗯,差不多了,再改就有点变态了 ***************************************
XML外部配置文件: <commands> <command name="a" class="CommandA"/> <command name="b" class="CommandB"/> <command name="c" class="CommandC"/> </commands>
public class CommandDemo{ static Hashtable hashTable = new Hashtable(); static loadXMLConfig(){ //load external XML file //动态加载所有的CommandX类 //放到hashTable中 } static Command findCommand(String commandName){ return (Command)hashTable.get(commandName); }
public static void main(String args[]){ //注册 // registCommand("a",new CommandA()); // registCommand("b",new CommandB()); // registCommand("c",new CommandC());
String commandName = args[0]; Command command = findCommand(commandName); command.execute(); }
void registerCommand(Command c){ v.add(c); } }
//定义Command接口 inteface Command{ void execute(); }
class CommandA implements Command{ void execute(){ //do a1 //do a2 //do a3 } } class CommandB implements Command{ void execute(){ //do b1 //do b2 //do b3 //do b4 //do b5
} } class CommandC implements Command{ void execute(){ //do c1 //do c2 //do c3 //do c4 //do c5 //do c6 //do c7 //do c8 //do c9 } }
//从cn_java上找来的!
|
|