使用“旧”的“生成器”模式创建新对象对象引用 [英] Create new object with Builder pattern with "old" object reference

查看:158
本文介绍了使用“旧”的“生成器”模式创建新对象对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩Builder模式,并被困在如何向新创建的对象添加一个新的属性:

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

我可以使用Builder创建一个新对象:

I can create a new object with Builder like this:

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

但是使用这种方法, req 一个新创建的对象丢失(当然)。

But with this approach the req string from a new-created object is lost (of course).

是否有可能使用新设置 isAlreadyTransfered 变量和创建一个新对象,旧的 req 字符串从旧对象?

Is there a possibility to create a new object with the new set isAlreadyTransfered variable and with the "old" req string from a "old" object?

也许我必须通过旧对象引用Builder,但我不知道该怎么做。可能使用Builder模式对于这种方法来说并不太有用吗?

Maybe I have to pass the old object reference to the Builder but I do not know how to do this. Maybe the use of Builder pattern is not really usefull for this approach?

编辑:(在Eugene发表评论后)

想想,我得到了:

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 to work or is something wrong with the code above? Can I use this approach without consideration?

推荐答案

让Builder构造函数以旧对象为参数,并设置任何你想从它到新的。

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

编辑

您需要阅读更多关于构建器模式的信息更好地掌握它是什么,如果你真的需要它。

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.

一般的想法是,当你有可选元素。有效的Java项目2是您最好的朋友。

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

对于您的类,如果要从另一个构建一个对象并同时使用Builder模式, / p>

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


  1. 在Builder构造函数中传递旧对象

  2. 创建一个方法 fromOld 等。

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

您将如何从另一个创建一个对象: / p>

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

这篇关于使用“旧”的“生成器”模式创建新对象对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆