4

サーバーから XML ドキュメントを取得するためのファクトリ パターンを実装しようとしています。(javax.xml.parsers.DocumentBuilder を使用)

今のところ以下のクラスがありますが、ご意見をいただけますか?このパターンの構造は理にかなっていますか? (コードレビューで同じ質問をしましたが、まだフィードバックがない場合)

DocumentGeneratorFactory (抽象ファクトリー)

public interface DocumentGeneratorFactory {

    public Document createDocument(String scheme, String authority,
            String path, HashMap<String, String> parameters)
            throws ParserConfigurationException, SAXException, IOException;    

}

ProductDocumentGeneratorFactory (ファクトリの作成)

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);     
    }

}
4

2 に答える 2

0

具体的なファクトリは作成したオブジェクトを返す必要がありますが、組み込みの Java を使用しているDocumentBuilder db = dbf.newDocumentBuilder();ため、何があっても同じオブジェクトを返すため、これは意味がありません。

ProductDocumentGeneratorFactory何らかの方法で type のオブジェクトを返す場合、それは理にかなっていますProductDocument

Abstract Factory パターンの例

したがって、あなたの場合、このパターンは理にかなっているとは思いません。

于 2012-11-30T10:05:43.443 に答える
0
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

}
于 2012-11-30T13:55:58.017 に答える