-
Notifications
You must be signed in to change notification settings - Fork 0
Builder Design Pattern
Devrath edited this page Dec 2, 2023
·
2 revisions
Contents |
---|
Gangs of Four(GOF) - Definition |
General reference scenario |
In the context of programming |
Examples in Android |
Programming example |
Programming example observation |
Separating the construction of the object from its representation.
- Consider a scenario where you have gone for brunch in a hotel 🏨
- In the hotel there are many items on the table so you can take them one by one.
- Now as you travel 🗺️ one by one on each item some you want and add it to your plate to eat and others you don't add and ignore but you could have added it if you wanted it.
- Meaning to say that adding something is optional and not compulsory.
- In the end once the items on the plate are ready, we start to eat the food.
Thus, the Builder pattern simplifies the process of creating objects and we choose the properties that we apply for an object and finally build the object. A different set of choices will result in a different object.
AlertDialog.Builder(this)
.setTitle("Add food type - 1")
.setMessage("Add food type - 2")
.setNegativeButton("No thanks") { dialogInterface, i ->
// "No thanks" action
}
.setPositiveButton("OK") { dialogInterface, i ->
// "OK" action
}
.show()
Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
Main.java
public class Main {
public static void main(String[] args) {
MaterialAlert alert = new MaterialAlert.Builder("Yes","No").build();
}
}
MaterialAlert.java
public class MaterialAlert {
// <!--------------- Optional parameters --------------->
private Boolean title = false;
private Boolean message = false;
// <!--------------- Optional parameters --------------->
// <!--------------- Compulsory parameters --------------->
private String positiveButtonText;
private String negativeButtonText;
private MaterialAlert(Builder builder) {
this.positiveButtonText = builder.positiveButtonText;
this.negativeButtonText = builder.negativeButtonText;
this.title = builder.isTitleVisible;
this.message = builder.isMessageVisible;
}
// <!--------------- Compulsory parameters --------------->
public void setTitle(Boolean title) { this.title = title; }
public void setMessage(Boolean message) { this.message = message; }
public void setPositiveButtonText(String text) { this.positiveButtonText = text; }
public void setNegativeButtonText(String text) { this.negativeButtonText = text; }
// Builder class
public static class Builder {
// <!--------------- Optional parameters --------------->
private Boolean isTitleVisible = false;
private Boolean isMessageVisible = false;
// <!--------------- Optional parameters --------------->
// <!--------------- Compulsory parameters --------------->
private final String positiveButtonText;
private final String negativeButtonText;
// <!--------------- Compulsory parameters --------------->
// Compulsory params are set in constructor of builder
public Builder(String positiveButtonText, String negativeButtonText){
this.positiveButtonText = positiveButtonText;
this.negativeButtonText = negativeButtonText;
}
/** ************* Set optional params to builder ************* **/
public Builder setTitleVisible(Boolean isTitleVisible){
// Modify the params and return the same instance of class
this.isTitleVisible = isTitleVisible;
return this;
}
public Builder setMessageVisible(Boolean isMessageVisible){
// Modify the params and return the same instance of class
this.isMessageVisible = isMessageVisible;
return this;
}
/** ************* Set optional params to builder ************* **/
public MaterialAlert build() {
return new MaterialAlert(this);
}
}
}
- We can observe that in the builder in each setter method, we modify the builder object and return the same instance, Meaning it gets modified.
- Builder constructor has the compulsory parameters.
- Also notice the Builder is the inner class.
- Now since the builder is the inner class we can access the parent class that is MaterialAlert
- we perform the new object creation of the parent class inside the builder.
- Important to make the constructor of the parent class private so that someone doesn't create the parent class object.
- Also we need to make the inner class, which is builder as static so we can access the inner classes without creating the object of parent materialalert.
.