The builder pattern

Another way to delegate the task of creating objects to another object is using the builder pattern. This pattern is used to create complex objects that usually require a step-by-step approach.

In the classical definition, this pattern involves the following actors:

  • The client: This is the object that needs a new object
  • The director: This is the actor who knows how to create an object, that is, it knows the necessary steps to get an object built
  • The builder: This actor actually builds the object by providing methods used by the director
  • The product: It is the resulting object built by the builder under the control of the director

In a nutshell, the client asks for a product from the director, who creates it by means of the builder. ...

Get Mastering JavaScript Object-Oriented Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.