我正在开发一个接受 SOAP 消息的 java 应用程序。SOAP 的主体包含各种文档。他们的数量不时变化(取决于专辑的版本)。
对于他们的分析,我正在尝试应用模式抽象工厂。但是我的实现,我遇到了一个问题:
IAlbumFactory albumFactory = AlbumFactory.buildDocument (Album.A_5_0_12);
第一个参数有我可以指向任何类型的枚举CustomDocument(虽然CustomDocument.DO1只有有效的Du)。而且这个错误只出现在运行时
IDocumentEntity <Du, org.w3c.dom.Element> documentEntity =
albumFactory.getWorker (CustomDocument.DO1);
如何避免?
一些类:
public interface IDocumentEntity<T,E> {
T getReport(E e) throws JAXBException ;
}
public interface IAlbumFactory {
IDocumentEntity getWorker(CustomDocument document);
}
/**
* Class for convert document DO1 (album 5.0.12) from org.w3c.dom.Element to Du
* entity type
*
* @author uas
*/
public class DO1_5_0_12 implements IDocumentEntity<Du, org.w3c.dom.Element> {
protected DO1ReportInType unmarshall(org.w3c.dom.Element e) throws JAXBException {
DO1ReportInType resultType = null;
JAXBContext result = JAXBContextHelper_DO1.getJaxbContextInstance();
Unmarshaller u = result.createUnmarshaller();
Object c = u.unmarshal(e);
if (c instanceof JAXBElement) {
JAXBElement jaxbe = (JAXBElement) c;
resultType = (DO1ReportInType) JAXBIntrospector.getValue(jaxbe);
}
return resultType;
}
@Override
public Du getReport(org.w3c.dom.Element e) throws JAXBException {
DO1ReportInType dO1Report = unmarshall(e);
DO1ReportIn_JAXBtoORCL btoORCL = new DO1ReportIn_JAXBtoORCL(dO1Report);
return btoORCL.getReport();
}
}
专辑 5_0_12 的工厂。
public class AlbumFactory_5_0_12 implements IAlbumFactory {
/**
* Return documentWorker byn CustomDocument value
* @param customDocument
* @return throws IllegalArgumentException
*/
@Override
public IDocumentEntity getWorker(CustomDocument customDocument) {
IDocumentEntity doc = null;
switch (customDocument) {
case DO1:
doc = new DO1_5_0_12();
break;
case DO2:
doc = new DO2_5_0_12();
break;
default:
throw new IllegalArgumentException("For album 5.0.12 " + customDocument.DocName() + " not support");
}
return doc;
}
}
public class AlbumFactory {
private AlbumFactory() {
}
public static IAlbumFactory buildDocument(Album album) {
IAlbumFactory result = null;
switch (album) {
case A_5_0_12:
result = new AlbumFactory_5_0_12();
break;
default:
throw new IllegalArgumentException("This version " + album.AlbumName() + " is not support");
}
return result;
}
}