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

前端 未结 1 407
你的背包
你的背包 2021-02-09 02:30

I am playing around with the Builder pattern and get stuck how to add a new \"property\" to a new-created object:

public class MsProjectTaskData {
  private bool         


        
相关标签:
1条回答
  • 2021-02-09 03:19

    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
    
    0 讨论(0)
提交回复
热议问题