Builder Builder中是否需要一个.build()方法? [英] Do we need a .build() method in the Builder Pattern?

查看:537
本文介绍了Builder Builder中是否需要一个.build()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于有效Java中涵盖的Builder模式的问题。我们需要一个 .build()方法才能正确实现模式?例如,假设我们有以下类:

I had a question regarding the "Builder Pattern" covered in "Effective Java". Do we need a .build() method for it to correctly implement the pattern? For instance, let's say that we have the following class:

public class CoffeeDrink {

    private int numEspressoShots;
    private short milkType;
    private boolean withWhip;

    private CoffeeDrink() {
    }

    public static CoffeeDrink buildNewDrink() {
        return new CoffeeDrink();
    }

    public CoffeeDrink withEspresso(int n) {
        this.numEspressoShots = n;
        return this;
    }

    public CoffeeDrink withMilkType(shot t) {
        this.milkType = t;
        return this;
    }

    public CoffeeDrink withWhip() {
        this.withWhip = true;
        return this;
    }
}

然后我们如何使用它:

CoffeeDrink c = CoffeeDrink.buildNewDrink()
                         .withEspresso(2)
                         .withMilkType(2)
                         .withWhip();

如果我没有静态内部 Builder,这仍然有效 class?我猜这个优点之一就是它不能创建一个新的 CoffeeDrink 对象,直到方法 .build()被调用,但是我仍在创建一个 Builder 对象。只是寻求一些澄清。

Would this still be valid if I don't have a static inner Builder class? I guess that one of the advantages is that it holds off from creating a new CoffeeDrink object until the method .build() is called, but I'm still creating a Builder object. Just seeking some clarifications.

推荐答案

不,这不是Builder模式。它是有效的Java,它将编译和运行。但是您的 buildNewDrink()方法,无论是调用 build()还是 buildNewDrink() / code>或其他东西,只是一个简单的Factory Method,它创建一个 CoffeeDrink 。那些其他的方法就像setter方法一样,它们会自动返回。

No, this is not the Builder pattern. It's valid Java, and it will compile and run. But your buildNewDrink() method, whether it's called build() or buildNewDrink() or something else, is just a simple Factory Method that creates a CoffeeDrink. Those other methods are like setter methods that happen to return themselves.

c $ c> static 嵌套的Builder类是必需的。在创建类实例的同时,它可以执行验证逻辑,以确保无法创建无效对象。我不确定对于 CoffeeDrink 的状态是无效的,但如果是,则使用代码,可以创建一个 CoffeeDrink ,并在创建后将其设置为无效状态,但在调用其他方法之前。 Builder模式通过在构建实例之前验证数据消除了这种可能性。它也消除了构建器爆炸的需要,其中需要许多具有所有可能的参数组合的构造函数来覆盖所有可能的情况。

The static nested Builder class is necessary. While holding off on creating the class instance, it can perform validation logic to ensure that an invalid object is not created. I'm not sure that there is an invalid state to a CoffeeDrink as you have it, but if it did, with your code, it would be possible to create a CoffeeDrink and have it in an invalid state after it was created, but before other methods were called. The Builder pattern eliminates this possibility by validating the data before building the instance. It also eliminates the need for constructor explosion, where lots of constructors with all possible combinations of parameters are needed, to cover all possible cases.

这篇关于Builder Builder中是否需要一个.build()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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