Java允许我们在Enum上嵌入数据和行为.我不想直接在Enum上实现工厂,因为我认为这不是它的作用.
但我可以在枚举上放置类引用,并在外部工厂上构造对象.与传统工厂模式相比,您的最佳实施方案是什么?在哪种情况下哪种解决方案更好用?
现在,代码.
两种解决方案中用于构造对象的函数.如果需要,可以使用Map实现fly-weight模式.
private Action getAction(Class<? extends Action> actionClazz) {
// logger + error handling
return actionClazz.newInstance();
}
Run Code Online (Sandbox Code Playgroud)
1)与传统工厂:
public enum ActionEnum {
LOAD_DATA,
LOAD_CONFIG;
}
public Action getAction(ActionEnum action) {
switch (action) {
case LOAD_CONFIG:
return getAction(ActionLoadConfig.class);
case LOAD_DATA:
return getAction(ActionLoadData.class);
}
}
Run Code Online (Sandbox Code Playgroud)
2)使用Enum风格的工厂:
public enum ActionEnum {
LOAD_DATA(ActionLoadConfig.class),
LOAD_CONFIG(ActionLoadData.class);
public ActionEnum(Class<? extends Action> clazz){...}
public Class<? extends Action> getClazz() {return this.clazz}
}
public Action getAction(ActionEnum action) {
return getAction(action.getClazz());
}
Run Code Online (Sandbox Code Playgroud) 是否有可能在一次移动中移动多个静态方法和/或多个静态字段?
public final class ClassA {
public static final String CONSTANTE_A = "CONSTANTE_A";
public static final String CONSTANTE_B = "CONSTANTE_B";
public static void methodA() {
// statements....
}
public static void methodB() {
// statements....
}
}
public final class ClassB {
// empty class
}
Run Code Online (Sandbox Code Playgroud)
我希望能够选择methodA,methodB,CONSTANTE_A和CONSTANTE_B,并对ClassB进行"移动..."
我想迭代一个包含<s:select>列表源名称的字符串列表,但HTML输出不是预期的:它是显示的列表的名称,而不是内容.
我的Action代码:
public class DescriptionTabArchiveAction extends ActionSupport {
private List<String> vegetables = new ArrayList<String>();
private List<String> devices = new ArrayList<String>();
// contain "vegetables" and "devices".
private List<String> selectList = new ArrayList<String>();
@Action("multipleSelect")
public String multipleSelect() {
vegetables.add("tomato");
vegetables.add("potato");
devices.add("mouse");
devices.add("keyboard");
selectList.add("vegetables");
selectList.add("devices");
return SUCCES;
}
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
JSP:
<s:iterator value="selectList" var="listName">
<s:select list="%{#listName}" />
<!-- I tried with this line too : same behaviour. -->
<%-- <s:select list="#listName" /> --%>
</s:iterator> …Run Code Online (Sandbox Code Playgroud) 我在一个项目中工作,其中包含许多嵌入了errorCode的"BusinessException".
在每个单元测试异常中,我必须测试这些错误代码重复这种模式:
@Test
public void zipFileReaderCtorShouldThrowAnExceptionWithInexistingArchive() {
try {
zfr = new ZipFileReader("unexpected/path/to/file");
fail("'BusinessZipException' not throwed");
} catch (BusinessZipException e) {
assertThat("Unexpected error code", e.getErrorCode(), is(ErrorCode.FILE_NOT_FOUND));
} catch (Exception e) {
fail("Unexpected Exception: '" + e + "', expected: 'BusinessZipException'");
}
}
Run Code Online (Sandbox Code Playgroud)
(由于错误代码测试,使用JUnit注释是不可能的)
我很无聊,特别是因为我必须在fail()的错误消息中复制/粘贴异常名称.
所以,我写了一个Util类.我使用抽象类来处理异常断言测试.
public abstract class TestExceptionUtil {
public void runAndExpectException(Class expectedException, String expectedErrorCode) {
String failUnexpectedExceptionMessage = "Unexpected exception. Expected is: '%s', but got: '%s'";
try {
codeToExecute();
fail("'" + expectedException.getName() + "' not throwed");
} catch …