简化代码以取消过多的if-else语句

时间:2019-01-20 16:56:32

标签: design-patterns

我试图用Java设计一个奖励系统,该系统将根据用户当前的交易信息来奖励他们。 我必须根据交易满足的特定条件返回不同的奖励权重。这是分配奖励权重的类的代码示例。

class RewardWeight {

    #condition 1
    public boolean is50thTransactionOfDay(Transaction t){
        //logic to find 50th Transaction
    }

    #condition 2
    public boolean is200thTransactionOfWeek(Transaction t){
        //logic to find 200th Transaction
    }


    #condition 3
    public boolean isTransactionAbove2000(Transaction t){
        //logic goes here
    }

    #condition 4
    public boolean isTransactionRepeated(Transaction t){
        //logic goes here
    }

    public int getRewardWeight(){
        // if condition 1 and condition 2 satisfied return 1
        // if condition 2 and condition 3 satisfied return 2
        // if condition 1 and condition 3 satisfied return 3
        // if condition 3 and condition 4 satisfied return 4
        // if condition 3 and condition 4 satisfied return 8
        // if condition 1 , 2 and 3 satisfied return 5
        // if condition 1 , 2 and 3,4 satisfied return 6
    }
}

是否可以像

一样返回不同的权重
  

getRewardWeight()

方法,如果没有太多其他条件的话。 另外,它还应该灵活地支持任何新功能。(例如新的奖励标准)。

2 个答案:

答案 0 :(得分:0)

不确定是否可以减少if语句的数量,但是最好的改进是可维护性(因为您期望更多的奖励标准)。

代替5个不同的奖励功能,创建一个简单的奖励功能并通过标准编号。在该函数中,您可以使用switch语句再次拆分它们。这不会减少if的值,但是您只有一个入口点,因此可以在for循环中调用奖励条件。让for循环返回数组中的值。比起您可以将数组值组合成一个数字来找到最终的奖励。

类似于伪代码:

class RewardWeight {
    public boolean checkReward(int number)
    {
        bool reward = False
        switch (number)
        {
        case 0: // 200th order
           // Logic or call function
        case 1: // Next
           ...
        }
        return reward
    }

替代1:

    bool weights[MAX_REWARDS];

    public int getRewardWeight() {
        for (int reward = 0; reward < MAX_REWARDS; reward++) { 
            weights[reward] = checkReward(reward) ? 1 : 0;

        // Here you can  use all if statements for the conditions.

替代2:

如果列表很大,则可以将组合因子放在单个值中,并制作一个包含权重因子的大数组:

    Rewards[] = { 1, 6, 3, 4, 6, ..., 25, 2, 3, 4, 3} // Means e.g.
        // Assuming there are 4 conditions, there are 16 values, the 5th value 
        // from the end
        // which is 0101 binary, means: condition 1 false, condition 2 true,
        // condition 3 false, condition 4 true has value 25.

    public int getRewardWeight() {
        for (int reward = 0; reward < MAX_REWARDS; reward++) { 
            weights += Power(2, reward) * checkReward(reward) ? 1 : 0;

        return Rewards[weights];

但是请注意,备选方案2可能会导致数组变大。

答案 1 :(得分:0)

您可以使用“责任链”设计模式实施:

    public abstract class Condition {

    private final boolean condition1;
    private final boolean condition2;
    private Condition next;

    public Condition(boolean condition1, boolean condition2) {
        this.condition1 = condition1;
        this.condition2 = condition2;
    }

    public void setNext(Condition next) {
        this.next = next;
    }

    public abstract void execute();

    public boolean isCondition1() {
        return condition1;
    }

    public boolean isCondition2() {
        return condition2;
    }

    public Condition getNext() {
        return next;
    }
}

Condition的第一个实现类

  public final class Condition12 extends Condition {

    public Condition12(boolean condition1, boolean condition2) {
        super(condition1, condition2);
    }

    @Override
    public void execute() {
        if (isCondition1() && isCondition2()) {
            System.out.println(1);
        }
        getNext().execute();
    }
}

Condition的第二个实现类

  public final class Condition23 extends Condition {

    public Condition23(boolean condition1, boolean condition2) {
        super(condition1, condition2);
    }

    @Override
    public void execute() {
        if (isCondition1() && isCondition2()) {
            System.out.println(2);
        }
        getNext().execute();
    }
}

Condition的第三种实现类

  public final class Condition34 extends Condition {

    public Condition34(boolean condition1, boolean condition2) {
        super(condition1, condition2);
    }

    @Override
    public void execute() {
        if (isCondition1() && isCondition2()) {
            System.out.println(3);
        }
    }
}

测试程序

   public class TestProgram {

    public static void main(String[] args) {
        //Case1
        Condition condition12 = new Condition12(true, false);
        Condition condition23 = new Condition23(true, true);
        Condition condition34 = new Condition34(true, false);

        condition12.setNext(condition23);
        condition23.setNext(condition34);
        // Output is 2
        condition12.execute();

        //Case2
        condition23 = new Condition23(false, true);
        condition34 = new Condition34(true, true);

        condition12.setNext(condition23);
        condition23.setNext(condition34);
        // Output is 3
        condition12.execute();
    }
}

如果您还有其他问题,请告诉我。