用很多开关和ifs重构类

时间:2015-08-04 12:48:12

标签: java enums functional-programming

我正在处理一个有很多开关和ifs的类。我想听听有人提出任何建议,以便获得最好的重构。我在想是否有可能使用函数式编程来避免太多ifs。

该课程有两个枚举,每个枚举至少有8种可能性。因此,逻辑基于根据switch-if-else结构返回枚举。

提前致谢!

3 个答案:

答案 0 :(得分:3)

如果没有代码,很难给出明确的答案,但主要的重构方法之一是Replacing Conditional with Polymorphism

修改Java example from Martin Fowler

答案 1 :(得分:0)

首先在OO ifs和switch中通常可以用 overriden 方法替换。在enum as方法中,或使用EnumMap。在java 8中,您也可以将一个lambda添加到枚举构造函数中。

更重要的是,我认为很多案例似乎表明业务逻辑是用代码写出来的。您也可以使用声明式样式,例如XML。这通常简化了结构并且更加确定。

答案 2 :(得分:0)

如果可能的话,将分支中的多个功能移动到具有公共接口的类中。然后尝试用动态双重调度替换所有静态分支。

比如说:

  int a = new Scanner(System.in).nextInt(); // Any number can be given here.
  if(a % 5 == 0) {
    performMultiple5Operation(a);
  } else if(a % 6 == 0) {
    performMultiple6Operation(a);
  } else if(a % 7 == 0) {
    performMultiple7Operation(a);
  } else if(isPrime(a)) {
    performPrimeOperation(a);
  }

使用动态双重调度方式替换此静态分支,如下所示:

public class DynamicDoubleDispatcher {
    public static void main(String arg[]) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        scanner.close();
        NumericOperator no = new NumericOperator(new Performer[] { new Multiple5Performer(), new Multiple6Performer(),
                new Multiple7Performer(), new PrimePerformer()});
        no.operate(a);  // The performs the right operation based on the input value of a.
    }
}

interface Performer {
    public boolean perform(int a);
}

class NumericOperator {
    private List<Performer> performers = new ArrayList<>();

    public NumericOperator(Performer[] arrPerformers) {
        this.performers = Arrays.asList(arrPerformers);
    }

    public void operate(int a) {
        for (Performer performer : performers) {
            if (performer.perform(a))
                break;
        }
    }
}

class Multiple5Performer implements Performer {
    public boolean perform(int a) {
        boolean isPerformed = false;
        if (a % 5 == 0) {
            isPerformed = true;
            performMultiple5Operation();
        }
        return isPerformed;
    }

    private void performMultiple5Operation() {
        // TODO: Do the multiple 5 Operation
    }
}

class Multiple6Performer implements Performer {
    public boolean perform(int a) {
        boolean isPerformed = false;
        if (a % 6 == 0) {
            isPerformed = true;
            performMultiple6Operation();
        }
        return isPerformed;
    }

    private void performMultiple6Operation() {
        // TODO: Do the multiple 6 Operation

    }
}

class Multiple7Performer implements Performer {
    public boolean perform(int a) {
        boolean isPerformed = false;
        if (a % 7 == 0) {
            isPerformed = true;
            performMultiple7Operation();
        }
        return isPerformed;
    }

    private void performMultiple7Operation() {
        // TODO: Do the multiple 7 Operation

    }
}

class PrimePerformer implements Performer {
    public boolean perform(int a) {
        boolean isPerformed = false;
        if (isPrime(a)) {
            isPerformed = true;
            performPrimeOperation();
        }
        return isPerformed;
    }

    private void performPrimeOperation() {
        // TODO: Do the prime Operation
    }

    private boolean isPrime(int a) {
        boolean isPrime = false;
        // TODO: check a is prime or not
        return isPrime;
    }
}

您将牺牲性能,但它有助于为将来使用提供可扩展的设计,同时对现有类进行特定且有限的修改。