Setter AND(不是OR或VS)构建器模式 [英] Setters AND ( not OR or VS ) builder patterns

查看:186
本文介绍了Setter AND(不是OR或VS)构建器模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我使用构建器模式来构造对象。最好的例子是披萨代码

  public class Pizza {
private int size;
私人布尔奶酪;
private boolean pepperoni;
私人布尔培根;

public static class Builder {
//必需
private final int size;

//可选
private boolean cheese = false;
private boolean pepperoni = false;
private boolean bacon = false;

public Builder(int size){
this.size = size;
}

public Builder cheese(布尔值){
cheese = value;
返回此;
}

public Builder pepperoni(布尔值){
pepperoni = value;
返回此;
}

公共生成器培根(布尔值){
bacon = value;
返回此;
}

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

private Pizza(生成器构建器){
size = builder.size;
cheese = builder.cheese;
pepperoni = builder.pepperoni;
bacon = builder.bacon;
}
}

到目前为止一切顺利。



现在让我们假设一个用例,我需要更新 奶酪。这需要 setter 。我从未见过一个构建器模式与setter共存的例子,让我怀疑我所做的是反模式。



setter和构建器可以共存吗?

解决方案

您从未见过使用过,因为大多数时候,构建器模式用于构建不可变对象。 / p>

但我不明白为什么他们不能共存。构建器构建一个对象,并且您希望构建的对象是可变的,然后它可以有setter。但是,如果它是可变的并且具有setter,为什么不使用简单的构造函数构建对象,并调用setter来改变状态?除非只有一两个字段是可变的,否则构建器不再具有实用性。


I have a situation where I use a builder pattern for constructing an object. Best example to give is the pizza code

public class Pizza {
  private int size;
  private boolean cheese;
  private boolean pepperoni;
  private boolean bacon;

  public static class Builder {
    //required
    private final int size;

    //optional
    private boolean cheese = false;
    private boolean pepperoni = false;
    private boolean bacon = false;

    public Builder(int size) {
      this.size = size;
    }

    public Builder cheese(boolean value) {
      cheese = value;
      return this;
    }

    public Builder pepperoni(boolean value) {
      pepperoni = value;
      return this;
    }

    public Builder bacon(boolean value) {
      bacon = value;
      return this;
    }

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

  private Pizza(Builder builder) {
    size = builder.size;
    cheese = builder.cheese;
    pepperoni = builder.pepperoni;
    bacon = builder.bacon;
  }
}

So far so good.

Now lets assume a usecase where I need to update the cheese. That needs a setter. I have never seen a single example where builder patterns coexist with setters, making me suspicious that what I was upto was an anti-pattern.

Can setters AND builders coexist together ?

解决方案

You've never seen that used because most of the time, the builder pattern is used to build an immutable object.

But I don't see why they couldn't coexist. The builder builds an object, and you want the built object to be mutable, then it can have setters. But then, if it is mutable and has setters, why not build the object using a simple constructor, and call setters to change the state? The builder isn't really useful anymore, unless only one or two fields among many are mutable.

这篇关于Setter AND(不是OR或VS)构建器模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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