用pattern替换else语句 [英] Replacing if else statement with pattern

查看:95
本文介绍了用pattern替换else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个if else语句可能会在不久的将来增长。

  public void decision(String someCondition){

if(someCondition.equals(conditionOne ){
//
someMethod(someParameter);

} else if(someCondition.equals(conditionTwo)){

//
someMethod(anotherParameter);

}


else {

someMethod(elseParameter);

}
}

既然,这已经看起来很乱我认为如果我可以在这里应用任何设计模式会更好。我研究了战略模式,但是我不知道这是否会在这里减少否则条件。任何建议?

解决方案

这是一个经典的替换条件调度程序.htmlrel =noreferrer>





基本上你做一个命令对象,您的旧if / else组中的每个代码块,然后制作一个命令的地图,其中的键是您的条件字符串

  interface Handler {
void handle(myObject o);
}


Map< String,Handler> commandMap = new HashMap<>();
//可以自由地将它们归因于自己的类或
//如果使用Java 8使用新的Lambda语法
commandMap.put(conditionOne,new Handler(){
void handle(MyObject o){
//从MyObject获取所需的参数,并且做任务
}
});
...

然后代替你的if / else代码: p>

  commandMap.get(someCondition).handle(this); 

现在,如果您需要稍后添加新命令,只需添加到哈希值。



如果要处理默认情况,可以使用 Null Object 模式来处理条件不是的情况在地图中。

 处理程序defaultHandler = ... 

if(commandMap.containsKey(someCondition) ){
commandMap.get(someCondition).handle(this);
} else {
defaultHandler.handle(this);
}


I have a if else statement which might grow in the near future.

    public void decide(String someCondition){

        if(someCondition.equals("conditionOne")){
            //
            someMethod("someParameter");

        }else if(someCondition.equals("conditionTwo")){

           //
           someMethod("anotherParameter");

        }
        .
        .
        else{

            someMethod("elseParameter");

        }
}

Since, this is already looking messy, I think it would be better if I can apply any design patterns here. I looked into Strategy pattern but I am not sure if that will reduce if else condition here. Any suggestions?

解决方案

This is a classic Replace Condition dispatcher with Command in the Refactoring to Patterns book.

Basically you make a Command object for each of the blocks of code in your old if/else group and then make a Map of those commands where the keys are your condition Strings

interface Handler{
    void handle( myObject o);
}


 Map<String, Handler> commandMap = new HashMap<>();
 //feel free to factor these out to their own class or
 //if using Java 8 use the new Lambda syntax
 commandMap.put("conditionOne", new Handler(){
         void handle(MyObject o){
                //get desired parameters from MyObject and do stuff
          }
 });
 ...

Then instead of your if/else code it is instead:

 commandMap.get(someCondition).handle(this);

Now if you need to later add new commands, you just add to the hash.

If you want to handle a default case, you can use the Null Object pattern to handle the case where a condition isn't in the Map.

 Handler defaultHandler = ...

if(commandMap.containsKey(someCondition)){
    commandMap.get(someCondition).handle(this);
}else{
    defaultHandler.handle(this);
}

这篇关于用pattern替换else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆