Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

  • 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.

    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.
    //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?


    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:
    //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
    How to use Builder Setters copeg
    3
    1. nagaraj200788@gmail.com -
      very nice way ....................
    1. S.O.P. -
      awesome..
    1. chncho -
      seems like the jquery syntax