Builder Pattern

The builder pattern is a creational design pattern that allows for the construction of complex objects step by step. It separates the construction of the object from its representation, making it easier to create different representations of the object without altering its construction algorithm.

Implementation

The builder pattern typically involves the following components:

  • Builder Class: A separate class is introduced to encapsulate the construction process. This class contains methods for setting various properties or configuring the object’s attributes.
  • Product Class: The class representing the complex object to be constructed. It serves as the blueprint for the object, defining its properties and methods.
  • Build Method: The builder class provides a build() method that returns the constructed object. This method involves invoking the various configuration methods to set the object’s properties and attributes.
  • Fluent Interface: The builder pattern aims to provide a fluent interface, where the configuration process is expressed in a natural and readable manner. This is achieved by returning the builder object itself from each configuration method, allowing for method chaining.

Benefits

  • Flexible Object Creation: The builder pattern promotes flexibility in creating different configurations of the object, allowing for more options without modifying the underlying logic.

  • Readable and Expressive Code: The fluent interface of the builder pattern simplifies the construction process and makes the code more readable and expressive.

  • Easier Debugging and Testing: The clear separation of construction from representation makes it easier to debug and test object creation.

  • Reduced Boilerplate Code: The builder pattern helps reduce boilerplate code by encapsulating repeated configuration tasks in the builder class.

 

Examples of the Builder Pattern

 

public class House {
    // Private variables
    private String walls;
    private String floors;
    private String rooms;
    private String roof;
    private String windows;
    private String doors;
    private String garage;

    // Private constructor,
    // Get the representation object from the inner class
    private House(HouseBuilder houseBuilder) {
        this.walls = houseBuilder.walls;
        this.floors = houseBuilder.floors;
        this.rooms = houseBuilder.rooms;
        this.roof = houseBuilder.roof;
        this.windows = houseBuilder.windows;
        this.doors = houseBuilder.doors;
        this.garage = houseBuilder.garage;
    }

    @Override
    public String toString() {
        return "House{" +
                "walls='" + walls + '\'' +
                ", floors='" + floors + '\'' +
                ", rooms='" + rooms + '\'' +
                ", roof='" + roof + '\'' +
                ", windows='" + windows + '\'' +
                ", doors='" + doors + '\'' +
                ", garage='" + garage + '\'' +
                '}';
    }

    // Inner class for create object
    public static class HouseBuilder {

        // Private variables
        private String walls;
        private String floors;
        private String rooms;
        private String roof;
        private String windows;
        private String doors;
        private String garage;

        // Public methods for creating a specific class.
        // The full class object is returned each time,
        // which makes it possible to use these methods in a very flexible way.
        public HouseBuilder buildWalls(String walls) {
            this.walls = walls;
            return this;
        }

        public HouseBuilder buildFloors(String floors) {
            this.floors = floors;
            return this;
        }

        public HouseBuilder buildRooms(String rooms) {
            this.rooms = rooms;
            return this;
        }

        public HouseBuilder buildRoof(String roof) {
            this.roof = roof;
            return this;
        }

        public HouseBuilder buildWindows(String windows) {
            this.windows = windows;
            return this;
        }

        public HouseBuilder buildDoors(String doors) {
            this.doors = doors;
            return this;
        }

        public HouseBuilder buildGarage(String garage) {
            this.garage = garage;
            return this;
        }
        
        // Public method build() returning completed representation of the Class
        public House build() {
            return new House(this);
        }
    }
}

 

Example of creating an object

 

   public static void main(String[] args) {

        House house = new House.HouseBuilder()
                .buildWalls("walls")
                .buildFloors("floors")
                .buildRoof("roof")
                .buildRooms("rooms")
                .build();
        System.out.println(house);
    }

Leave a Comment

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