6 java reflection design-patterns command-pattern
我有控制器根据命令名执行一些命令,取自url.重点是不使用if和switch子句.据我所知,只有两种方法可以做到 - 1)命令模式2)反射.
//Command pattern
class Controller{
private HashMap<String,Command> commands;
public void executeCommand(String commandName){
commands.get(commandName).execute();
}
...
}
//reflection
class Controller{
public void readCommand(){
....
}
public void executeCommand(String commandName){
this.getClass().getMethod(commandName+"Command").invoke(this);
}
...
}
Run Code Online (Sandbox Code Playgroud)
所以问题:
| 归档时间: |
|
| 查看次数: |
1007 次 |
| 最近记录: |