建造者模式是否可以做得太多? [英] Can the builder pattern ever be doing too much?

查看:144
本文介绍了建造者模式是否可以做得太多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在研究小组研究设计模式,并且已经明白,构建器模式对于创建由许多(可能可选的)零件组成的复杂对象非常有用。



但是,建筑师做得太多了吗?假设我们有一个类具有许多不同的对象组合,还有另外一种可能更适合的模式,而不是使几十种不同的构建器?不完全具体的建设者可以减少您需要的建设者数量吗?



我的研究组和我不断回来的例子是汽车制造商如汽车公司的网站。任何汽车公司都有数十辆汽车,每种汽车都有许多不同的功能,颜色,附加功能等。我明白的方式是,您的制造商应该具体到您正在制作的确切对象,因此在此示例中应用构建器模式将产生数百看起来像RedSUVWithSunroofBuilder,BlueSUVWithSunroofBuilder,RedSUVBuilder等等的构建器。



是否有任何理由使用构建器模式,我无法传入的这些价值观,以减少我需要创建的建设者的数量?例如,代替使用RedSUVWithSunroofBuilder或BlueSUVWithSunroofBuilder,仍然适合构建器模式来执行SUVWithSunroofBuilder(Red)和SUVWithSunroofBuilder(Blue),还是更适合不同的模式?

解决方案

构建器模式当然是自由裁量的,如果它过于复杂,那么它过于复杂,你可能想要考虑一种不同的方法来创建对象,如工厂模式。我认为有几种情况下,生成器模式优于




  • 创建具有多个有效配置的对象

    • 我认为你的车是一个很好的例子


  • 创建不可变对象,其中不需要提供所有必需的数据前面的



以下是您如何实施汽车制造商的一个例子:

  public class Car {
private final boolean hasSunroof;
私人最终颜色;
private final int horsePower;
private final String modelName;

private Car(Color color,int horsePower,String modelName,boolean hasSunroof){
this.color = color;
this.horsePower = horsePower;
this.hasSunroof = hasSunroof;
this.modelName = modelName;
}

public static Builder生成器(颜色,int马力){
return new Builder(color,horsePower);
}

public static class Builder {
private final颜色颜色;
private final int horsePower;
private boolean hasSunroof;
private String modelName =unknown;

public Builder(颜色,int马力){
this.color = color;
this.horsePower = horsePower;
}

public Builder withSunroof(){
hasSunroof = true;
返回这个;
}

public Builder modelName(String modelName){
this.modelName = modelName;
返回这个;
}

public Car createCar(){
return new Car(color,horsePower,modelName,hasSunroof);
}
}
}

Builder没有作为一个嵌套类,但它允许你隐藏你的构造函数的人谁可能滥用您的API。还要注意,必须提供最小必需参数以创建构建器。您可以这样使用此构建器:

  Car car = Car.builder(Color.WHITE,500).withSunroof()。 MODELNAME( 野马)createCar(); 


I've been studying design patterns with a study group recently, and have come to understand that the builder pattern can be very useful for creating complex objects that are made up of many (potentially optional) parts.

However, is there ever a point where the builder is doing too much? Let's say we have a class that has many many different combinations of objects, is there another pattern that may be better suited for that instead of making dozens of different builders? Is it possible to reduce the number of builders you need by not making completely specific builders?

The example my study group and I kept coming back to was a car builder, such as on a car company's website. Any car company has dozens of cars, each with many different features, colors, extras etc. The way I understand it, your builder should be specific to the exact object you are making, so applying a builder pattern to this example would yield hundreds of builders that look like "RedSUVWithSunroofBuilder", "BlueSUVWithSunroofBuilder", "RedSUVBuilder" etc.

Is there any reason that, using the builder pattern, I couldn't pass in some of these values to reduce the number of builders that I would need to create? For example, instead of having RedSUVWithSunroofBuilder, or BlueSUVWithSunroofBuilder, is it still fitting of the builder pattern to do SUVWithSunroofBuilder("Red") and SUVWithSunroofBuilder("Blue"), or is this more fitting of a different pattern?

解决方案

The builder pattern is certainly discretionary, if it is overly complicated then it is overly complicated, and you may want to consider a different way to create objects, like the factory pattern. I think there are a few scenarios where the builder pattern excels:

  • Creating objects that have several valid configurations
    • I think your car thing is a great example
  • Creating immutable objects where not all required data can be supplied up front
    • For a great example of this check out the guava immutable collection builders for instance ImmutableSet.Builder

Here's one example of how you could implement a car builder:

public class Car {
    private final boolean hasSunroof;
    private final Color color;
    private final int horsePower;
    private final String modelName;

    private Car(Color color, int horsePower, String modelName, boolean hasSunroof) {
        this.color = color;
        this.horsePower = horsePower;
        this.hasSunroof = hasSunroof;
        this.modelName = modelName;
    }

    public static Builder builder(Color color, int horsePower) {
        return new Builder(color, horsePower);
    }

    public static class Builder {
        private final Color color;
        private final int horsePower;
        private boolean hasSunroof;
        private String modelName = "unknown";

        public Builder(Color color, int horsePower) {
            this.color = color;
            this.horsePower = horsePower;
        }

        public Builder withSunroof() {
            hasSunroof = true;
            return this;
        }

        public Builder modelName(String modelName) {
            this.modelName = modelName;
            return this;
        }

        public Car createCar() {
            return new Car(color, horsePower, modelName, hasSunroof);
        }
    }
}

The Builder doesn't have to be a nested class, but it does allow you to hide your constructors from people who might misuse your API. Also notice that the minimum required parameters must be supplied to even create a builder. You could use this builder like so:

Car car = Car.builder(Color.WHITE, 500).withSunroof().modelName("Mustang").createCar();

这篇关于建造者模式是否可以做得太多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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