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!
i don't one. there no constructors defined in
builder
class, means compiler automatically generate parameterless one.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.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.yes, right. create instance of inner class, , use
build
method obtain instance of outer class.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.)yes, can, there
public
constructor inperson
class takes 2string
objects.one can unit test creating instance of
builder
, usewithfirstname
,withlastname
methods set name, , call getter methods on returnedperson
object , check see if equal passed-in values (using stringequals
method).you check see whether or not arguments meet preconditions using methods in
string
class, , throw exception if not.(i'm assuming mean if 1 passes in
null
values, case because of default initialization.) can take advantage of handy method introduced injava.util.objects
class introduced in java 7,objects.requirenonnull()
. pass argument it, , throw exception if argumentnull
. otherwise, return argument.
Comments
Post a Comment