I wanted an easy way for non coders to map new "Tests" with "Test" Types with the least amount of code change, using Guice for dependency injection. I like the simplicity, but not the obvious lack of type safety. Any suggestions to make this better?
我想要一个简单的方法让非编码人员使用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;
}
}
The "Test" would get instantiated like this:
“Test”会像这样实例化:
TestIF tester = Guice.createInjector().getInstance(testType.tester());
tester.execute();
1 个解决方案
#1
0
This helps force the Tests to implement TestIF, which is a bit safer in this case.
这有助于强制测试实现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;
}
#1
0
This helps force the Tests to implement TestIF, which is a bit safer in this case.
这有助于强制测试实现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;
}