Create new object with Builder pattern with “old” object reference

允我心安 提交于 2019-12-03 08:35:40

Make the Builder constructor take as an argument the "old" object and set whatever you want from it to the new one.

Edit

You need to read a bit more about the builder pattern to get a better grasp at what it is and if you really need it.

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

For your class, if you want to build one object from another and use a Builder pattern at the same time, you

  1. Either pass the "old" object in the Builder constructor
  2. Create a method from or fromOld, etc.

So how does that looks like? I am going to provide only the first one you can figure out the second on your 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 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 you will 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

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 this

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 to change the boolean to "transfered", I feel it is easier to understand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!