[Building Sakai] Java Help - Builder Pattern and Setters

Raymond Naseef raymond.naseef at gmail.com
Sun Aug 11 20:13:31 PDT 2013


Hi Chuck,

The code example below would not compile -- I think your code would be:
 public Stuff setThing (String thing).

As for changing the return type of methods, changing from void will not
break existing code that calls the void methods, since the old calling code
cannot be written to look a response.  JBoss HIbernate and Spring Framework
both use reflect to lookup accessory methods, so those may break.  It all
depends on if they look at a methods return type or not.  My guess is that
Hibernate and Spring do not look at return type for setters.

As for using builder pattern, I am not sure I follow how you would end up
making the changes to accessory methods, as a builder would not be written
by overriding accessory methods.  For Java properties (i.e. fields using
getField() and setField() methods), the accessory methods should be as
minimal as possible, and doing anything other than simply getting/setting
the value of the field is an anti-pattern.

If you are talking about methods called setThing(), and those methods are
not accessory methods, you can certainly write code that way, and that is
fine.

If you give an example of what you are thinking to write, I may be able to
help you think  through this more.


All the best,

Ray


On Aug 11, 2013 9:48 AM, "Charles Severance" <csev at umich.edu> wrote:

> Hi all,
>
> I have a Java question.
>
> I would like to use a builder pattern for my JSON stuff for LTI 2.0 in
> Sakai.  One of the things that I need to do is make all my setters return
> the instance.  So instead of:
>
> public void setThing(String thing) {
>   this.thing = thing;
> }
>
> I need to do something like this:
>
> public Stuff public void setThing(String thing) {
>   this.thing = thing;
>   return this;
> }
>
> My question is whether switching the return value from void to an
> instance, do I break all the other things in the world that look for
> Setters?
>
> Thanks.
>
> /Chuck
>
> P.S. I still hate setters and getters  - but the builder pattern is
> warming on me because of its type checking and being a little more
> self-documenting than massive constructors with seven String parameters.
> _______________________________________________
> sakai-dev mailing list
> sakai-dev at collab.sakaiproject.org
> http://collab.sakaiproject.org/mailman/listinfo/sakai-dev
>
> TO UNSUBSCRIBE: send email to
> sakai-dev-unsubscribe at collab.sakaiproject.org with a subject of
> "unsubscribe"
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://collab.sakaiproject.org/pipermail/sakai-dev/attachments/20130811/ab08673b/attachment.html 


More information about the sakai-dev mailing list