我希望非编码人员能够轻松地使用Guice进行依赖注入,将新的“测试”映射到具有最少代码更改的“测试”类型。我喜欢简单,但不是明显缺乏类型安全。有什么建议让这更好吗?
public enum TestType {
TEST_TYPE_1("TEST-1", Test1.class),
TEST_TYPE_2("TEST-2", Test2.class),
TEST_TYPE_3("TEST-3", Test3.class),
TEST_TYPE_4("TEST-4", Test4.class),
TEST_TYPE_5("TEST-5", Test5.class);
private final String testType;
private final Class<TestIF> tester;
private <TestIF> TestType(String testType, Class<TestIF> tester) {
this.testType = testType;
this.tester = (Class<tester.TestIF>) tester;
}
public String toString() {
return this.testType;
}
public Class<TestIF> tester() {
return this.tester;
}
}
“Test”会像这样实例化:
TestIF tester = Guice.createInjector().getInstance(testType.tester());
tester.execute();
答案 0 :(得分:0)
这有助于强制测试实现TestIF,在这种情况下更安全。
public enum TestType {
TEST_TYPE_1("TEST-1", Test1.class),
TEST_TYPE_2("TEST-2", Test2.class),
TEST_TYPE_3("TEST-3", Test3.class),
TEST_TYPE_4("TEST-4", Test4.class),
TEST_TYPE_5("TEST-5", Test5.class);
private final String testType;
private final Class<? extends TestIF> tester;
private <T extends TestIF> TestType(String testType, Class<? extends TestIF> tester) {
this.testType = testType;
this.tester = (Class<tester.TestIF>) tester;
}
public String toString() {
return this.testType;
}
public Class<? extends TestIF> tester() {
return this.tester;
}