Learn Builder Pattern through JAVA


1. What is builder pattern?
Builder pattern is one of commonly using design patterns to solve the problems in software development.
2. Where we can use it?
Its very simple. If you need to build the Same type objects with different options you can use the builder pattern. Example – Going to build a Computer, You can have multiple options to build the Computer.
1. Computer 1 – Mother Board, HD, RAM, Power supplier.
2. Computer 2 – Mother Board, HD, RAM, Power supplier, CD ROM, Secondary HD.
3. Computer 3 – Mother Board, HD, RAM, Power supplier, DVD ROM, Keyboard, Mouse.

Lets look at the construction of Computer object without using the builder pattern.

public class Computer {

    private String motherBoard;
    private String ram;
    private String hd;
    private String powerSupplier;

    //Optional fields
    private String cdrom;
    private String secondaryHd;
    private String keyboard;
    private String mouse;

    public Computer(String motherBoard, String ram, String hd, String powersupplier, 
            String cdrom, String secondaryHd, String keyboard, String mouse) {

    }

    public String getMotherBoard() {
        return motherBoard;
    }

    public String getRam() {
        return ram;
    }

    public String getHd() {
        return hd;
    }

    public String getPowerSupplier() {
        return powerSupplier;
    }

    public String getCdrom() {
        return cdrom;
    }

    public String getSecondaryHd() {
        return secondaryHd;
    }

    public String getKeyboard() {
        return keyboard;
    }

    public String getMouse() {
        return mouse;
    }

}

Problems in the above code snippet:
1. When you need to construct the object you have to pass the parameters in correct order in to the constructor. When the parameters count is high there is a chance to get swap the arguments.
2. Even some of the arguments are optional, you have to pass the argument as NULL.
3. One day if you need to add new parameter in to the constructor you have more code changes.

All the above problems are going to be solved with the builder pattern. Lets look at the builder pattern implementation.
1. You need to create static nested class and copy all the arguments to the builder class.
2. The Builder class should have a public constructor with all the required attributes as parameters as well as setters for the optional parameters and return the same builder object.
3. build() method in the builder class that will return the expected Object.

public class Computer {

    
    private String motherBoard;
    private String ram;
    private String hd; 
    private String powerSupplier;

    //Optional fields
    private String cdrom;
    private String dvdrom;
    private String secondaryHd;
    private String keyboard;
    private String mouse;

    private Computer(ComputerBuilder builder) {
        this.motherBoard = builder.motherBoard;
        this.ram = builder.ram;
        this.hd = builder.hd;
        this.powerSupplier = builder.powerSupplier;
        
        this.cdrom = builder.cdrom;
        this.secondaryHd = builder.secondaryHd;
        this.keyboard = builder.keyboard;
        this.mouse = builder.mouse;
    }

    public String getMotherBoard() {
        return motherBoard;
    }

    public String getRam() {
        return ram;
    }

    public String getHd() {
        return hd;
    }

    public String getPowerSupplier() {
        return powerSupplier;
    }

    public String getCdrom() {
        return cdrom;
    }

    public String getSecondaryHd() {
        return secondaryHd;
    }

    public String getKeyboard() {
        return keyboard;
    }

    public String getMouse() {
        return mouse;
    }

    public void setDvdrom(String dvdrom) {
        this.dvdrom = dvdrom;
    }

    public static class ComputerBuilder {

        private String motherBoard;
        private String ram;
        private String hd;
        private String powerSupplier;

        //Optional fields
        private String cdrom;
        private String dvdrom;
        private String secondaryHd;
        private String keyboard;
        private String mouse;

        public ComputerBuilder(String mb, String ram, String hd, String pwSupplier) {
            this.motherBoard = mb;
            this.ram = ram;
            this.hd = hd;
            this.powerSupplier = pwSupplier;
        }
        
        public ComputerBuilder setCDRom(String cdrom){
            this.cdrom = cdrom;
            return this;
        }
        
         public ComputerBuilder setDVDRom(String dvdrom){
            this.dvdrom = dvdrom;
            return this;
        }
        
        public ComputerBuilder setSecondaryHD(String secondaryHd){
            this.secondaryHd = secondaryHd;
            return this;
        }
        
        public ComputerBuilder setKeyboard(String keyboard){
            this.keyboard = keyboard;
            return this;
        }
        
        public ComputerBuilder setMouse(String mouse){
            this.mouse = mouse;
            return this;
        }
        
        public Computer build(){
            return new Computer(this);
        }
        
    }

}

Now build above explained three types of Computers through the builder class as follows.

    public static void main(String[] args) {
        Computer computer1 = new ComputerBuilder("M", "ram", "hd", "pw").build();
        Computer computer2 = new ComputerBuilder("M", "ram", "hd", "pw")
                .setCDRom("cdrom").setSecondaryHD("hd").build();
        Computer computer3 = new ComputerBuilder("M", "ram", "hd", "pw")
                .setDVDRom("dvd").setKeyboard("keyboard").setMouse("mouse").build();
        
    }

Computer class has not contain the public constructor so the only way is to construct the object is through the ComputerBuilder class.

You can download the complete source form here

Blog at WordPress.com.

Up ↑