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?
Introduction
Builder is a Creational Design Pattern 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 technical, employee could ask "Do you need this laptop for gaming?". So you need some steps to construct your computer and they are defined in Abstract Builder.
public abstract class LaptopBuilder {
protected Laptop laptop;
public void createNewLaptop(){
laptop = new Laptop();
}
public Laptop getMyLaptop(){
return 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:
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 your Customer is a business guy and he watches presentations and Excel reports in plain:
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:
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:
TripLaptopBuilder tripBuilder = new TripLaptopBuilder();
GamingLaptopBuilder gamingBuilder = new GamingLaptopBuilder();
BuyLaptop shopForYou = new BuyLaptop();
shopForYou.setLaptopBuilder(gamingBuilder);
shopForYou.constructLaptop();
Laptop laptop = shopForYou.getLaptop();
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 Java?
Nothing special, just how to format string
s:
System.out.print(String.format("Laptop: %s, %s, %s, %s, %s", MonitorResolution,
Processor, Memory, HDD, Battery));