Summary of What I learned in Design Pattern: Builder

What is Builder Design Pattern?

The Builder is a creational design pattern that helps produce complex objects using generic mini objects in the step-by-step building process. And, It also gives the flexibility to re-use the created objects for constructing different types of representations. Once there’s a complex procedure involved in creating an object. The builder pattern is ideal to use.

Use-case Scenario.

In analogy, assume that we are building our personal computers. So, to have that, we need to buy or gather the components of a computer like RAM, Motherboard, Processor, Etc. Then, assemble it, we can now have our PC.

In correspondence to programming, we have to make objects instances for RAM, Motherboard, Processor, Hard drive, Etc. Afterward, We will use those instances and make a step-by-step procedure. For, assembling computer parts to have our personal computer. It’s also possible to re-use those instances for other computers. That’s the idea of the Builder pattern.

UML Class Diagram.

Participants.

  • Builder — specifies an abstract interface for creating parts of a Product object.
  • ConcreteBuilder — constructs and assembles parts of the product by implementing the Builder interface, keeps track of the representation it creates, and It provides an interface for retrieving the product.
  • Director — constructs an object using the Builder interface.
  • Product — represents the complex object under construction.
  • ConcreteBuilder — builds the product’s internal representation and defines the process by which it’s assembled and It includes classes that define the constituent parts, including interfaces for assembling the parts into the final result.

Code in C#.

Kindly take a look at the example code.

Advantages.

  • You can construct objects step-by-step, defer construction steps or run steps recursively.
  • You can reuse the same construction code when building various representations of products.
  • Single Responsibility Principle. You can isolate complex construction code from the business logic of the product.

Disadvantage.

  • The overall complexity of the code increases since the pattern requires creating multiple new classes.

When to use Builder Pattern?

  • Use the Builder pattern to get rid of a “telescopic constructor”.
  • Use the Builder pattern when you want your code to be able to create different representations of some product.
  • Use the Builder to construct Composite trees or other complex objects.

Conclusion.

In summary, The builder pattern is a great design to implement because It focuses on creating complex objects. And, let us decouple the process of building a complex object. Thus, It also isolates the code for construction and representation. Therefore, It leads to good benefits like efficiency, reusable, scalable, and ease of maintenance.

Author: Marc Kenneth Lomio | Senior Software Engineer | BlastAsia, Inc.

References:

--

--

Marc Kenneth Lomio & Melrose Mejidana

Software engineers that shares knowledge and experience through this medium.