java - How does builder pattern works -


ruby (rails) programmer 2 years, switched team uses java. have questions java builder pattern.

i understand benefits of using pattern, namely avoid telescoping constructor , java bean setters creates inconsistent state have trouble understanding how works, following exact pattern team requires me use:

public class person {     //why important final, hence immutable     private final string firstname;     private final string lastname;      // constructor     public person(string firstname, string lastname)     {         this.firstname = firstname;         this.lastname = lastname;     }      //i have absolutely no idea , why necessary     public static builder builder()     {         return new builder();     }      //this inner class, person outer class (the owning class)     //but why has static class?     public static class builder     {         private string firstname;         private string lastname;          public builder withfirstname(string firstname)         {             //this.firstname refers builder instance firstname             this.firstname = firstname;             return this;             //what return this?  returning instance of builder object?         }          public builder withlastname(string lastname)         {             this.lastname = lastname;             return this;         }          public person build()         {             return new person(firstname, lastname);             //firstname , lastname here refer builder's object instance vars,             //and used create new person object         }     } } 

to use it:

person p = new person.builder(5).firstname("foo").lastname("bar").build(); 

1) param "5" builder for?

2) why builder inner class static?

3) use of public static builder builder() method?

4) correct creating new inner class - builder object, build method within inner class return new person object?

5) seems create person class, have double memory usage, 1 outer class , 1 inner class, in-efficient?

6) correct can still create new person object person p = new person("foo", "bar");

7) how unit test this? how unit test setters , getters?

8) can perform validation on of fields?

9) how specifiy field required, throw exception tries build 1 firstname, no lastname supplied.

many in advance!

  1. i don't one. there no constructors defined in builder class, means compiler automatically generate parameterless one.

  2. if builder class wasn't static, need instance of enclosing class access inner class. "chicken , egg" problem: inner class used obtain instances of outer class, first need instance of outer class use it! static modifier makes instance of outer class not needed use it.

  3. this example of factory method. now, creates instance of builder , returns it, in future, changed to, say, create instance , modify before returning it, or perform security check before returning instance.

  4. yes, right. create instance of inner class, , use build method obtain instance of outer class.

  5. most of time, there tradeoff between design , efficiency. time, although doubling memory usage (since both classes contain 2 variables of type string), actual memory usage not much. (if run out of memory, can change jvm heap space passing arguments jvm.)

  6. yes, can, there public constructor in person class takes 2 string objects.

  7. one can unit test creating instance of builder, use withfirstname , withlastname methods set name, , call getter methods on returned person object , check see if equal passed-in values (using string equals method).

  8. you check see whether or not arguments meet preconditions using methods in string class, , throw exception if not.

  9. (i'm assuming mean if 1 passes in null values, case because of default initialization.) can take advantage of handy method introduced in java.util.objects class introduced in java 7, objects.requirenonnull(). pass argument it, , throw exception if argument null. otherwise, return argument.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -