Java 避免多重if条件

Java 避免多重if条件,java,php,Java,Php,可能重复: 如何避免多重if条件?例如: Public void search(String a,b,c,d,e) String aTerm; If(aTerm. equalsIgnoreCase(a)){ call function a();} If(aTerm. equalsIgnoreCase(b)){ call function b();} If(aTerm. equalsIgnoreCase(b) and aTerm. equalsIgnoreCase(b)){ call func

可能重复:

如何避免多重if条件?例如:

Public void search(String a,b,c,d,e)
String aTerm;
If(aTerm. equalsIgnoreCase(a)){ call function a();}
If(aTerm. equalsIgnoreCase(b)){ call function b();}
If(aTerm. equalsIgnoreCase(b) and aTerm. equalsIgnoreCase(b)){ call function ab();}
…………………… and so on………………………….
现在,传递的参数的单个和多个组合中,哪一个包含“aTerm”?例如,输出可能如下所示:

1 - aTerm appears in "a" only
2 - aTerm appears in "a,c", and "e"
3 - aTerm appears in "d" and "e"
对于每个单独的或可能的组合,我想调用一个特定的函数。我写了很多if条件,但看起来很糟糕。例如:

Public void search(String a,b,c,d,e)
String aTerm;
If(aTerm. equalsIgnoreCase(a)){ call function a();}
If(aTerm. equalsIgnoreCase(b)){ call function b();}
If(aTerm. equalsIgnoreCase(b) and aTerm. equalsIgnoreCase(b)){ call function ab();}
…………………… and so on………………………….

有没有更干净的方法?解决方案可以是PHP或Java。

构建一个字符串,并根据该字符串的名称调用该方法:

// Psuedo-code
str = "";
If( aTerm.equalsIgnoreCase(a)) str += "a";
If( aTerm.equalsIgnoreCase(b)) str += "b";
If( aTerm.equalsIgnoreCase(c)) str += "c";
If( aTerm.equalsIgnoreCase(d)) str += "d";
If( aTerm.equalsIgnoreCase(e)) str += "e";
call function named by str

多态性可以替代ifs/switch:

interface LogicWithMatcher {
    boolean match(String aTerm);
    void yourFunction();
}

class MatcherA implements LogicWithMatcher() {...}
class MatcherB implements LogicWithMatcher() {...}
class MatcherC implements LogicWithMatcher() {...}
class MatcherD implements LogicWithMatcher() {...}
class MatcherE implements LogicWithMatcher() {...}
如果必须将一个函数与给定输入匹配:

public LogicWithMatcher search(String yourString) {
    LogicWithMatcher[] logics = {new MatcherA(), new MatcherB ...}
    for (LogicWithMatcher logic : logics) {
        if (logic.match(yourString)) 
            return logic;
    return null;
}

String yourString = "....."
LogicWithMatcher logic = search(yourString);
if (logic != null) 
    logic.yourFunction();
else
    print("nothing matched");
或者,如果给定的输入可能与多个函数匹配:

public void runFunctionsFor(String yourString) {
    LogicWithMatcher[] logics = {new MatcherA(), new MatcherB ...}
    for (LogicWithMatcher logic : logics) {
        if (logic.match(yourString)) 
            logic.yourFunction();
}

String yourString = "....."
runFunctionsFor(yourString);

我可能会这样做:

public class YourClass {
    static Runnable[] targets = new Runnable[32];

    public void search(String a, String b, String c, String d, String e) {
        int tgt = 0;
        if(a.indexOf(aTerm) >= 0) tgt |= 1;
        if(b.indexOf(aTerm) >= 0) tgt |= 2;
        if(c.indexOf(aTerm) >= 0) tgt |= 4;
        if(d.indexOf(aTerm) >= 0) tgt |= 8;
        if(e.indexOf(aTerm) >= 0) tgt |= 16;
        targets[tgt].run();
    }
}
然后,只需包装要在
Runnable
s中运行的各种函数,并将它们存储在
targets
中相应的索引中。我很确定这会比使用反射更好

有没有更干净的方法

我认为简单的答案是“不”

有多种方法可以在Java中对其进行编码,以避免显式的if/else语句链,但最终结果通常比原始代码更复杂。在我看来,“干净”代码应该意味着代码易于阅读


在您的示例中,您有一个固定的方法签名和一个固定的谓词结构。依我看,if/else链是Java中的自然解决方案。

取决于定义清理器,为什么这个问题用
PHP
标记?为什么不使用switch case?是的,循环和数组。我不明白你为什么要把这个问题标记为PHP。有这么多不同的组合和方法,不管解决方案如何,整个问题都很难闻。你能和我们分享一下让你做出这个设计决定的基本要求吗?我添加了PHP标记,因为我不局限于Java。或者使用..当它不匹配时,你可能需要处理。多重组合如何?以上所有条件仅适用于一个实例。例如:a、b和c中的aTerm?还是a、c和e中的aTerm?等等@user751637这就是
+=
的作用。如果
a
e
匹配,那么字符串将是
“ae”
我将忽略PHP标记。如果你想要多语言的答案/解决方案,可以问多个问题。。。首先检查问题是否已经回答。