How to use Builder Setters
Oftentimes constructing a class might require many different variables - in short a particular class might need to be built with a range of options; options that are only decided at runtime. Creating a constructor that accepts several different parameters is an option, as is using a series of setter methods as needed (see below).
As an example, lets build a Pizza. The following class contains the information we want to specify for our pizza - it allows us to specify the toppings, if we want it delivered, etc...This is a simple class, all of its variables are private and accessed only through the modifier get/set methods. For brevity I have left out comments in the code.
Code java:
import java.util.ArrayList;
import java.util.List;
public class Pizza{
private String crustType = "Thick";
private boolean extraCheese = false;
private boolean forDelivery = false;
private List<String> toppings = new ArrayList<String>();
public Pizza(){
}
public Pizza(String crust, boolean extraCheese, boolean forDelivery, String...toppings){
this.crustType = crust;
this.extraCheese = extraCheese;
this.forDelivery = forDelivery;
for ( String t : toppings ){
this.topings.add(t);
}
}
public String getCrustType() {
return crustType;
}
public void setCrustType(String crustType) {
this.crustType = crustType;
}
public boolean isExtraCheese() {
return extraCheese;
}
public void setExtraCheese(boolean extraCheese) {
this.extraCheese = extraCheese;
}
public boolean isForDelivery() {
return forDelivery;
}
public void setForDelivery(boolean forDelivery) {
this.forDelivery = forDelivery;
}
public List<String> getToppings() {
return toppings;
}
public void addTopping(String topping) {
this.toppings.add(topping);
}
}
To use this class, we need to make use of the constructor (which can get ugly for more complex classes), or use the empty argument version and go through several lines of setter methods.
Code java:
//use the full constructor
Pizza pizza = new Pizza("Thick", false, false, "peppers", "olives");
//Or build using accessor methods...
pizza = new Pizza();
pizza.setCrustType("Thick");
pizza.setExtraCheese(false);
pizza.setForDelivery(false);
pizza.addTopping("peppers");
pizza.addTopping("olives");
Is there another way? Let's slightly adjust the code above, modifying the setter methods. Take a look at the class below - notice anything different?
Code java:
import java.util.ArrayList;
import java.util.List;
public class Pizza{
private String crustType = "Thick";
private boolean extraCheese = false;
private boolean forDelivery = false;
private List<String> toppings = new ArrayList<String>();
public Pizza(){
}
public Pizza(String crust, boolean extraCheese, boolean forDelivery, String...toppings){
this.crustType = crust;
this.extraCheese = extraCheese;
this.forDelivery = forDelivery;
for ( String t : toppings ){
this.toppings.add(t);
}
}
public String getCrustType() {
return crustType;
}
public Pizza setCrustType(String crustType) {
this.crustType = crustType;
return this;
}
public boolean isExtraCheese() {
return extraCheese;
}
public Pizza setExtraCheese(boolean extraCheese) {
this.extraCheese = extraCheese;
return this;
}
public boolean isForDelivery() {
return forDelivery;
}
public Pizza setForDelivery(boolean forDelivery) {
this.forDelivery = forDelivery;
return this;
}
public List<String> getToppings() {
return topings;
}
public Pizza addTopping(String toping) {
this.topings.add(toping);
return this;
}
}
The instance (this) is returned for every setter method. This might seem strange, so what does this accomplish? Coming back to our first example where we construct an instance of this class - both previous ways still work - the new modifications allow us to build a pizza with an entirely new syntax, nesting the setter methods as needed:
Code java:
//Now we have another way to build a pizza
Pizza pizza = new Pizza().setCrustType("Thick").setExtraCheese(false).setForDelivery(false).addToping("peppers").addToping("olives");
It may seem like a strange syntax, but in my opinion adds a more compact and dynamic alternative to building a class (there is a reason why I chose Pizza as an example - not only do I like Pizza, but it was a good demonstration of building an Object of a class where a great many different options can quickly get out of control). Your mileage may vary
Happy Coding
Re: How to use Builder Setters
Brilliant Article thanks!
Many people should pay attention to this. It allows for the production of very slick solutions.
Chris
Re: How to use Builder Setters
Haha really nice, can't believe I never thought of doing this!
Thank you.
Re: How to use Builder Setters
Wow, that's a really unique and awesome idea! I'll be sure to keep that in mind whenever I program! Thanks a ton! :D