java - Create new object with Builder pattern with "old" object reference -
i playing around builder pattern , stuck how add new "property" new-created object:
public class msprojecttaskdata { private boolean isalreadytransfered; private string req; public static class builder { private boolean isalreadytransfered = false; public builder withtransfered(boolean val) { isalreadytransfered = val; return this; } public msprojecttaskdata build() { return new msprojecttaskdata(this); } } private msprojecttaskdata(builder builder) { isalreadytransfered = builder.isalreadytransfered; } public msprojecttaskdata(string req) { this.req = req; } }
i can create new object builder this:
msprojecttaskdata data = new msprojecttaskdata.builder().withtransfered(true).build();
but approach req
string new-created object lost (of course).
is there possibility create new object new set isalreadytransfered
variable , with "old" req
string "old" object?
maybe have pass old object reference builder not know how this. maybe use of builder pattern not usefull approach?
edit: (after comment eugene)
think, got it:
public static class builder { private boolean isalreadytransfered = false; private msprojecttaskdata data; public builder(msprojecttaskdata data) { this.data = data; } public builder withtransfered(boolean val) { isalreadytransfered = val; data.setalreadytransfered(isalreadytransfered); return this; } public msprojecttaskdata build() { return data; } }
seems work or wrong code above? can use approach without consideration?
make builder constructor take argument "old" object , set whatever want new one.
edit
you need read bit more builder pattern better grasp @ , if really need it.
the general idea builder pattern used when have optional elements. effective java item 2 best friend here.
for class, if want build 1 object , use builder pattern @ same time, you
- either pass "old" object in builder constructor
- create method from or fromold, etc.
so how looks like? going provide first 1 can figure out second on own.
class msprojecttaskdata { private final string firstname; private final string lastname; private final int age; private msprojecttaskdata(builder builder){ this.firstname = builder.firstname; this.lastname = builder.lastname; this.age = builder.age; } public static final class builder{ //fields required must private final private final string firstname; private final string lastname; //fields optional not final private int age; public builder(string firstname, string lastname){ this.firstname = firstname; this.lastname = lastname; } public builder(msprojecttaskdata data){ this.firstname = data.firstname; this.lastname = data.lastname; } public builder age(int val){ this.age = val; return this; } public msprojecttaskdata build(){ return new msprojecttaskdata(this); } } public string getfirstname() { return firstname; } public string getlastname() { return lastname; } public int getage() { return age; } }
and how create 1 object another:
msprojecttaskdata.builder builder = new msprojecttaskdata.builder("bob", "smith"); msprojecttaskdata oldobj = builder.age(23).build(); msprojecttaskdata.builder newbuilder = new msprojecttaskdata.builder(oldobj); msprojecttaskdata newobj = newbuilder.age(57).build(); system.out.println(newobj.getfirstname() + " " + newobj.getlastname() + " " + newobj.getage()); // bob smith 57