Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use builder pattern?

In most tutorials (Judith Bishops' book, or here), I see example similar to the one below.

If the builder pattern means to create method Construct in Director class, which performs set of Builder childs operations...

class Director {
  public static void Construct(Builder builder) {
    builder.BuildPartA();
    builder.BuildPartB();
    ...
  }
}

abstract class Builder {
  public abstract void BuildPartA();
  public abstract void BuildPartB();
  ...
}

class Builder1 : Builder { ... }
class Builder2 : Builder { ... }

void Main() {
  Builder1 b1 = new Builder1();
  Builder2 b2 = new Builder2();
  Director.Construct(b1);
  Director.Construct(b2);
}

...why don't we just move the Construct method to the Builder class?

class Builder {
  public virtual void BuildPartA();
  public virtual void BuildPartB();
  public void Construct() {
    BuildPartA();
    BuildPartB();
    ...
  }
  ...
}

class Builder1 : Builder { ... }
class Builder2 : Builder { ... }

void Main() {
  Builder1 b1 = new Builder1();
  Builder2 b2 = new Builder2();
  b1.Construct();
  b2.Construct();
}

Please show me some example where the builder pattern is really helpful.

like image 292
Jan Turoň Avatar asked Feb 18 '12 22:02

Jan Turoň


People also ask

Why do we use the builder pattern?

The intent of the Builder design pattern is to separate the construction of a complex object from its representation. By doing so, the same construction process can create different representations.

Should we use builder pattern?

Builder Pattern: When to Use This pattern should be used: When it's necessary to use a constructor with a long parameter list or when there's a long list of constructors with different parameters. When it's necessary to build different representations of the same object.

Why should I use builder in Java?

The builder pattern simplifies the creation of objects. It also simplifies the code as your do not have to call a complex constructor or call several setter methods on the created object. The builder pattern can be used to create an immutable class.

What is the use of builder design pattern in Java?

Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object.


1 Answers

The Directory is supposed to know the correct sequence of assembling different components to construct the objects. I believe it is simply to separate the concern of knowing the order or method of constructing these objects from the base Builder class (which is simply a base class). If you moved the construct into the Builder base it would resemble the template method pattern.

It is better to have a separate director that knows how to assemble the components because in the even that the "recipe" of constructing the components changes, the base class does not need to be changed. For example, let's say a certain class of components need to be built by executing 3 steps in a certain order:

  1. Step A
  2. Step B
  3. Step C

Let's say at some point another component is added to the family which can be built with the same steps but in a different order:

  1. Step A
  2. Step C
  3. Step B

In this case, if the logic for the sequence is separated in the Director as opposed to the base class Builder, one can inherit a new director and use that to construct. If the logic was in the base Builder, the base class, which may be a part of a separate library or JAR or a header file in case of C++, it may have required recompilation of the concrete classes or at least shipping a new JAR.

I'm sure there are more advantages of separating such a concern.

like image 79
Sid Avatar answered Oct 08 '22 23:10

Sid