Imagine that you have shop where your Customers could buy Laptops with configuration they prefer. You need to develop system which will allow you easily build any configuration of Laptop for any User.
How could you easily accomplish this?

BUILDER

Builder is Creational Design Patter which allows you build some whole Product (Laptop) by constructing together some parts like Processor, Memory, Monitor, HDD, Battery and so on.
So your employee talks to Customer and asks questions which memory do you want and so on. Or otherwise if Customer is not technic, employee could ask “Do you need this laptop for gaming?”.
So you need some steps to construct you computer and they are defined in Abstract Builder.

Code:

/** Abstract Builder */
public abstract class LaptopBuilder {
    protected Laptop laptop;
   
    public void createNewLaptop(){
        laptop = new Laptop();
    }

    public Laptop getMyLaptop(){
        return laptop;
    }
    //mentioned steps to build laptop
    public abstract void setMonitorResolution();
    public abstract void setProcessor();
    public abstract void setMemory();
    public abstract void setHDD();
    public abstract void setBattery();
}

 If your Customer answers like “Yes, I wanna play… play…!!!“. You already have implementation of Concrete Builder for gaming laptop like:

/** Concrete Builder */
public class GamingLaptopBuilder extends LaptopBuilder {
    public void setBattery() {
        laptop.Battery = “6 lbs”;
    }
    public void setHDD() {
        laptop.HDD = “500 Gb”;
    }
    public void setMemory() {
       laptop.Memory = “6144 Mb”;  
    }
    public void setMonitorResolution() {
        laptop.MonitorResolution = “1900X1200”;
    }
    public void setProcessor() {
        laptop.Processor = “Core 2 Duo, 3.2 GHz”;
    }
}

Or if you Customer is business guy and he watches presentations and exel reports in plain:

Code:

/** Concrete Builder */
public class TripLaptopBuilder extends LaptopBuilder {
    public void setBattery() {
        laptop.Battery = “12 lbs”;
    }
    public void setHDD() {
        laptop.HDD = “250 Gb”;
    }
    public void setMemory() {
       laptop.Memory = “2048 Mb”;
    }
    public void setMonitorResolution() {
        laptop.MonitorResolution = “1200X800”;
    }
    public void setProcessor() {
        laptop.Processor = “Celeron 2 GHz”;
    }
}

To manage steps to build your Laptop basing on the answer you need Director:

Code:

/** Director */
public class BuyLaptop {
    private LaptopBuilder laptopBuilder;
   
    public void setLaptopBuilder(LaptopBuilder lBuilder){
        laptopBuilder = lBuilder;
    }
   
    public Laptop getLaptop(){
        return laptopBuilder.getMyLaptop();
    }
   
    public void constructLaptop(){
       
        laptopBuilder.createNewLaptop();
       
        laptopBuilder.setMonitorResolution();
        laptopBuilder.setProcessor();
        laptopBuilder.setMemory();
        laptopBuilder.setHDD();
        laptopBuilder.setBattery();
    }
}

Here we have usage code.

//Your system could have bulk of builders
TripLaptopBuilder tripBuilder = new TripLaptopBuilder();
GamingLaptopBuilder gamingBuilder = new GamingLaptopBuilder();

BuyLaptop shopForYou = new BuyLaptop();//director

shopForYou.setLaptopBuilder(gamingBuilder);//Customer answered that he wants to play
shopForYou.constructLaptop();

Laptop laptop = shopForYou.getLaptop();//just get what he wants
laptop.print();

Output:

Laptop: 1900X1200, Core 2 Duo, 3.2 GHz, 6144 Mb, 500 Gb, 6 lbs

Easy, simple and great pattern. 

What have I learned regarding of Java?
Nothing special, just how to format strings:

Code:

System.out.print(String.format(“Laptop: %s, %s, %s, %s, %s”, MonitorResolution, Processor, Memory, HDD, Battery));

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone