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.

Results 1 to 4 of 4

Thread: How to use Builder Setters

  1. #1
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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
    Last edited by copeg; October 26th, 2011 at 03:01 PM.

  2. The Following 7 Users Say Thank You to copeg For This Useful Post:

    B33R4NG (July 5th, 2012), Freaky Chris (November 4th, 2011), helloworld922 (November 4th, 2011), JavaPF (November 1st, 2011), newbie (November 9th, 2011), snowguy13 (January 19th, 2012), Tjstretch (November 17th, 2011)


  3. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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

  4. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to use Builder Setters

    Haha really nice, can't believe I never thought of doing this!
    Thank you.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default 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!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. Am I doing it rite? Using netbeans GUI builder
    By erm3r in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2011, 10:48 AM
  2. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  3. [SOLVED] NetBeans GUI Builder image paintint
    By Kakashi in forum Java IDEs
    Replies: 2
    Last Post: June 15th, 2011, 08:06 AM
  4. How do I change Look and Feel in Window Builder?
    By Zorobay in forum AWT / Java Swing
    Replies: 1
    Last Post: May 3rd, 2011, 09:08 PM
  5. Process Builder Issue
    By javameanslife in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 20th, 2010, 09:13 PM