Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 寻找减少重复的方法_Java_Oop_Inheritance_Polymorphism - Fatal编程技术网

Java 寻找减少重复的方法

Java 寻找减少重复的方法,java,oop,inheritance,polymorphism,Java,Oop,Inheritance,Polymorphism,我试图理解简化设计的概念。下面是两个具有相同功能的类的代码 interface Factorial { long factorial(long n); } class FactorialA implements Factorial { long factorial(long n) { // imperative implementation goes here } } class FactorialB implements Factorial { long

我试图理解简化设计的概念。下面是两个具有相同功能的类的代码

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(...)
   }
}
在这种情况下,我如何减少重复?
我曾尝试将它们放在一个类中,并使这些方法成为静态的,但我仍然无法确定该如何称呼它们。我也尝试了上面提到的接口,但是在这种情况下,单元测试将被重复测试所困扰。任何有用的提示都将不胜感激,因为这将帮助我减少代码中的冗余。如何将上述代码压缩成一个类,减少测试中的冗余?

根据您在评论中的说明,我将这样做:

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(...)
   }
}
因此,测试代码不会重复,但每个实现仍然有一个通过/失败的单元测试

如果希望将每个实现作为静态方法,则可以执行以下操作:

class Factorial {
  static long imperative(long n) { ... }
  static long recursive(long n) { ... }
  static long functional(long n) { ... }
}

class FactorialTest {
   @Test
   public void testFactorials() {
      testFactorial(n -> Factorial.imperative(n));
      testFactorial(n -> Factorial.recursive(n));
      testFactorial(n -> Factorial.functional(n));
   }
...
   private void testFactorial(Function<Long,Long> f) {
     assertThat(...)
   }
}
类阶乘{
静态长命令(长n){…}
静态长递归(长n){…}
静态长泛函(长n){…}
}
类因子测试{
@试验
公共void testFactorials(){
testFactorial(n->Factorial.private(n));
testFactorial(n->Factorial.recursive(n));
testFactorial(n->Factorial.functional(n));
}
...
私有void testFactorial(函数f){
断言(……)
}
}

似乎您的主要问题是如何避免为每个接口实现复制测试

假设您使用的是JUnit,那么我建议使用带有方法源的参数化测试。这允许您使用一个方法来生成每个实现的实例

解决方案将类似于:

@ParameterizedTest
@MethodSource("factorialProvider")
void testConstants(Factorial impl) {
    assertEquals(1, impl.factorial(1));
}

@ParameterizedTest
@MethodSource("factorialProvider")
void testInduction(Factorial impl) {
    for (int i = 10; i < 20; i++)
        assertEquals(impl.factorial(i) * (i + 1), impl.factorial(i + 1));
}

static Stream<String> factorialProvider() {
    return Stream.of(new FactorialA(), new FactorialB(), new FactorialC());
}
@ParameterizedTest
@MethodSource(“factorialProvider”)
void testConstants(阶乘impl){
资产质量(1,显式阶乘(1));
}
@参数化测试
@MethodSource(“factorialProvider”)
无效测试导入(阶乘impl){
对于(int i=10;i<20;i++)
资产质量(显式阶乘(i)*(i+1),显式阶乘(i+1));
}
静态流factorialProvider(){
返回Stream.of(new FactorialA(),new FactorialB(),new FactorialC());
}

您可以使用同一个提供程序进行任意多个测试。

您的类非常相似,您可以只使用一个类,并指定要在每个实例的基础上打印的消息。因此,我正在尝试将这一概念应用到我正在研究的某个方面。基本上我有一个关于阶乘的程序,它有三种不同的方式。递归、命令式和函数式。当我进行测试时,我最终会对每个方法进行重复测试。我想减少这一点,并有一个完整的测试,其中包括所有内容。如果这是你想要回答的问题,你可能应该发布该问题,而不是你发布的问题。我不太确定你希望你的一个类看起来像什么,你能编辑你的问题来解释吗?我已经用接口尝试过了,还做了多重测试。但问题是,它们都是阶乘的函数——一个是强制性的、递归的和功能性的。我将如何调用它?我已经在上面澄清了。谢谢,我将尝试这个,因为它似乎是我的问题的理想选择。