我想实现从服务器获取XML文档的工厂模式。 (使用javax.xml.parsers.DocumentBuilder)获取XML数据
我现在有下面的类,你能给你的意见吗?这种模式的结构是否有意义? (我问在代码审查的samequestion但是,如果还没有任何反馈还)
DocumentGeneratorFactory(抽象工厂)
public interface DocumentGeneratorFactory {
public Document createDocument(String scheme, String authority,
String path, HashMap<String, String> parameters)
throws ParserConfigurationException, SAXException, IOException;
}
ProductDocumentGeneratorFactory(Concreate厂)
public class ProductDocumentGeneratorFactory implements
DocumentGeneratorFactory {
public Document createDocument(String scheme, String authority,
String path, HashMap<String, String> parameters)
throws ParserConfigurationException, SAXException, IOException {
Uri.Builder uri = new Uri.Builder();
uri.scheme(scheme);
uri.authority(authority);
uri.path(path);
Set<Map.Entry<String, String>> set = parameters.entrySet();
for (Map.Entry<String, String> params : set) {
uri.appendQueryParameter(params.getKey(), params.getValue());
}
URL url = new URL(uri.toString());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
return doc;
}
}
请求 (抽象产品)
public abstract class Request {
Document doc;
HashMap<String, String> queryStrings;
abstract void prepareRequest() throws ParserConfigurationException, SAXException, IOException;
}
ProductRequest(产品)
public class ProductRequest extends Request{
ProductDocumentGeneratorFactory DocumentGeneratorFactory;
HashMap<String, String> queryStrings;
public ProductRequest(ProductDocumentGeneratorFactory DocumentGeneratorFactory,HashMap<String, String> queryStrings){
this.DocumentGeneratorFactory = DocumentGeneratorFactory;
this.queryStrings = queryStrings;
}
@Override
void prepareRequest() throws ParserConfigurationException, SAXException, IOException {
doc = this.DocumentGeneratorFactory.createDocument("http", "ip-address", "default.aspx",this.queryStrings);
}
}
在'ProductDocumentGeneratorFactory'中,我没有看到特定于产品的任何事情。你能详细说明你在那里试着做什么吗? –
会有不同的网址请求。例如,我将有一个产品,评论,用户的XML数据...所以我认为我应该有不同的文档,并做到这一点。我创建了抽象的DocumentGeneratorFactory。 ProductDocumentGeneratorFactory使用querystrin hashmap创建特定文档。 (也会有VODDocumentGeneratorFactory,CommentDocumentGeneratorFactory ...) – Talha
然后ProductRequest产品将更便宜的获得XMl操作的其他部分。例如,这个人会获取数据并将其解析为特定的对象。但我还没有写这部分。我只是想知道逻辑?或者你对这个结构的方法 – Talha