简体   繁体   English

替代if else陈述或建议最佳方法

[英]Alternative to if else statements or suggest best approach

There are 3 different user input fields like Text box & Drop downs on the UI. 用户界面上有3个不同的用户输入字段,例如“文本框”和“下拉列表”。 The user can search a table with any of the three input fields or by providing different combinations of input fields to narrow the result set.I have used if else statements to implement my logic using these three fields, but the problem is the code looks very ugly with lot of if-else statements and more over if there is one more field added to the UI then I have to check for more combinations with if else statements. 用户可以使用三个输入字段中的任何一个来搜索表,或者通过提供不同的输入字段组合来缩小结果集。我已经使用if else语句使用这三个字段来实现我的逻辑,但是问题是代码看起来非常如果有很多if-else语句则很丑陋,而且如果在UI中添加了更多字段,那么就不得不检查if else语句的更多组合。 I really appreciate if someone throws light on how to follow best practices or better approach in these scenarios. 如果有人阐明在这些情况下如何遵循最佳做法或更好的方法,我将非常感激。

My Code: 我的代码:

      public boolean filterResults(UserInputs userInputs){

            String firstName = userInputs.getFirstName();
            String lastName = userInputs.getLastName();
            String phoneNumber = userInputs.getNumber();

                  //**search only by First Name**
                 if((firstName!=null && firstName!="") 
                    && (lastName==null || lastName=="" )
                   && phoneNumber==null || phoneNumber ==""){

                    //logic to narrow down the result set based on condition met input values
                     list =DAO.getService(firstName);
                      return true;
                       }else if((lastName!=null && lastName!="")// **search only by Last Name**
                                  && (firstName== null || firstName == "")
                                   && (phoneNumber==null || phoneNumber == "")){

                             //logic to narrow down the result set based on condition met input values
 list =DAO.getService(lastName);                          return true;
                       }else if((phoneNumber!=null && phoneNumber!="")// **Search Only by PhoneNumber**
                                               && (lastName==null || lastName=="" )
                                                && (firstName== null || firstName == "")){
        //logic to narrow down the result set based on condition met input values
                    list =DAO.getService(phoneNumber);                                                return true;
                               }else if((firstName!=null && firstName!="")// **Search by all params**.
                                      &&(lastName!=null && lastName!="")
                                       && (phoneNumber!=null && phoneNumber!="")){
        //logic to narrow down the result set based on condition met input values
                      list =DAO.getService(firstName, lastName, phoneNumber);
                                                       return true;
                            }else if((firstName!=null && firstName!="")//**Search by first&last name.**
                                && (lastName!=null || lastName!="" )
                                && (phoneNumber==null || phoneNumber =="")){
                           //logic to narrow down the result set based on condition met input values
                                 list =DAO.getService(firstName, lastName);

                                 return true;
                                 }else if (.......){// **Search by last name & phone number.**
                                       return true;
                                   }else if (.........){// **Search by phone number & first name.**
                                    return true;}



                     return false;

            }

Right now, your logic is equivalent to this single line: 现在,您的逻辑等效于以下这一行:

return (firstName != null && !firstName.isEmpty()) ||
    (lastName != null && !lastName.isEmpty()) ||
    (phoneNumber != null && !phoneNumber.isEmpty());

It looks like you're implementing a search method for each possible combination of your search criteria. 似乎您正在为搜索条件的每种可能组合实施一种搜索方法。 You shouldn't do it like that. 你不应该那样做。 Instead, the method filtering the elements should test each criterion: 相反,过滤元素的方法应测试每个条件:

public List<Element> search(List<Element> allElements, UserInputs criteria) {
    List<Element> acceptedElements = new ArrayList<>();
    for (Element e : allElements) {
        if (isAccepted(e, criteria)) {
            acceptedElements.add(e);
        }
    }
    return acceptedElements;
}

private boolean isAccepted(Element e, UserInputs criteria) {
    String firstName = criteria.getFirstName();
    if (isCriterionFilled(firstName) && !hasFirstName(e, firstName)) {
        return false;
    }

    String lastName = criteria.getLastName();
    if (isCriterionFilled(lastName) && !hasLastName(e, lastName)) {
        return false;
    }

    String phone = criteria.getPhone();
    if (isCriterionFilled(phone) && !hasPhone(e, phone)) {
        return false;
    }

    return true;
}

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

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