Builder
Builder comes under creational design pattern.
The intention is to build a complex object step by step.
Behaviour & Advantages
Separates the construction of a complex object from its representation
so that the same construction process can create different representations.
Can alter the object construction process easily.
Separates the object construction process from its representation.
Participants
Builder
Abstract interface for creating parts of complex product.
Concrete Builder
An implementation of Builder which assembles the parts of product.
Director
It controls the object construction process and flow.
Internally it depends on Builder to assemble the final product.
Product
The object to be constructed using builder.
In this example we are going to build a complex product (PersonalComputer ) using builder pattern.
Class IPCBuilder represents a builder. Classes HighCostPCBuilder , LowCostPCBuilder
are two implementations of IPCBuilder . Class PCSeller acts as a director which controls
the PC construction process.
package com.bethecoder.tutorials.dp.builder;
public class PersonalComputer {
private String motherboard;
private String processor;
private String hardDisk;
private String monitor;
private String ram;
private String mouse;
private String keyboard;
public String getMotherboard () {
return motherboard;
}
public void setMotherboard ( String motherboard ) {
this .motherboard = motherboard;
}
public String getProcessor () {
return processor;
}
public void setProcessor ( String processor ) {
this .processor = processor;
}
public String getHardDisk () {
return hardDisk;
}
public void setHardDisk ( String hardDisk ) {
this .hardDisk = hardDisk;
}
public String getMonitor () {
return monitor;
}
public void setMonitor ( String monitor ) {
this .monitor = monitor;
}
public String getRam () {
return ram;
}
public void setRam ( String ram ) {
this .ram = ram;
}
public String getMouse () {
return mouse;
}
public void setMouse ( String mouse ) {
this .mouse = mouse;
}
public String getKeyboard () {
return keyboard;
}
public void setKeyboard ( String keyboard ) {
this .keyboard = keyboard;
}
public String toString () {
StringBuilder sb = new StringBuilder () ;
sb.append ( "----------- PC Configuration -----------" ) ;
sb.append ( "\n Mother Board : " ) .append ( motherboard ) ;
sb.append ( "\n Processor : " ) .append ( processor ) ;
sb.append ( "\n Hard Disk : " ) .append ( hardDisk ) ;
sb.append ( "\n Monitor : " ) .append ( monitor ) ;
sb.append ( "\n RAM : " ) .append ( ram ) ;
sb.append ( "\n Mouse : " ) .append ( mouse ) ;
sb.append ( "\n Keyboard : " ) .append ( keyboard ) ;
return sb.toString () ;
}
}
The PC Builder interface is shown below,
package com.bethecoder.tutorials.dp.builder;
public interface IPCBuilder {
/**
* Setup motherboard.
*/
public void setupMotherBoard () ;
/**
* Setup RAM and processor.
*/
public void setupProcessor () ;
/**
* Setup hard disk.
*/
public void setupHardDisk () ;
/**
* Input peripherals - keyboard, mouse
* Output peripherals - monitor
*/
public void setupPeripherals () ;
/**
* Get the fully constructed PC.
*
* @return PersonalComputer
*/
public PersonalComputer getPC () ;
}
High Cost PC Builder implementation is shown below,
package com.bethecoder.tutorials.dp.builder;
/**
* High Cost PC Builder Implementation
*/
public class HighCostPCBuilder implements IPCBuilder {
private PersonalComputer personalComputer = null ;
public HighCostPCBuilder () {
this .personalComputer = new PersonalComputer () ;
}
@Override
public PersonalComputer getPC () {
return personalComputer;
}
@Override
public void setupHardDisk () {
personalComputer.setHardDisk ( "500 GB Hard disk" ) ;
}
@Override
public void setupMotherBoard () {
personalComputer.setMotherboard ( "GigaByte GA-X58A-UD3R Desktop Motherboard" ) ;
}
@Override
public void setupPeripherals () {
personalComputer.setKeyboard ( "Logitech Multimedia Keyboard" ) ;
personalComputer.setMouse ( "Logitech G9 Laser Mouse" ) ;
personalComputer.setMonitor ( "19 inch LCD monitor" ) ;
}
@Override
public void setupProcessor () {
personalComputer.setProcessor ( "Intel P4 processor" ) ;
personalComputer.setRam ( "4 GB" ) ;
}
}
Low Cost PC Builder implementation is shown below,
package com.bethecoder.tutorials.dp.builder;
/**
* Low Cost PC Builder Implementation
*/
public class LowCostPCBuilder implements IPCBuilder {
private PersonalComputer personalComputer = null ;
public LowCostPCBuilder () {
this .personalComputer = new PersonalComputer () ;
}
@Override
public PersonalComputer getPC () {
return personalComputer;
}
@Override
public void setupHardDisk () {
personalComputer.setHardDisk ( "40 GB Hard disk" ) ;
}
@Override
public void setupMotherBoard () {
personalComputer.setMotherboard ( "Asus A7N8X-LA Motherboard" ) ;
}
@Override
public void setupPeripherals () {
personalComputer.setKeyboard ( "Logitech Standard Keyboard" ) ;
personalComputer.setMouse ( "Logitech Wireless Mouse M215" ) ;
personalComputer.setMonitor ( "15 inch LCD monitor" ) ;
}
@Override
public void setupProcessor () {
personalComputer.setProcessor ( "Intel P3 processor" ) ;
personalComputer.setRam ( "512 MB" ) ;
}
}
The director implementation is shown below,
package com.bethecoder.tutorials.dp.builder;
public class PCSeller {
public static PersonalComputer getPC ( IPCBuilder pcBuilder ) {
/**
* Build PC Step by Step
* The construction process/flow is
* controlled by this class.
*/
pcBuilder.setupMotherBoard () ;
pcBuilder.setupProcessor () ;
pcBuilder.setupHardDisk () ;
pcBuilder.setupPeripherals () ;
return pcBuilder.getPC () ;
}
}
Builder usage is shown below,
package com.bethecoder.tutorials.dp.builder;
public class Test {
/**
* @param args
*/
public static void main ( String [] args ) {
System.out.println ( PCSeller.getPC ( new LowCostPCBuilder ())) ;
System.out.println () ;
System.out.println ( PCSeller.getPC ( new HighCostPCBuilder ())) ;
}
}
It gives the following output,
----------- PC Configuration -----------
Mother Board : Asus A7N8X-LA Motherboard
Processor : Intel P3 processor
Hard Disk : 40 GB Hard disk
Monitor : 15 inch LCD monitor
RAM : 512 MB
Mouse : Logitech Wireless Mouse M215
Keyboard : Logitech Standard Keyboard
----------- PC Configuration -----------
Mother Board : GigaByte GA-X58A-UD3R Desktop Motherboard
Processor : Intel P4 processor
Hard Disk : 500 GB Hard disk
Monitor : 19 inch LCD monitor
RAM : 4 GB
Mouse : Logitech G9 Laser Mouse
Keyboard : Logitech Multimedia Keyboard