如何处理java中使用同一对象的多if语句和事例?

7dl7o3gd  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(364)

我有一个问题,当使用多个if语句在逻辑中使用同一对象时,如何对代码进行最佳排序。
我在一个表中自定义列值,在这个表中,根据columnid,将应用不同的逻辑来提取该特定列的值。
假设我有三个不同的列,分别命名为“column 1”、“column 2”和“column 3”,其中“column 1”和“column 3”在其逻辑中都使用名为obj的自定义对象,而“column 2”则没有。
排列if语句的最佳方法是什么?我应该在哪里创建对象?我添加了三个不同的案例,哪一个是最好的,为什么?另一个更好的解决方案?
提前谢谢!

// Case 1
public Object getColumnValue(String columndId) {
    if (columndId.equals("Column 1") || columndId.equals("Column 3")) {
        customObject obj = new customObject();
        if (columndId.equals("Column 1")) {
            // do something with the custom object
        } else if (columndId.equals("Column 3") ||) {
            // do something else with the custom object
        }
    } else if (columndId.equals("Column 2")) {
        // do something without the custom object
    } else {
        return null;
    }
}

// Case 2
public Object getColumnValue(String columndId) {
    customObject obj = new customObject();
    if (columndId.equals("Column 1") {
        // do something with the custom object
    } else if (columndId.equals("Column 2")) {
        // // do something without the custom object
    } else if (columndId.equals("Column 3")) {
        /// do something with the custom object
    } else {
        return null;
    }
}

// Case 3
public Object getColumnValue(String columndId) {
    if (columndId.equals("Column 1") {
        customObject obj = new customObject();
        // do something with the custom object
    } else if (columndId.equals("Column 2")) {
        // // do something without the custom object
    } else if (columndId.equals("Column 3")) {
        customObject obj = new customObject();
        /// do something with the custom object
    } else {
        return null;
    }
}

// Case 4
public Object getColumnValue(String columndId) {
    switch (columnId) {
        case "Column 1":
        case "Column 3":
            customObject obj = new customObject();
            switch (columnId) {
                case "Column 1":
                    // do somethingt with custom object
                case "Column 3":
                    // do something else with custom object
            }
        case "Column 2":
            // do something without custom object
    }
}
8aqjt8rx

8aqjt8rx1#

我认为策略设计模式是最好的选择。每个定制都有一个这样的专用类(更好的名称是mycustomizationstrategy):

public abstract class CustomizationStrategy {

  protected abstract String getSupportedColumnId(); 

  public boolean isApplicable(String columnId) {
    return getSupportedColumnId().equals(columnId);
  }

  public abstract Object customizeColumnValue(String columnId);

}

public class MyCustomizationStrategy extends CustomizationStrategy {
  private final static String SUPPORTED_COLUMN_ID = "Column X";

  @Override
  protected List<String> getSupportedColumnId() {
    return SUPPORTED_COLUMN_ID;
  }

  public Object customizeColumnValue(String columnId) {
    return ...
  }
}

在拥有 getColumnValue() :

private static final List<CustomizationStrategy> CUSTOMIZATION_STRATEGIES = 
  Arrays.asList(new MyCustomizationStrategy(), ...);

public Object getColumnValue(String columndId) {
  return CUSTOMIZATION_STRATEGIES.stream()
    .filter(strategy -> strategy.isApplicable(columnId))
    .map(strategy -> strategy.customizeColumnValue(columnId))
    .findFirst()
    .orElse(null);
}

相关问题