繁体   English   中英

如何实现累加器接口

[英]How to implement the Accumulator interface

任务:创建实现通过测试单元的Accumulator的IntegerAccumulator类

我在下面尝试过的代码不起作用,因为测试单元在使用 add 方法时只使用 1 个参数。 我得到的错误代码主要是由于参数。

我试过了:

class IntegerAccumulator implements Accumulator{
    void add(Function function, DataStrore store){

    }

    T getResult(){
        return null;
    }
}

以下是提供的代码: 主要:

public class Main
{
    public static void main(final String[] argv)
    {
        final DataStore<Integer> intStore;
        final DataStore<String> strStore;
        final Accumulator<Integer, Integer> intAccumulator;
        final Accumulator<String, String> strAccumulator;

        intStore = new MemoryDataStore<>(1, 3);
        intStore.setValueAt(0, 0, 10);
        intStore.setValueAt(0, 1, 200);
        intStore.setValueAt(0, 2, 3);

        strStore = new MemoryDataStore<>(1, 3);
        strStore.setValueAt(0, 0, "Hello");
        strStore.setValueAt(0, 1, "Evil");
        strStore.setValueAt(0, 2, "World");

        intAccumulator = new IntegerAccumulator();
        intAccumulator.add((value) -> value, intStore);
        System.out.println(intAccumulator.getResult());

        strAccumulator = new StringAccumulator();
        strAccumulator.add((value) -> value, strStore);
        System.out.println(strAccumulator.getResult()); // Hello, Evil, World

        strStore.setValueAt(0, 0, "Bye");
        strStore.setValueAt(0, 1, null);
        strStore.setValueAt(0, 2, "Bye!"); // Hello, Evil, World, Bye, , Bye!

        strAccumulator.add((value) -> value, strStore);
        System.out.println(strAccumulator.getResult());
    }
}


public interface Accumulator<D, T>
{
    void add(Function<D, T> function, DataStore<D> store);
    T getResult();
}

public interface DataStore<T>
{
    void setValueAt(int row, int col, T value);
    T getValueAt(int row, int col);
    int getRows();
    int getCols();
}
class IntegerAccumulatorTest
{
    @Test
    void add()
    {
        final IntegerAccumulator a;
        final IntegerAccumulator b;

        a = new IntegerAccumulator();
        b = new IntegerAccumulator();
        assertThat(a.getResult(), equalTo(0));
        assertThat(b.getResult(), equalTo(0));

        a.add(1);
        assertThat(a.getResult(), equalTo(1));
        assertThat(b.getResult(), equalTo(0));

        a.add(1);
        assertThat(a.getResult(), equalTo(2));
        assertThat(b.getResult(), equalTo(0));

        a.add(3);
        assertThat(a.getResult(), equalTo(5));
        assertThat(b.getResult(), equalTo(0));

        a.add(null);
        assertThat(a.getResult(), equalTo(5));
        assertThat(b.getResult(), equalTo(0));

        a.add(-6);
        assertThat(a.getResult(), equalTo(-1));
        assertThat(b.getResult(), equalTo(0));
    }
}

从问题和评论中理解的您的任务:

  • 将给定的接口AccumulatorIntegerAccumulator
  • 使给定的测试通过

实现接口:

public interface Accumulator<D, T>
{
    void add(Function<D, T> function, DataStore<D> store);
    T getResult();
}

该接口需要由该接口实现。接口是泛型的,这意味着它具有类型参数(占位符D可以代表作为输入的数据T可以代表作为结果的总数)。 当由具体类实现时,两个类型参数都需要由具体类型(如IntegerString )定义。

测试通过(仅摘录):

class IntegerAccumulatorTest
{
    @Test
    void add()
    {
        final IntegerAccumulator a;
        final IntegerAccumulator b;

        a = new IntegerAccumulator();
        b = new IntegerAccumulator();
        assertThat(a.getResult(), equalTo(0));
        assertThat(b.getResult(), equalTo(0));

        a.add(1);
        assertThat(a.getResult(), equalTo(1));
        assertThat(b.getResult(), equalTo(0));

        // further code omitted
    }

}

请注意,此测试并未完全验证接口。 只测试了一个方法getResult ,另一个add(1)与接口不同。 所以它必须被实现才能通过,虽然不是接口的一部分。

解决测试

class IntegerAccumulator implements Accumulator<Integer, Integer> {

    // needed to store the result
    private int total = 0;

    void add(Function function, DataStrore store){
      // TODO: needs to be implemented to make `main` work
      // not verified in unit-test
    }

    /**
     * Should return the result as accumulation of previously added data.
     */
    @Override
    Integer getResult(){
        return this.total;
    }

    /**
     * Is verified by the unit-test, although not part of the interface {@link Accumulator}.
     */
    @Override
    public void add(int value) {
      this.total += value;
    }
}

现在您的测试应该变为绿色,表明您的实现已经通过了它。 但是主程序可能仍然无法正常工作( main可能不会打印预期的注释),因为接口还没有完全实现。

下一步:使main工作正常

您可能必须:

  • 完全实现接口
  • 和.. main 方法中使用的String的累加器怎么样?
  • 跟随债务合作者:对于两者,我们还需要实现接口DataStore

以上所有实现都不能作为单独的类添加。 因此失去了独立(松散耦合)的好处。 所以你不能随意添加和删除。 相反,您必须编辑/更改现有代码。

您可以修改main方法以将接口实现为匿名类(例如,直接在它们的final声明中)。 或者直接编辑您的interface Accumulator以将方法存根实现default实现

在接口中使用default方法是自 Java 8 以来的一个新特性。它甚至可以使您以前的实现过时,因为您不必覆盖将行为置于接口方法中(它将自动使用定义的default方法)。

请参阅有关默认方法的本教程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM