I am trying to implement a factory template to get an XML document from a server. (using javax.xml.parsers.DocumentBuilder)
I have classes below, could you please give your opinion? Does the structure make sense for this model? (I asked the same thing about codevide, but if you don't have feedback yet)
DocumentGeneratorFactory (abstract factory)
public interface DocumentGeneratorFactory { public Document createDocument(String scheme, String authority, String path, HashMap<String, String> parameters) throws ParserConfigurationException, SAXException, IOException; }
ProductDocumentGeneratorFactory (Concreate factory)
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; } }
Request (abstract product)
public abstract class Request { Document doc; HashMap<String, String> queryStrings; abstract void prepareRequest() throws ParserConfigurationException, SAXException, IOException; }
ProductRequest (product)
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); } }