85. Creational Design Patterns - Builder Pattern.
Let's say we have the following class:
public class Phone{
private String os;
private String processor;
private int battery;
private int camera;
public Phone(String os, String processor, int battery, int camera){
super();
this.os = os;
this.processor = processor;
this.battery = battery;
this.camera = camera;
}
}
Now when we have to create a Object of this class, we have to pass all the 4 arguments in the correct order. This is a sort of a headache. To avoid this passing of values, we can use a PhoneBuilder class.
public class PhoneBuilder{
private String os;
private String processor;
private int battery;
private int camera;
public PhoneBuilder setOs(String os){
this.os = os;
return this;
}
public PhoneBuilder setProcessor(String processor){
this.processor = processor;
return this;
}
public PhoneBuilder setBattery(int battery){
this.battery = battery;
return this;
}
public PhoneBuilder setCamera(int camera){
this.camera = camera;
return this;
}
public Phone getPhone(){
return new Phone(os, processor, battery, camera);
}
}
We have setters, all which return PhoneBuilder. We have a getPhone method as well.
Now how do we use this?
public class Shop{
Phone p = new PhoneBuilder().setOs("Android").setBattery(1000).getPhone();
}
Now we have instantiated the class, without giving all args and in correct order. We can choose to give only a few Args and in any order that we like!