Skip to content
APPCITORThe Tech Blog
Esc

Creational – Builder Design Pattern

July 29, 2020 1 min read

Creational – Builder Design Pattern

Builder Pattern is a creational design Pattern that used to create objects made from bunch of other objects. The advantages of builder pattern are,

  • You can see clear separation between construction and representation of an object.
  • Provides better control over construction process.
  • Supports to change the internal representation of objects.

To create a program of Builder Design Pattern, you need to follow some steps. Let’s see them with a real world example.

First create an interface called “CreateRobot”. Inside this defines all the methods a robot must have.

Then create the abstract class “Robot” which will implement the interface “CreateRobot”.

Next you should create the interface called “RobotBuilder” that defines what every robot has.

What is the next class? Next class is the concrete builder class that sets fields for robots built using specific Specification. So create the class “OldRobotBuilder” which will implement the interface “RobotBuilder”.

Then you should create the class “RobotMaker” that creates a robot with the fields set by the builder.

At last create the main class and build the robot by using the features of OldRobotBuilder. Those should be taken by the RobotMaker and RobotMaker will make the robot and give it to you.

Here is the output.

But keep in mind Builder Design Pattern should be used when object can’t be created in a single step like in the de-serialization of a complex object.

Thank you.

Share this article

Keep reading