Design Patterns: Builder

Builder pattern, similar to Factory pattern, is a creational design pattern. While there are other established Java design pattern categories such as structural and behavioral, I will be mainly covering creational design patterns for now.

Builder patterns are used to create objects made from other objects, being independent of the main object and also to hide creation of parts from the client, the builder knows all specifics and other classes involved know nothing about what’s going on.

This has several advantages:

  • It provides clear separation between the construction and representation of an object.
  • It provides better control over construction process.
  • It supports to change the internal representation of objects.
In this JavaTPoint example, BuilderDemo at runtime calls CDBuilder to Build a CD through the main method.

Unlike Factory, Builder is only truly needed when an object cannot be produced in one step.  Being less commonly used however does not make this an inferior design pattern as every design pattern is best for different programming needs. Use them as you see fit!

Follow the original code on my GitHub profile!

Leave a comment

Design a site like this with WordPress.com
Get started