Retrieving XML Data

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); } } 
+4
source share
2 answers

This does not make sense, since specific factories should return the objects they created, but you use the built-in Java DocumentBuilder db = dbf.newDocumentBuilder(); , so return the same object no matter what.

This would make sense if ProductDocumentGeneratorFactory somehow returned an object of type ProductDocument .

Factory abstract template example

So, in your case, I do not think this template makes sense.

0
source
 public abstract class AbstractFactory { public abstract Product createProduct(); public abstract Comment createComment(); public abstract User createUser(); public Document createDocument(String scheme, String authority, String path, HashMap<String, String> parameters) throws ParserConfigurationException, SAXException, IOException { //---------Your code ----- return doc; } } public class XMLFactory implements AbstractFactory { public Product createProduct() { /* use createDocument() */ } //Remaining Code for other stuff u want to create... } public interface Product{ //product } public class ProductImpl implements Product { //implementation } 
0
source

All Articles