内部与外部类的Builder模式? [英] Builder Pattern inside vs outside class?

查看:124
本文介绍了内部与外部类的Builder模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在课堂外使用构建器模式有什么好处?

What are the advantages between using the builder pattern within vs outside the class?

内部课程:

public class Person {
    private String name;
    private String eyeColor;
    private String hairColor;

    public Person setName(String name) {
        this.name = name;
        return this;
    }

    public Person setEyeColor(String eyeColor) {
        this.eyeColor = eyeColor;
        return this;
    }

    public Person setHairColor(String hairColor) {
        this.hairColor = hairColor;
        return this;
    }
} 

// Example usage:
Person p = new Person()
                 .setName("Bob")
                 .setHairColor("Black")
                 .setEyeColor("Brown")

课外:

public class Person {
    private String name;
    private String eyeColor;
    private String hairColor;

    public Person(String name, String eyeColor, String hairColor) {
        this.name = name;
        this.eyeColor = eyeColor;
        this.hairColor = hairColor; 
    }  
} 

public class PersonBuilder {
    private String name;
    private String eyeColor;
    private String hairColor;

    public Person build() {
        return new Person(name, eyeColor, hairColor);
    }

    public PersonBuilder with(String name) {
        this.name = name;
        return this;
    }

    public PersonBuilder setEyeColor(String eyeColor) {
        this.eyeColor = eyeColor;
        return this;
    }

    public PersonBuilder setHairColor(String hairColor) {
        this.hairColor = hairColor;
        return this;
    }
} 

// Example usage:
Person p = new PersonBuilder()
                 .setName("Bob")
                 .setHairColor("Black")
                 .setEyeColor("Brown")
                 .build();


推荐答案

取决于。

我经常将 Builder Factory 模式结合起来,因此将Builder作为一个模型是有意义的外部类,因为构建对象可能是许多实现之一。这有一个额外的好处,允许实现是包私有或甚至是Builder的私有内部类。

I often combine Builder and Factory patterns so it makes sense to have the Builder as an outside class since the "built" object could be one of many implementations. This has an added benefit of allowing the implementations to be package private or even private inner classes of the Builder.

如果你使用Builder来创建一个构造函数公共阶级更多的意图揭示或更少混乱,而不是真正的个人偏好。人们可以权衡在API中增加一个类的优缺点,这可能会让用户感到困惑。

If you are using the Builder to just make the constructor of a public class more intent revealing or less cluttered than it is really personal preference. One could weigh the pros and cons of having an additional class in the API which could make it confusing for users.

但是,我建议选择其中一个选项并坚持使用它贯穿整个API。如果所有构建器都是内部类或外部类,它使整个系统架构更容易理解;不要混用它们。

However, I recommend picking one of the options and sticking with it throughout the API. It makes the overall system architecture easier to understand if all Builders are either inner classes or outer classes; don't mix them.

不要像这样混合内部和外部建造者:

Don't mix inner and Outer Builders like this:

Foo foo = new Foo.Builder(...)

Bar bar = new BarBuilder(...)

这篇关于内部与外部类的Builder模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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