Deep dive on Builder Design Pattern ?

Motivation

  • Since this design pattern is mentioned by my seniors a lot and I did not know how it works and how to use it , I will use this article to read more on this and learn about this design pattern

When to use this pattern ?

  • builder design pattern suggest you extract the object construction code out of its own class and move it to separate objects called builders.
  • builder design pattern is *creational design pattern* like factory pattern and abstract factory pattern.

  • introduced to solve, problem with factory and abstract factory design pattern, when an object contains too many attributes

  • builder design pattern: will solve the issue with a large number of optional parameters and inconsistent state by providing a way to build the object, step by step and provide a method to return the final object.

Steps to create builder class

  1. Builder class will have public constructor, with all the required attributes.

  2. Builder class will have method to set the optional parameters

  3. Builder class will hvae build method which will return the object, of the parent class using this

  4. in builder design pattern, only way to get the object is through the builder class.

    1. this will return the object of the parent class, and it passes builder class as an arguement in the method
  5. private constructer in parent class that will set the values given by the builder class

Future Todo :

Code Examples
package org.example;



public class Computer {

    //required fileds
    private String HDD;
    private String RAM;

    //optional paramters
    private boolean isGraphicsCardEnabled;
    private boolean isBluetoothEnabled;

    public String getHDD() {
        return HDD;
    }

    public String getRAM() {
        return RAM;
    }

    public boolean isGraphicsCardEnabled() {
        return isGraphicsCardEnabled;
    }

    public boolean isBluetoothEnabled() {
        return isBluetoothEnabled;
    }

    private Computer(ComputerBuilder builder) {
        this.HDD=builder.HDD;
        this.RAM=builder.RAM;
        this.isGraphicsCardEnabled=builder.isGraphicsCardEnabled;
        this.isBluetoothEnabled=builder.isBluetoothEnabled;

    }


    //Builder Class
    public static class ComputerBuilder {

        //required fileds
        private String HDD;
        private String RAM;

        //optional paramters
        private boolean isGraphicsCardEnabled;
        private boolean isBluetoothEnabled;

        // this constructor will set the required values
        public ComputerBuilder(String hdd,String ram) {
            this.HDD=hdd;
            this.RAM=ram;
        }

        //methods to set the optional fields
        public ComputerBuilder setGraphicsCardEnabled(boolean isGraphicsCardEnabled) {
            this.isGraphicsCardEnabled=isGraphicsCardEnabled;
            return this;
        }

        //method to set the optional field isBluetoothEnabled
        public ComputerBuilder setBluetoothEnabled(boolean isBluetoothEnabled) {
            this.isBluetoothEnabled=isBluetoothEnabled;
            return this;
        }

        public Computer build() {
            return new Computer(this);
        }




    }

    public static void main(String[] args) {

        Computer comp= new Computer.ComputerBuilder("1tb","8bg").setGraphicsCardEnabled(true).build();

        System.out.println(comp.getHDD());
        System.out.println(comp.getRAM());
        System.out.println(comp.isGraphicsCardEnabled());
        System.out.println(comp.isBluetoothEnabled());


    }



}