[英]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));
}
}
从问题和评论中理解的您的任务:
Accumulator
为IntegerAccumulator
public interface Accumulator<D, T>
{
void add(Function<D, T> function, DataStore<D> store);
T getResult();
}
该接口需要由该接口实现。接口是泛型的,这意味着它具有类型参数(占位符D
可以代表作为输入的数据; T
可以代表作为结果的总数)。 当由具体类实现时,两个类型参数都需要由具体类型(如Integer
或String
)定义。
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
工作正常您可能必须:
String
的累加器怎么样?DataStore
以上所有实现都不能作为单独的类添加。 因此失去了独立(松散耦合)的好处。 所以你不能随意添加和删除。 相反,您必须编辑/更改现有代码。
您可以修改main
方法以将接口实现为匿名类(例如,直接在它们的final
声明中)。 或者直接编辑您的interface Accumulator
以将方法存根实现为default
实现。
在接口中使用default
方法是自 Java 8 以来的一个新特性。它甚至可以使您以前的实现过时,因为您不必覆盖将行为置于接口方法中(它将自动使用定义的default
方法)。
请参阅有关默认方法的本教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.