Results 1 to 3 of 3
  1. #1
    dazz is offline Member
    Join Date
    Apr 2017
    Posts
    27
    Rep Power
    0

    Default With builder, where to set default values optonal variables

    Hi

    I am using builder for the first time on an object that can take up to 16 variables.
    Using this example of builder
    where would be the best/right place to set the default values of optional variables not called with the constructor?

    I don't want to leave the optional variables undefined.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: With builder, where to set default values optonal variables

    Looking at that example, the only place to do that is in the ComputerBuilder. The build() method will overwrite all the properties of the Computer, so any optional with a default value should be initialized in the ComputerBuilder. In the example below I have set the booleans to true:
    Java Code:
    //Builder Class
    public static class ComputerBuilder{
    	// required parameters
            private String HDD;
    	private String RAM;
    
    	// optional parameters
    	private boolean isGraphicsCardEnabled = true;
    	private boolean isBluetoothEnabled = true;
    		
    	public ComputerBuilder(String hdd, String ram){
    		this.HDD=hdd;
    		this.RAM=ram;
    	}
    ... snip ...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    dazz is offline Member
    Join Date
    Apr 2017
    Posts
    27
    Rep Power
    0

    Default Re: With builder, where to set default values optonal variables

    Hi
    OK, I will do that.

    For the benefit of other Java beginners like me who may find this in the future, I have added additional comments to the example. The additional comments visibly divide the code into sections.

    As a general comment, the builder template seems like a clumsy add-on to Java. My class file to implement a 16 parameter builder is over 200 lines long (including comments etc). The outcome is more user friendly but the input requires a lot of typing.

    I have written a second and third builder constructor in the same class with different input args. One has no args which I use to define a special case. Nice.

    package com.journaldev.design.builder;

    public class Computer {
    //### DECLARE VARIABLES
    //required parameters
    private String HDD;
    private String RAM;

    //optional parameters
    private boolean isGraphicsCardEnabled;
    private boolean isBluetoothEnabled;

    //### GETTERS
    public String getHDD() {
    return HDD;
    }

    public String getRAM() {
    return RAM;
    }

    public boolean isGraphicsCardEnabled() {
    return isGraphicsCardEnabled;
    }

    public boolean isBluetoothEnabled() {
    return isBluetoothEnabled;
    }
    // ### DECLARE THE BUILDER - include all variables
    private Computer(ComputerBuilder builder) {
    this.HDD=builder.HDD;
    this.RAM=builder.RAM;
    this.isGraphicsCardEnabled=builder.isGraphicsCardE nabled;
    this.isBluetoothEnabled=builder.isBluetoothEnabled ;
    }
    //### BUILDER CLASS
    //Builder Class
    public static class ComputerBuilder{
    //# RE-DECLARE VARIABLES
    // required parameters
    private String HDD;
    private String RAM;

    // optional parameters : set defaults (optional)
    private boolean isGraphicsCardEnabled = true;
    private boolean isBluetoothEnabled = true;

    //# CONSTRUCTOR WITH REQUIRED PARAMETERS

    public ComputerBuilder(String hdd, String ram){
    this.HDD=hdd;
    this.RAM=ram;
    }
    //# SETTERS FOR OPTIONAL VARIABLES

    public ComputerBuilder setGraphicsCardEnabled(boolean isGraphicsCardEnabled) {
    this.isGraphicsCardEnabled = isGraphicsCardEnabled;
    return this;
    }

    public ComputerBuilder setBluetoothEnabled(boolean isBluetoothEnabled) {
    this.isBluetoothEnabled = isBluetoothEnabled;
    return this;
    }

    public Computer build(){
    return new Computer(this);
    }

    }

    }
    Last edited by dazz; 05-11-2017 at 03:41 AM.

Similar Threads

  1. Replies: 3
    Last Post: 03-23-2015, 10:10 PM
  2. How to display default constructor values?
    By IanKW in forum New To Java
    Replies: 3
    Last Post: 09-14-2014, 10:25 AM
  3. Default values of stored procedures
    By vinayakshukre in forum JDBC
    Replies: 2
    Last Post: 01-13-2009, 12:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •