I need to build a 'MyThingie' using 'A'. MyThingie is in the model package and currently no code in model accesses the DB. My question is which of the following patterns should I use? The top or bottom? Or something completely different.
package com.model;
public class MyThingie {
private String foo = "";
private String bar = "";
private X x = null;
private Y y = null;
private Z z = null;
public MyThingie() {
}
public MyThingie(A a, X x, Y y, Z z) {
this.foo = a.getFoo();
this.bar = a.getBar();
this.x = x;
this.y = y;
this.z = z;
}
public static MyThingie createFromDb(A a) {
X x = fetchXFromDB(a.getBlah());
Y y = fetchYFromDB(a.getFlah());
Z z = fetchZFromDb(a.getZlah());
return new MyThingie(a, x, y, z);
}
// getters and setters
}
// ----------- OR----------------
package com.model;
public class MyThingie {
private String foo = "";
private String bar = "";
private X x = null;
private Y y = null;
private Z z = null;
public MyThingie() {
}
// getters and setters
}
package com.builder;
public class MyThingieBuilder {
public MyThingieBuilder() {
}
public static MyThingie createFromDb(A a) {
MyThingie m = new MyThingie();
m.setFoo(a.getFoo());
m.setBar(a.getBar());
X x = fetchXFromDB(a.getBlah());
Y y = fetchYFromDB(a.getFlah());
Z z = fetchZFromDb(a.getZlah());
m.setX(x);
m.setY(y);
m.setZ(z);
return m;
}
}