Let’s find out what the construction is. In common meaning builder is someone who make some buildings.
In programming it is a pattern which helps to crate some objects. As an example we can show a house contains kitchen, living room, bedroom, bathroom, garage or garden. One of its main assumptions is to separate the object from the process of its creation
We use the builder pattern mainly when:
- the structure of the project is multi-level and extensive
- simplification of the design is impossible to implement
- the objects will be built multiple times
An example would be the Airplane class which I will present below:
public class Airplane {
private String engine;
private String wings;
private String chassis;
public void setEngine(String engine){
this.engine=engine;
}
public void setWings(String wings){
this.wings=wings;
}
public void setChassis(String chassis){
this.chassis=chassis;
}
public String getEngine(){
return engine;
}
public String getWings(){
return wings;
}
public String getChassis(){
return chassis;
}
}
As we can see, our simplified model of the plane contains three elements:
- engine
- wings
- chassis
Getters and setters for setting data have also been introduced.
Using the Builder interface presented below, we will create the AirplaneBoeingBuilder class.
public interface Builder {
void mountingEngine();
void mountingWings();
void mountingChassis();
}
public class AirplaneBoeingBuilder implements Builder{
private Airplane airplane= new Airplane();
@Override
public void mountingEngine() {
airplane.setEngine("Engine Boeing");
}
@Override
public void mountingWings() {
airplane.setWings("Wings Boeing");
}
@Override
public void mountingChassis() {
airplane.setChassis("Chassis Boeing");
}
public Samolot mountingAirplane(){
System.out.println("\n"+ airplane.getChassis()+ "\n"+ airplane.getEngine()+ "\n" + airplane.getWings());
System.out.println("\nNew Airplain is ready");
return airplane;
}
}
Together with AirplaneBoeingBuilder, we implement interferjs Builder and create a new aircraft object. Interface allows you to implement methods:
- mountingEngine
- mountingWings
- mountingChassis
As you know, there is a construction manager over every builder, so it is important to introduce a class that will control his actions.
In the given example, it will be the chief builder class:
public class ChefBuilder {
public void CreateAirplane (Builder constructor){
constructor.mountingChassis();
constructor.mountingEngine();
constructor.mountingWings();
}
}
It’s time to test the classes in our project.
public class TestBuild {
public static void main(String[] args){
ChefBuilder chefBuilder= new ChefBuilder();
AirplaneBoeingBuilder airplaneBoeing= new AirplaneBoeingBuilder();
chefBuilder.CreateAirplane(airplaneBoeing);
airplaneBoeing.mountingAirplane();
}
}
The result of the class is as follows
Chassis Boeing
Engine Boeing
Wings Boeing
New airplane is ready
At the beginning, we create a chief builder class object responsible for overseeing the builder. The next step is to create an object of the builder, which is then called as an argument in the createAirplane method. Finally, the airplane assembly method displays the above messages.