Repository: java-sf-builder-simple-example
What is double brace initialization?
See what-is-double-brace-initialization-in-java and DoubleBraceInitialization.
- Very simply to use.
- Breaks "equals" compatibility.
- No checks performed, when use direct assignments.
- Possible memory leaks.
Can we avoid these troubles? Yes!
- Make separate "Builder" class especially for double brace initialization.
- Declare fields with default values.
- Put object creation method in that class.
- Simply to use.
- Do not breaks "equals" compatibility.
- You can perform checks in creation method.
- No memory leaks.
None
And, as a result, we have simplest java builder pattern ever.
All: java-sf-builder-simple-example
Simple class: MyClass.java
Inheritance: MyBaseClass.java, MyChildClass.java
Updated samples: replace passing of all parameters to pass only builder in class constructor.
New simple class: NewMyClass.java
New inheritance: NewBaseClass.java, NewChildClass.java
For most complicated java builder pattern see java-sf-builder-mega
Article: http://habrahabr.ru/post/261163/