Create a new object using the Builder template with an "old" object reference

I play with the Builder pattern and loop how to add a new “property” to the new object being created:

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 a new object with Builder as follows:

 MsProjectTaskData data = new MsProjectTaskData.Builder().withTransfered(true).build(); 

But with this approach, the req line from the created new object is lost (of course).

Is it possible to create a new object with the new variable isAlreadyTransfered and with the string "old" req from the "old" object?

Maybe I need to pass the old link to the Builder object, but I don't know how to do this. Maybe using the Builder pattern is not very useful for this approach?

EDIT: (After a comment from Eugene)

Think I understood:

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

It seems to work or something is wrong with the code above? Can I use this approach without consideration?

+4
source share
2 answers

Make the Builder constructor as an argument the “old” object and set everything you want from it to the new one.

Edit

You need to read a little more about the builder pattern in order to better understand what it is and if you really need it.

The general idea is that the Builder pattern is used when you have additional elements. Effective Java Point 2 is your best friend here.

For your class, if you want to create one object from another and use the Builder template at the same time, you

  • Or pass the "old" object in the Builder constructor
  • Create a method from or from Old, etc.

What does it look like? I am going to provide only the first, which you can determine the second at your discretion.

 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 that are REQUIRED must be private final private final String firstname; private final String lastname; //fields that are optional are 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 do you create one object from 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 
+11
source

I would change it to

  public class MsProjectTaskData { private boolean transfered; private String request; public static class Builder { private boolean transfered = false; private String request; public Builder() { // empty } public Builder(MsProjectTaskData old) { this.request(old.request); this.transfered(old.transfered); } public Builder request(String val) { request = val; return this; } public Builder transfered(boolean val) { transfered = val; return this; } public MsProjectTaskData build() { return new MsProjectTaskData(this); } } private MsProjectTaskData(Builder builder) { transfered = builder.transfered; request = builder.request; } } 

And used like that

 MsProjectTaskData data = new MsProjectTaskData.Builder().transfered(true).request("request").build(); MsProjectTaskData changedData = new MsProjectTaskData.Builder(data).transfered(false).request("changeRequest").build(); MsProjectTaskData sameData = new MsProjectTaskData.Builder(data).build(); 

I took the liberty of changing the logical to “transposed”, I feel that it is easier to understand.

0
source

All Articles