Java Design Patterns – Builder Pattern

Builder Pattern is the type of creational design patterns. It means that this pattern deal with object creation. The name Builder tells us that we have structure used to create and build something.

In this post, you can find:

  • example situation when to use Builder design pattern
  • Builder pattern code structure in Java
  • example how to use Builder

Exemplary Situation

Builder Pattern helps us when we have some objects to create and build it into one bigger piece. Let’s look at that situation. We have a house. House is built with roof and walls. The roof is built from e.g. ceramic tile and have some attributes like a color. The house has walls. We would want to define how many walls the house would have and what type of building block was used to build walls.
Let’s change our example into code.

Builder pattern example in Java

We would have the class like Roof and Walls. The Hause class would have variables with the type of Roof and Walls to join these objects into one class. We will need Builder interface and Director that will help us with the simplifying process of building object. That’s the Builder design pattern core.

First class Roof

As you can see, we have two variables type and color. Thankfully to IntelliJ setters, getters, and toString() method were generated automatically.

Class Walls

The code structure is the same as in Roof class. You can add more methods to the class or another class based on this structure on your own.

Class Hause

Here, we use classes created previously to set the type to variables. If you previously added more classes add them here now. This class joins all object into one.

Interface Builder

That’s simple interface with methods that we will implement in all class that would build House object.

Class Director

This class is created to manage Builder’s objects creation. We will use these methods to set and get an instance of House.
It’s time for first object builder via our Builder pattern.

Class CommonHouse

We implement Builder interface and override methods with our implementation. We creating objects like Walls or Roof. On this objects, we are using setters to set attributes. Then prepared object we set to the variable with the instance of House.

How to use Builder Pattern?

Here it is the Client class.

Console output:

First, we have the variable that is the instance of Builder and we initialize class CommonHouse on it. Then, we use our Director and we giving our houseBuilder as the parameter to the constructor. Now, we create the object via buildHouse() method. Finally, we can get the object and e.g. print it via toString() method.

You can find code used in this article on my GitHub: Builder

That’s it! We have Builder Pattern implement in Java. You can add a new class with another style of a house also add new attributes to entities Walls, Roof or House. Write an implementation of Builder on your own. Create your own example and implement it to code. That’s the best way to learn!

Good luck!

 

Want more? Look at my post about Abstract Factory Pattern.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.