Sam
Sam

Reputation: 6900

Ambiguity at Builder Design Pattern

I have several questions on the Builder Pattern. Builder Pattern uses several methods for constructing an instance of a class and each method return this as return value.

My questions are:

  1. Why does each method return a return type instance of same class?
  2. What is the benefit of this pattern vs using setter method?

Upvotes: 0

Views: 96

Answers (3)

user4961365
user4961365

Reputation:

Builder pattern similare to Factory Pattern but use for complex construction. This means you set a factory interface an related implementation but in builder pattern this Structure use for complex construction.

Upvotes: 1

Andy Turner
Andy Turner

Reputation: 140494

  1. Why does each method return a return type instance of same class?

This is not strictly necessary. You could return something else (or void). Returning the Builder makes it a fluent builder, meaning that you can chain method calls conveniently, perhaps avoiding the need for an explicit reference to the builder:

Built thing = new Built.Builder().setFoo("bar").build();

Vs

Builder builder = new Builder();
builder.setFoo("bar");
Built built = builder.build();

Note that each chained call pushes another call onto the stack, so you are limited in the number of chained calls that you can make - although you are practically unlikely to hit this issue.

Also, making the Builder fluent does create more boilerplate code - having to return this at the end of every method would be annoying to do manually if you were creating a large number of setter methods.

  1. What is the benefit of this pattern vs using setter method?

One benefit is the potential for immutability of the built object, and all of the desirable properties that provides (safe sharing of the instance with untrusted clients, no need for cloning, thread safety etc).

For example:

final class Built {
  private final String foo;

  private Built(Builder builder) {
    this.foo = builder.foo;
  }

  String getFoo() { return foo; }

  static final class Builder {
    private String foo;

    Built build() { return new Built(this); }
    Builder setFoo(String value) { this.foo = value; return this; }
  }
}

Instances of Built are immutable because there is no mutable state and inheritance is forbidden. This means that if I give you an instance of Built (or you give me an instance), then I know that you can't do something to change it subsequently - it is not necessary to reason about state changes.

Upvotes: 1

sprinter
sprinter

Reputation: 27976

You can implement the builder pattern without having the methods return this. But the advantage of having the methods return an instance of the same class is that you can chain the methods together. For example:

return new ObjectBuilder().withWidget("A").withGadget(5).build();

The primary reason to use the pattern (over setters on a constructed object) is to separate the logic for building an object from the object itself. Typically this means that the built object has no public setters which simplifies it considerably. However it might need either very complex constructors or protected setters to support the builder.

Upvotes: 1

Related Questions