使用子类中构造函数的Builder模式 [英] use Builder pattern from the constructor in a subclass

查看:131
本文介绍了使用子类中构造函数的Builder模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Builder模式,紧跟维基百科文章中提出的Java实现 Builder模式 http://en.wikipedia.org/wiki/Builder_pattern

I am currently using the Builder pattern, following closely the Java implementation suggested in the Wikipedia article Builder pattern http://en.wikipedia.org/wiki/Builder_pattern

这是一个示例代码,用于说明我的实现

This is a sample code that ilustrates my implementation

public class MyPrimitiveObject {
  private String identifier="unknown";
  public static class Builder {
    private final MyPrimitiveObject obj = new MyPrimitiveObject();
    public MyPrimitiveObject build() { return obj; }
    public Builder setidentifier (String val) {
     obj.identifier = val;
     return this;
    }
  }
  public static Builder createBuilder() { return new Builder(); }
  @Override public String toString() { return "ID: "+identifier; }
}

在我的一些使用这个类的应用程序中,我碰巧找到了类似的构建代码,所以我想在 MySophisticatedObject 中继承 MyPrimitiveObject ,并将所有重复的代码移动到其构造函数中。这是问题所在。

In some of my applications that use this class, I happen to find very similar building code , so I thought to subclass MyPrimitiveObject in MySophisticatedObject and move all my repeated code into its constructor.. and here is the problem.

我如何调用超类构建器并将其返回的对象指定为我的实例?

How may I invoke the superclass Builder and assign its returned object as my instance?

public class MySophisticatedObject extends MyPrimitiveObject {
  private String description;
  public MySophisticatedObject (String someDescription) {
    // this should be the returned object from build() !!
    Builder().setidentifier(generateUUID()).build()
    description = someDescription;
  }     
}


推荐答案

你可能要考虑具有嵌套的 MySophisticatedObject.Builder 延伸 MyPrimitiveObject.Builder ,并覆盖其 build()方法。在构建器中有一个受保护的构造函数,以接受要设置值的实例:

You might want to consider having a nested MySophisticatedObject.Builder which extends MyPrimitiveObject.Builder, and overrides its build() method. Have a protected constructor in the builder to accept the instance on which to set values:

public class MyPrimitiveObject {
  private String identifier="unknown";
  public static class Builder {
    private final MyPrimitiveObject obj;
    public MyPrimitiveObject build() { return obj; }
    public Builder setidentifier (String val) {
     obj.identifier = val;
     return this;
    }

    public Builder() {
        this(new MyPrimitiveObject());
    }

    public Builder(MyPrimitiveObject obj) {
        this.obj = obj;
    }
  }
  ...
}

public class MySophisticatedObject extends MyPrimitiveObject {
  private String description;

  public static class Builder extends MyPrimitiveObject.Builder {
    private final MySophisticatedObject obj;
    public Builder() {
      this(new MySophisticatedObject());
      super.setIdentifier(generateUUID());
    }     
    public Builder(MySophisticatedObject obj) {
      super(obj);
      this.obj = obj;
    }

    public MySophisticatedObject build() {
      return obj;
    }

    // Add code to set the description etc.
  }
}

这篇关于使用子类中构造函数的Builder模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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