The Significance of the Builder Design Pattern in Java

The Significance of the Builder Design Pattern in Java

(Get your basics strong with Simplified explanation along with an example)

Below we have Printer as Interface and 3 implementations of that interface.

  • LaserPrinter
  • InkJetPrinter
  • DotMatrixPrinter

public class Printer {
    private String printType;
    private String resolution;
    private String speed;
    private boolean color;

    public Printer(String printType, String resolution, String speed, boolean color) {
        this.printType = printType;
        this.resolution = resolution;
        this.speed = speed;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Printer{" +
                "printType='" + printType + '\'' +
                ", resolution='" + resolution + '\'' +
                ", speed='" + speed + '\'' +
                ", color=" + color +
                '}';
    }

    public static class PrinterBuilder {
        private String printType;
        private String resolution;
        private String speed;
        private boolean color;

        public PrinterBuilder(String printType) {
            this.printType = printType;
        }

        public PrinterBuilder setResolution(String resolution) {
            this.resolution = resolution;
            return this;
        }

        public PrinterBuilder setSpeed(String speed) {
            this.speed = speed;
            return this;
        }

        public PrinterBuilder setColor(boolean color) {
            this.color = color;
            return this;
        }

        public Printer build() {
            return new Printer(printType, resolution, speed, color);
        }
    }

    public static void main(String[] args) {
        Printer laserPrinter = new Printer.PrinterBuilder("Laser")
                .setResolution("1200 dpi")
                .setSpeed("30 ppm")
                .setColor(true)
                .build();
        System.out.println(laserPrinter);
    }
}        

The Builder Pattern in the Printer class offers:

  1. Flexible Construction: It allows creating Printer objects step by step, adjusting attributes like resolution, speed, and color easily.
  2. Readability and Maintenance: The PrinterBuilder class simplifies object creation, making it clear what attributes are being set with each method call.
  3. Avoids Constructor Overload: Instead of having multiple constructors for different configurations, the Builder Pattern streamlines object creation without excessive code duplication.
  4. Supports Immutability: Printer objects are immutable, meaning their state cannot change after creation. The Builder Pattern ensures this by constructing objects step by step and returning fully initialized instances.

Let's say you want to create an object but, the constructor takes 5 parameters, and you only want to pass 3 and skip the remaining 2 and let that 2 be taken default, Builder pattern kicks in there.


要查看或添加评论,请登录

Pradeep H R的更多文章

社区洞察