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

public class Roof {

    private String type;
    private String color;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Roof{" +
                "type='" + type + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}

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

Class Walls

public class Walls {

    private String type;
    private int amount;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    @Override
    public String toString() {
        return "Walls{" +
                "type='" + type + '\'' +
                ", amount=" + amount +
                '}';
    }
}

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

public class House {

    private Walls walls;
    private Roof roof;

    public Walls getWalls() {
        return walls;
    }

    public void setWalls(Walls walls) {
        this.walls = walls;
    }

    public Roof getRoof() {
        return roof;
    }

    public void setRoof(Roof roof) {
        this.roof = roof;
    }

    @Override
    public String toString() {
        return "House{" +
                "walls=" + walls +
                ", roof=" + roof +
                '}';
    }
}

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

public interface Builder {

    void buildWalls();

    void buildRoof();

    House getHouse();
}

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

Class Director

public class Director {

    private Builder builder;

    Director(Builder builder) {
        this.builder = builder;
    }

    public void buildHouse() {
        builder.buildWalls();
        builder.buildRoof();
    }

    public House getHouse() {
        return this.builder.getHouse();
    }
}

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

public class CommonHouse implements Builder {

    private House commonHouse;

    CommonHouse() {
        this.commonHouse = new House();
    }

    @Override
    public void buildWalls() {
        Walls walls = new Walls();
        walls.setType("Brick");
        walls.setAmount(4);

        commonHouse.setWalls(walls);
    }

    @Override
    public void buildRoof() {
        Roof roof = new Roof();
        roof.setType("ceramic tile");
        roof.setColor("red");

        commonHouse.setRoof(roof);
    }

    @Override
    public House getHouse() {
        return this.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.

public class Client {

    public static void main(String[] args) {

        Builder houseBuilder = new CommonHouse();
        Director houseDirector = new Director(houseBuilder);
        houseDirector.buildHouse();

        House commonHouse = houseDirector.getHouse();

        System.out.println(commonHouse);
    }
}

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.