我的课程有几个领域。
public class Foo {
int firstCoef;
int secondCoef;
public Foo(String args){
this.firstCoef=Integer.parseInt(args[0]);
this.secondCoef=Integer.parseInt(args[1]);
}
}
以这种方式分配参数是因为我通过从 .csv 读取数据来创建此类的多个成员。我有另一个管理 Foo 实例列表的类。它通过从文件中读取它来立即创建整个列表并使用该列表进行计算。在类构造函数中创建列表时,它使用new Foo(string)
.
public class FooManager {
protected List<Foo> allFoos = new ArrayList<Foo>();
public FooManager(List<String[]> input) {
String[] line;
for (int lineNumber = 0; lineNumber < input.size(); lineNumber++) {
line = input.get(lineNumber);
allFoos.add(new Foo(line));
}
}
public int calculate(int number) {
int result = 0;
for (Foo foo : allFoos) {
result += Math.pow(number + foo.getFirstCoef(), foo.getSecondCoef());
}
return result;
}
}
据我了解,这被认为是糟糕的设计,因为无法注入依赖项。此外,很难测试。如何在不使输入复杂化的情况下更改设计?这两个类的唯一目标是最终能够执行计算。