我试图理解简化设计的概念。下面是具有两个具有相同功能的类的代码。
interface Factorial {
long factorial(long n);
}
class FactorialA implements Factorial {
long factorial(long n) {
// imperative implementation goes here
}
}
class FactorialB implements Factorial {
long factorial(long n) {
// recursive implementation goes here
}
}
class FactorialB implements Factorial {
long factorial(long n) {
// functional implementation goes here
}
}
...
class FactorialTest {
@Test
public void testFactorialA() {
testFactorial(new FactorialA());
}
...
private void testFactorial(Factorial f) {
assertThat(...)
}
}
在这种情况下,我将如何减少重复?我曾尝试将它们放在一个类中并使方法静态化,但我被困在如何称呼它上。我也尝试过按照上面的接口,但在这种情况下,单元测试将受到重复测试的困扰。任何有用的提示将不胜感激,因为这将帮助我减少代码中的冗余。如何将上面的代码压缩为一类,减少测试冗余?