0

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...

Eric Huang
  • 1,114
  • 3
  • 22
  • 43
  • 1
    Why not just keep the ID in session until you're ready to build the entire widget? – GriffeyDog Apr 21 '14 at 19:43
  • @GriffeyDog Yes... I guess I could do that as well.... thanks!! its cleaner haha. But i wanted to know if there is a way to do this... – Eric Huang Apr 30 '14 at 02:03

1 Answers1

1
w.serialNumber("something") //  Can not find symbol

because serialNumber is not a method of the w object. What you're probably looking for is method chaining:

public class Widget {
    ... //assuming you have all the right fields here
    public Widget setSerialNumber(String id) {
        this.id = id;
        return this;
    }
    public Widget setArea(String area) {
        this.area = area;
        return this;
    }
    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){...}
}

then you can do something like this:

w.setSerialNumber(id).setArea(area);

Use Widget.Builder when you want to construct a new Widget object, and method chaining when you want to change an existing Widget object.

pawelb
  • 99
  • 2
  • 4
  • Thank you for getting back at the question so quickly. Thank you for pointing out my mistake. I have edit my code, now i am able to invoke the inner Builder class. I am actually referring to builder pattern maybe my reference is not good. [Builder- Pattern](http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern) So if I need to add additional value to Widget obj through 2nd servlet will this create redundant Widget obj? – Eric Huang Apr 21 '14 at 20:53
  • If you use the new methods that I've added to the class (setArea and setSerialNumber), then no - it won't create new instances of the Widget object. Each of those methods returns the object that is being modified - the this value. – pawelb Apr 23 '14 at 22:36