java - Better Design for multiple data retrieval -
i inherited code decorator pattern used retrieve data multiple data sources. flow of code initial datagetter created , other datagetters encapsulate retrieve more data other sources. kind of looks this:
datagettera gettera = new datagettera(); datagetterb getterb = new datagetterb(gettera); datagetterc getterc = new datagetterc(getterb); map data = getterc.getdata();
each datagetter
retrieves data different table , combines data 1 map
.
i wondering there better design this?
you define interface load data map.
public interface dataloader {
void loaddata(map map);
}
implement interface different data sources
public class a_dataloader implements dataloader {
public void loaddata(java.util.map map) { // load data data source map }
}
public class b_dataloader implements dataloader {
public void loaddata(java.util.map map) { // load data data source b map }
}
public class c_dataloader implements dataloader {
public void loaddata(java.util.map map) { // load data data source c map }
}
load data
public static void main(string[] args) {
map map = new hashmap(); // load data data source dataloader loader = new a_dataloader(); loader.loaddata(map); // load data data source b loader = new b_dataloader(); loader.loaddata(map); // load data data source c loader = new c_dataloader(); loader.loaddata(map);
}
you may have xml/database configuration define list of dataloader implementation used. if use ioc container such spring ioc or cdi. easy instance of dataloader implementation.
Comments
Post a Comment