mardi 23 février 2016

Design Pattern for Data Structure with Methods to Populate It?

I have a data structure in Java that I am populating via different methods. One method populates it from an API, another method populates it from parsing some HTML, another populates it a different way, etc. Basically, a method for every data source that could populate it. What I'm wondering is, what design patterns are available in Java for this? What's the best/cleanest OOP approach to this problem?

E.g.,

public class Data {
    private String foo;
    private List<String> bar;
    private Map<String, Integer> baz;

    public Data (String foo, List<String> bar, Map<String, Integer baz) {
        this.foo = foo;
        this.bar = bar;
        this.baz = baz;
    }

    // Setters and Getters here, etc
}


public class FacebookParser {
    private Document dom;

    public static Data parse(Document dom) {
        // Parse document
        // Create Data object
        return Data;
    }
}

public class TwitterParser {
    private Document dom;

    public static Data parse(Document dom) {
       // Parse Twitter
       Data d = new Data(stuff from twitter);
       return d;
    }
}

Aucun commentaire:

Enregistrer un commentaire