替代if-else语句或更好的方法

时间:2018-05-15 17:27:38

标签: java if-statement conditional conditional-statements

UI上有4个不同的用户输入字段,如Multiselect Dropdown。用户可以提供输入字段的不同组合以缩小结果集。 (从多选选项中选择所有/几个选项)。

示例 2多选选项

多选选项A包含元素{" 10xyz1"," 11xyz1",...},

多选选项B包含元素{" xyz101"," abc111",...}

2 ^ 2可能的输入组合(提示:二进制真值表):

1)从Multiselect选项A&&中选择所有元素。从Multiselect选项B中选择所有元素,

2)从Multiselect选项A&&中选择所有元素。从Multiselect Option B中选择几个元素,

3)从Multiselect选项A&&和从Multiselect选项B中选择所有元素,

4)从Multiselect选项A&& amp;从Multiselect Option B中选择几个元素

注意:必须从任一选项中选择至少一个元素

我使用了if else语句来实现我的逻辑(我为每个可能的选择分别设置了mongo集合,4个字段,16种可能的组合,所以16个mongo集合),但问题是代码看起来非常丑陋很多如果在UI中添加了另外一个字段,那么if-else语句就会结束,那么我必须检查添加其他条件语句。

目前条件if-else语句的数量是2 ^ N(其中N是Multiselect下拉输入字段的数量)。

代码段:

    private void constructQuery(MongoQuEry query, AnalysisFilter filter)
    {
    query.addFilterField("_id.operator", MongoQuEry.OP_EQUALS, "alpha");

    query.addFilterField("_id.month_year", MongoQuEry.OP_IN, getListOfMonthYear(filter));


    String collectionName;
    if (filter.getAllMarket() && filter.getAllCmts() && filter.getAllNode() && filter.getAllPackage()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR.toString();
    } else if (filter.getAllMarket() && filter.getAllCmts() && filter.getAllNode()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_PACKAGE.toString();
        query.addFilterField("_id.package", MongoQuEry.OP_IN, filter.getSubscriberPackage());
    } else if (filter.getAllMarket() && filter.getAllCmts() && filter.getAllPackage()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_NODE.toString();
        query.addFilterField("_id.node", MongoQuEry.OP_IN, filter.getNode());
    } else if (filter.getAllMarket() && filter.getAllCmts()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_NODE_PACKAGE.toString();
        query.addFilterField("_id.node", MongoQuEry.OP_IN, filter.getNode());
        query.addFilterField("_id.package", MongoQuEry.OP_IN, filter.getSubscriberPackage());
    } else if (filter.getAllMarket() && filter.getAllNode() && filter.getAllPackage()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_CMTS.toString();
        query.addFilterField("_id.cmts", MongoQuEry.OP_IN, filter.getCmts());
    } else if (filter.getAllMarket() && filter.getAllNode()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_CMTS_PACKAGE.toString();
        query.addFilterField("_id.cmts", MongoQuEry.OP_IN, filter.getCmts());
        query.addFilterField("_id.package", MongoQuEry.OP_IN, filter.getSubscriberPackage());
    } else if (filter.getAllMarket() && filter.getAllPackage()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_CMTS_NODE.toString();
        query.addFilterField("_id.cmts", MongoQuEry.OP_IN, filter.getCmts());
        query.addFilterField("_id.node", MongoQuEry.OP_IN, filter.getNode());
    } else if (filter.getAllMarket()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_CMTS_NODE_PACKAGE.toString();
        query.addFilterField("_id.cmts", MongoQuEry.OP_IN, filter.getCmts());
        query.addFilterField("_id.node", MongoQuEry.OP_IN, filter.getNode());
        query.addFilterField("_id.package", MongoQuEry.OP_IN, filter.getSubscriberPackage());
    } else if (filter.getAllCmts() && filter.getAllNode() && filter.getAllPackage()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_MARKET.toString();
        query.addFilterField("_id.market", MongoQuEry.OP_IN, filter.getMarket());
    } else if (filter.getAllCmts() && filter.getAllNode()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_MARKET_PACKAGE.toString();
        query.addFilterField("_id.market", MongoQuEry.OP_IN, filter.getMarket());
        query.addFilterField("_id.package", MongoQuEry.OP_IN, filter.getSubscriberPackage());
    } else if (filter.getAllCmts() && filter.getAllPackage()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_MARKET_NODE.toString();
        query.addFilterField("_id.market", MongoQuEry.OP_IN, filter.getMarket());
        query.addFilterField("_id.node", MongoQuEry.OP_IN, filter.getNode());
    } else if (filter.getAllCmts()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_MARKET_NODE_PACKAGE.toString();
        query.addFilterField("_id.market", MongoQuEry.OP_IN, filter.getMarket());
        query.addFilterField("_id.node", MongoQuEry.OP_IN, filter.getNode());
        query.addFilterField("_id.package", MongoQuEry.OP_IN, filter.getSubscriberPackage());
    } else if (filter.getAllNode() && filter.getAllPackage()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_MARKET_CMTS.toString();
        query.addFilterField("_id.market", MongoQuEry.OP_IN, filter.getMarket());
        query.addFilterField("_id.cmts", MongoQuEry.OP_IN, filter.getCmts());
    } else if (filter.getAllNode()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_MARKET_CMTS_PACKAGE.toString();
        query.addFilterField("_id.market", MongoQuEry.OP_IN, filter.getMarket());
        query.addFilterField("_id.cmts", MongoQuEry.OP_IN, filter.getCmts());
        query.addFilterField("_id.package", MongoQuEry.OP_IN, filter.getSubscriberPackage());
    } else if (filter.getAllPackage()) {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_MARKET_CMTS_NODE.toString();
        query.addFilterField("_id.market", MongoQuEry.OP_IN, filter.getMarket());
        query.addFilterField("_id.cmts", MongoQuEry.OP_IN, filter.getCmts());
        query.addFilterField("_id.node", MongoQuEry.OP_IN, filter.getNode());
    } else {
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_MARKET_CMTS_NODE_PACKAGE.toString();
        query.addFilterField("_id.market", MongoQuEry.OP_IN, filter.getMarket());
        query.addFilterField("_id.cmts", MongoQuEry.OP_IN, filter.getCmts());
        query.addFilterField("_id.node", MongoQuEry.OP_IN, filter.getNode());
        query.addFilterField("_id.package", MongoQuEry.OP_IN, filter.getSubscriberPackage());
    }


    query.setCollection(collectionName);
    query.addGroupByFields("_id.operator", "_id.month_year");
    query.addAliasAggregateField("count", MongoQuEry.AGGR_SUM, "subscriber_count");

    int order = MongoQuEry.ORDER_ASC;
    query.addOrderByFields(order, "_id.month_year");

}

3 个答案:

答案 0 :(得分:1)

您可以通过对常见条件进行分组来创建决策树,例如,如果每次A为真,您都希望执行method1(),而不是在每个'上编写此行代码,如果&#39 ;如果A为真,你可以先检查A是否为真,然后执行这个公共代码块,然后检查其他条件,如:

而不是写作:

if(a && b) {
     example.method1();
     example.method2();
     // more code 1
} else if(a && !b) {
     example.method1();
     // more code 2
} else if(!a && b) {
     example.method2();
     // more code 3
} else if(!a && !b) {
     // more code 4
}
你可以写:

if(a) {
     example.method1();
     if(b) {
          example.method2();
          // more code 1
     } else {
          // more code 2
     }
} else {
     if(b) {
          example.method2();
          // more code 3
     } else {
          // more code 4
     }
}

答案 1 :(得分:1)

您的集合名称似乎有一个基于过滤器字段的模式,如果是这种方法可能有效。

Object marketFilter = filter.getAllMarket();
Object cmtsFilter = filter.getAllCmts();
Object packageFilter = filter.getAllPackage();
Object nodeFilter = filter.getAllNodes();


if(marketFilter != null){
    query.addFilterField("_id.market", MongoQuEry.OP_IN, filter.getMarket());
}
.
.
. upto filter N

String collectionName = getCollectionName(marketFilter, ... filter N)

/* 
 create a function getCollectionName(filter1, ... filterN)   
  in this function you can generate collection name by checking which 
  filters are not null.
*/

注意: - 只有当您的集合名称基于过滤器时,此方法才有效。

答案 2 :(得分:0)

switch caseIIf表达怎么样?

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator

switch case会更清晰,并将if-else内容分成多个函数:

void FunctionName()
{
        collectionName = CollectionNames.ME_USAGE_MONTH_YEAR_NODE_PACKAGE.toString();
        query.addFilterField("_id.node", MongoQuEry.OP_IN, filter.getNode());
        query.addFilterField("_id.package", MongoQuEry.OP_IN, 
        filter.getSubscriberPackage());
}