如何避免Java中的else if条件

时间:2018-07-02 16:19:28

标签: java

我的代码看起来像

public void convertValues(String sourceValue,String targetValue) {
    if (sourceValue.equals(targetValue)) {
         //dosomething 
    } else if (sourceValue.equals(value1) && targetValue.equals(value2)) {
        //dosomething
    } else if (sourceValue.equals(value2) && targetValue.equals(value1)) {
        //dosomething 
    }
}

就像我有40个条件一样,如果其他条件是愚蠢的,我会写那些条件。

使用枚举或哈希表是一种好方法吗?

2 个答案:

答案 0 :(得分:0)

要加快输入意大利面条代码的速度,您可能需要准备一个帮助程序类,以简化添加重复的行的过程。

如果您不喜欢使用else if,请使用return;(注意:势利的人会过敏地看到return并未出现在结尾处)。

使用助手类/方法检查值。

示例:

public class Main {
String value1 = "a";
String value2 = "b";
String value3 = "c";
String value4 = "d";

// use a nested private class where you could add all long 
// and repetitive methods that need to be done
// with source and target

private class Helper {
    private String src;
    private String tgt;
    Helper(String src, String tgt) {
        this.src = src;
        this.tgt = tgt;
    }
    public boolean eq(String val, String val1) {
        return src.equals(val) && tgt.equals(val1);
    }
    // add other repetitive functions here
}


public void convertValues(String sourceValue, String targetValue) {

    // use a simple name for the helper class.
    Helper $ = new Helper(sourceValue, targetValue);

    if (sourceValue.equals(targetValue)) {
        // put your execution lines into a separate method.
        // this way, it will become reusable and much easier to maintain
        // call a method1
        return;
    }

    if ($.eq(value1, value2)) {
        // call a method2
        return;
    }

    if ($.eq(value2, value1)) {
        // call a method3
        return;
    }

    if ($.eq(value1, value3)) {
        // call a method4
        return;
    }
    // ....
}


// public void method1()
// public void method2()

public static void main(String[] args) throws java.lang.Exception {
   // Everything else

}
}

答案 1 :(得分:0)

在Java 8中使用lambda表达式,可以像这样实现:

定义值类别:

public class Value {

    private final String value1;
    private final String value2;
    // Function to exetute
    private final BiConsumer<String, String> f;

    public Value(String value1, String value2, BiConsumer<String, String> f) {
        super();
        this.value1 = value1;
        this.value2 = value2;
        this.f = f;
    }

    public boolean execute(String src, String tgt) {
        if(src.equals(value1) && tgt.equals(value2)) {
            f.accept(src, tgt);
            return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((value1 == null) ? 0 : value1.hashCode());
        result = prime * result + ((value2 == null) ? 0 : value2.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Value other = (Value) obj;
        if (value1 == null) {
            if (other.value1 != null)
                return false;
        } else if (!value1.equals(other.value1))
            return false;
        if (value2 == null) {
            if (other.value2 != null)
                return false;
        } else if (!value2.equals(other.value2))
            return false;
        return true;
    }           
}

//初始化值

Set<Value> values = new HashSet<>();
values.add(new Value("value1", "value2", (src, tgt) -> {/* do something here */}));
values.add(new Value("value2", "value1", (src, tgt) -> {/* do something here */}));
values.add(new Value("value4", "value5", (src, tgt) -> {/* do something here */}));

//执行

if (sourceValue.equals(targetValue)) {
     //dosomething 
} else {
    for(Value v:values) {
        if(v.execute(src, tgt)) {
             break;
        }
    }
}