I just started learning and implementing builder patterns from Wiki. And also CH2 of Effective Java.
This pertains to JSP servlets, this might be a little convoluted, but I just wanted to pass this by you guys to see how to do this correctly.
Lets start with a scenario, where you can't build the object completely there are certain information that is not given. So most likely you have to put the object in session and then add variables to the session object later. How would I accomplish this with Build pattern?
Here is a code example
public class Widget(){
public static class Builder(){
public Builder(String id) {...}
public Builder serialNumber (String val) {...}
public Builder area (String val) {...}
public Widget build() { return new Widget(this); }
}
private Widget(Builder builder){...}
}
JSP Servlet 1 // only have ID information
Widget w = new Widget().Builder(id).build();
HttpSession session = request.getSession();
session.setAttribute("widget", w);
JSP Servlet 2 // Now I have serial and area
Widget.Builder w = (Widget.Builder) session.getAttribute("widget");
//** This is as far as I go **
w.serialNumber("something") // Now this works
.area("sideArea") //
.build() // < I know if I do this I will be creating another Object. Is there a way to use build pattern without creating redundant obj?
Thank you all...