Properties:
- Helps simplifying creation of complex objects having constructers with many paramters.
- Can be identified easily by mehods returning reference to itself and a build method which returns reference of the target object.
Implementation:
- An interface defines which methods should builders have.
- Implementing builders may come with different building strategies.
- Each method in builder takes a parameter and returns reference of it self, so that methods can be chained.
- Finally a build method is provided to build actual object.
Java Standard Library Implementations:
- java.lang.StringBuilder
- java.lang.StringBuffer
Example Usage:
// A builder typically lets method chaining
StringBuilder sb=new StringBuilder()
.append("String1 ")
.append(123)
.insert(5,"Middle");
// after setting build parameters,
//object instance can be built using build method
sb.toString();
Comments
Post a Comment