What is the Builder Pattern?
The Builder pattern is one of the Design Patterns in the Creational Design Pattern group that is used to build complex objects with a large number of classes and parameters. It's really not very useful in small classes that simply don't have many constructors.
When a program has many classes, parameters, as well as many constructors, using the Factory Pattern or Abstract Factory Pattern will be very complicated. So we can use the Builder Pattern to overcome.
We will make a simple program with the Builder Pattern to help you understand how it should work and under what circumstances.
Let's create a class with certain properties, specifically as follows:
- Variables: computerCase, CPU, motherboard, GPU, HDD, operatingSystem are of type String.
- Variables: powerSupply, amountOfRAM are of type INT.
All the above variables must be encapsulated (Private).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Computer {<font></font> private String computerCase;<font></font> private String CPU;<font></font> private String motherboard;<font></font> private String GPU;<font></font> private String HDD;<font></font> private String operatingSystem;<font></font> private int powerSupply;<font></font> private int amountOfRAM;<font></font> <font></font> public Computer(String computerCase, String CPU, String motherboard, String GPU, <font></font> String HDD, String operatingSystem, int powerSupply, int amountOfRAM) {<font></font> this .computerCase = computerCase;<font></font> this .CPU = CPU;<font></font> this .motherboard = motherboard;<font></font> this .GPU = GPU;<font></font> this .HDD = HDD;<font></font> this .operatingSystem = operatingSystem;<font></font> this .powerSupply = powerSupply;<font></font> this .amountOfRAM = amountOfRAM;<font></font> }<font></font> <font></font> //getters and setters<font></font> } |
One problem here is that even such a small and simple class requires a very large constructor.
We can create more constructors in class using Builder Pattern.
To do this, we will nest a static Builder class inside the Computer class.
This Builder way will be used to build objects in a clean and readable way, unlike the code above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | public class Computer {<font></font> <font></font> public static class Builder {<font></font> private String computerCase;<font></font> private String CPU;<font></font> private String motherboard;<font></font> private String GPU;<font></font> private String HDD;<font></font> private String operatingSystem;<font></font> private int powerSupply;<font></font> private int amountOfRAM;<font></font> <font></font> public Builder withCase(String computerCase) {<font></font> this .computerCase = computerCase;<font></font> return this ;<font></font> }<font></font> <font></font> public Builder withCPU(String CPU) {<font></font> this .CPU = CPU;<font></font> return this ;<font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > }</font></font><font></font> <font></font> public Builder withMotherboard(String motherboard) {<font></font> this .motherboard = motherboard;<font></font> return this ;<font></font> }<font></font> <font></font> public Builder withGPU(String GPU) {<font></font> this .GPU = GPU;<font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > return this ;</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > }</font></font><font></font> <font></font> public Builder withHDD(String HDD) {<font></font> this .HDD = HDD;<font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > return this ;</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > }</font></font><font></font> <font></font> public Builder withOperatingSystem(String operatingSystem) {<font></font> this .operatingSystem = operatingSystem;<font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > return this ;</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > }</font></font><font></font> <font></font> public Builder withPowerSupply( int powerSupply) {<font></font> this .powerSupply = powerSupply;<font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > return this ;</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > }</font></font><font></font> <font></font> public Builder withAmountOfRam( int amountOfRAM) {<font></font> this .amountOfRAM = amountOfRAM;<font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > return this ;</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > }</font></font><font></font> <font></font> public Computer build() {<font></font> Computer computer = new Computer();<font></font> computer.computerCase = this .computerCase;<font></font> computer.CPU = this .CPU;<font></font> computer.motherboard = this .motherboard;<font></font> computer.GPU = this .GPU;<font></font> computer.HDD = this .HDD;<font></font> computer.operatingSystem = this .operatingSystem;<font></font> computer.powerSupply = this .powerSupply;<font></font> computer.amountOfRAM = this .amountOfRAM;<font></font> <font></font> return computer;<font></font> }<font></font> }<font></font> <font></font> private Computer() {<font></font> //nothing here<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> }</font></font><font></font> <font></font> //fields<font></font> //getters and setters<font></font> } |
This nested class has the same properties as the Computer class and uses them to construct Computer objects.
Once all is set up, we can initialize the Computer Objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Main {<font></font> public static void main(String[] args) {<font></font> Computer computer = new Computer.Builder()<font></font> .withCase( "Tower" )<font></font> .withCPU( "Intel i5" )<font></font> .withMotherboard( "MSI B360M-MORTAR" )<font></font> .withGPU( "nVidia Geforce GTX 750ti" )<font></font> .withHDD( "Toshiba 1TB" )<font></font> .withOperatingSystem( "Windows 10" )<font></font> .withPowerSupply( 500 )<font></font> .withAmountOfRam( 8 )<font></font> .build();<font></font> }<font></font> } |
This is a shorter and cleaner way of writing it.
1 2 3 4 5 6 | public class Main {<font></font> public static void main(String[] args) {<font></font> Computer computer = new Computer( "Tower" , "Intel i5" , "MSI B360M-MORTAR" , <font></font> "nVidia GeForce GTX 750ti, " Toshiba 1TB ", " Windows 10 ", 500, 8);<font></font><font style=" vertical-align: inherit; "><font style=" vertical-align: inherit;"> }</font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" > }</font></font> |
You can practice with other simple programs to get used to this pattern. I hope this tutorial will help you understand what the Builder Pattern is and how to implement it in Java.