简体   繁体   English

多个if语句的设计方法?

[英]Design approach for multiple if else statement?

I have method which returns sql query as string. 我有将sql查询返回为字符串的方法。 This method consider parameter 'level' , based on this parameter there are multiple if else statement like 此方法考虑参数'level',基于此参数有多个if else语句,例如

   if(level.equals("A1")){
    // add some  field in query
   }
   else if (level.equals("A2"))
   {
    // add some  field  and logic in query
   }
   ..

   so on 

In future number of levels are going to increase and I don`t want to write pesky if else statements so I am looking for cleaner and maintainable design approach for this scenario. 将来级别的数量将增加,如果不想使用其他语句,我不想​​写讨厌的东西,因此我正在寻找一种更干净,可维护的设计方案。 I am thinking of strategy design pattern but not sure whether it is best for this scenario. 我正在考虑策略设计模式,但不确定是否适合这种情况。

You have several options: 您有几种选择:

  1. if / else if as you've indicated. if / else if如您所指示。

  2. A switch statement . 一个switch语句

  3. A Map with the keys being the level string and the values being references of a functional interface type ( Runnable or an appropriate interface from java.util.function , or your own) you can initialize with method references , lambdas , anonymous class instances, or even concrete class instances as required. 可以使用方法引用lambdas ,匿名类实例或java.util.function初始化的Map ,其键为级别字符串,值为功能接口类型( Runnablejava.util.function的适当接口,或您自己的接口)的引用甚至需要的具体类实例。

Here's an example of that last option, using the most basic functional interface, Runnable , with method references: 这是最后一个选项的示例,该示例使用最基本的功能接口Runnable以及方法引用:

import java.util.Map;
import java.util.HashMap;

public class Example {
    private Map<String, Runnable> handlers;

    public Example() {
        this.handlers = new HashMap<String, Runnable>();
        this.handlers.put("A1", this::a1action);
        this.handlers.put("A2", this::a2action);
    }

    private void a1action() {
        System.out.println("Action A1");
    }

    private void a2action() {
        System.out.println("Action A2");
    }

    public void handleAction(String action) {
        Runnable handler = this.handlers.get(action);
        if (handler == null) {
            System.out.println("No handler for '" + action + "'");
        } else {
            handler.run();
        }
    }

    public static void main(String[] args) throws Exception {
        Example ex = new Example();
        ex.handleAction("A1");
        ex.handleAction("A2");
        ex.handleAction("A3");
    }
}

You can use an enum to list the operations and use valueOf to work out which to use. 您可以使用enum列出操作,并使用valueOf确定要使用的操作。

enum Levels {
    A1 {
        @Override
        void doSomethingToTheQuery(Query q) {
            // add some  field in query
        }
    },
    A2{
        @Override
        void doSomethingToTheQuery(Query q) {
            // add some  field  and logic in query
        }
    };

    abstract void doSomethingToTheQuery(Query q);
}

public void test() {
    // Lookup the level and do hack the query.
    Levels.valueOf("A1").doSomethingToTheQuery(q);
}

You can then add new levels just by adding to the enum . 然后,只需添加到enum即可添加新级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM