Builder Design Pattern

From JholJhapata
Revision as of 17:45, 19 October 2020 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Builder pattern aims to "Separate the construction of a complex object". It was was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes.

In other words, it is used to construct a complex object step by step and the final step will return the object. The process of constructing an object should be generic so that it can be used to create different representations of the same object.

Solution

Code Example

Advantages

  • Code is more maintainable and readable.
  • Less chances of error as we have distributed a complex object to few simple objects.

Disadvantages

Number of lines of code increases in builder pattern, but it makes sense as the effort pays off in terms of maintainability and readability.

Conclusion

If your object has only a few constructor arguments, it makes no sense to use the builder pattern.