简体   繁体   English

带有公共构造函数的生成器模式

[英]Builder Pattern, with a public Constructor vaild

I know there are several questions about the builder pattern. 我知道关于构建器模式有几个问题。
- Use builder pattern from the constructor in a subclass - 在子类中使用构造函数中的构建器模式
- When would you use the builder pattern - 什么时候使用构建器模式
- Java improving builder pattern on a specific class -Java改进特定类的构建器模式
- Builder design pattern why do we need a director - 建筑商设计模式为什么我们需要导演

So far I used the builder pattern like descripted in Bloch Item 2. 到目前为止,我使用了Bloch项目2中所述的构建器模式。

Yesterday I changed some small detail. 昨天我更改了一些小细节。 I added a public constructor for the default values. 我为默认值添加了一个公共构造函数。 So you could use both the Builder for a complex object or a constructor for simple object with the necessary values. 因此,您既可以将Builder用于复杂的对象,也可以将构造函数用于具有必要值的简单对象。

public class Blub {
  private final String id;
  public Blub( final String id ) {
    this( Blub.Builder(id) );
  }

  private Blub( Builder builder ) {
    this.id = builder.id; 
  }

  public static class Builder {
    private final String id;
    public Builder( final String id ) {
      this.id = id;
    }

    public Blub build() {
      return new Blub(this);
    }      
  }
}

But I am not sure if this has a design flaw. 但是我不确定这是否存在设计缺陷。 Because if I finalize the class I'm sure there are no disadvantages. 因为如果我完成该课程的确定,那么我肯定没有缺点。 So in the easy case, it would be possible to do call the Blub constructor. 因此,在简单的情况下,可以调用Blub构造函数。

new Blub("1a");

instead of 代替

(new Blub.Builder("1a")).build();

But if I don't finalize the class it would be possible to extend it and do all kind of stuff in the new constructor. 但是,如果我不完成该类的定义,则可以扩展它并在新的构造函数中进行各种操作。 And I am not sure if there are no cases to mess this up now. 而且我不确定现在是否有任何情况可以解决这个问题。 Any idea? 任何想法?

If you're going to use a Builder (or Factory pattern for that matter), it is usually counter-intuitive to have parameterized constructors as well (unless they are dependencies and you are using IoC). 如果要使用构建器(或者使用Factory模式),则通常也要使用参数化的构造函数(除非它们是依赖项,并且您正在使用IoC)是违反直觉的。 You are essentially breaking the orthogonality and DRY principle. 您实际上在打破正交性和DRY原理。 Builder and Factory patterns are usually used to solve construction challenges where creating an instance is non-trivial or otherwise error prone. 在创建实例非常重要或容易出错的情况下,通常使用Builder和Factory模式来解决构造难题。

In your case, this does not seem to be. 您的情况似乎并非如此。

Is it a design flaw? 这是设计缺陷吗? No. Can it potentially cause the API to become confusing? 否。是否可能导致API变得混乱? Absolutely. 绝对。 Particularly over time, as more code divergers and starts using either method A and B. Are you going to explain to new developers when they should use method A for creating objects or when they should use method B? 特别是随着时间的流逝,随着越来越多的代码出现分歧,并开始使用方法A和B。您是否要向新开发人员说明何时应使用方法A创建对象或何时应使用方法B?

One thing that I often do is provide a builder AND static factory methods to build the common simple cases. 我经常做的一件事是提供一个生成器和静态工厂方法来构建常见的简单案例。 This way you can still have a private constructor and get intent revealing clean ways to construct the default cases. 这样,您仍然可以拥有私有构造函数,并有意揭示构造默认案例的简洁方法。

Blub blub = Blub.createWithId("id");

This also lets you add several static factory methods without having to have multiple constructors or constructors with confusing parameters. 这也使您可以添加几个静态工厂方法,而不必具有多个构造函数或参数混乱的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 生成器模式,但是对象构造函数中的生成器 - Builder pattern but builder in objects constructor 有效 Java。 可序列化生成器模式(如何添加公共无参数构造函数?) - Effective Java. Serializable Builder pattern (how to add public no-arg constructor?) 构建器模式(java)。 为什么 Builder 类是公开的? - Builder pattern (java). Why is Builder class public? 在子类的构造函数中使用构建器模式 - use Builder pattern from the constructor in a subclass 将默认的公共构造函数添加到生成的生成器内部类中 - Add default public constructor to generated builder inner class 是否可以扩展使用Builder模式和private构造函数的类? - It is possible to extend classes that uses Builder pattern and private constructor? 在 spring 构造函数中注入使用构建器模式的对象 - Injecting an object that uses builder pattern inside spring constructor 不明确的构造函数(java 7)......围绕它的唯一方法是构建器模式,对吗? - Ambiguous constructor (java 7)… the only way around this is a Builder pattern, right? 在我的生成器模式中找不到匹配的构造函数错误 - Could not find matching constructor error in my Builder Pattern Java Pattern类没有公共构造函数,为什么? - Java Pattern class doesn't have a public constructor, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM