commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Niall Pemberton (JIRA)" <j...@apache.org>
Subject [jira] Commented: (BEANUTILS-335) Provide support for "fluid" beans
Date Fri, 13 Feb 2009 00:05:02 GMT

    [ https://issues.apache.org/jira/browse/BEANUTILS-335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12673095#action_12673095
] 

Niall Pemberton commented on BEANUTILS-335:
-------------------------------------------

I'm against adding this to BeanUtils - its always been based on "conforming to the JavaBeans
naming patterns for property getters and setters". IMO we should retain this principle and
reject requests that go against this. Its also unclear to me why something which is designed
to make the manual coding of the getting/setting of properties (i.e. method chaining) easier
would be of much benefit to a component which is designed to dynamically perform this task
through expressions.

> Provide support for "fluid" beans
> ---------------------------------
>
>                 Key: BEANUTILS-335
>                 URL: https://issues.apache.org/jira/browse/BEANUTILS-335
>             Project: Commons BeanUtils
>          Issue Type: New Feature
>          Components: Bean / Property Utils
>            Reporter: Dan Fabulich
>
> The attached patch allows users to easily define what I'm calling a "fluid" bean (though
there might be a better name for it).
> The idea here is to write a bean that doesn't follow the standard JavaBean convention.
 Specifically, a "fluid" bean's setters return "this," so you can "chain" calls to the setters,
and the getters and setters don't start with "get/set" but are just the name of the property.
 For example:
> {code}public class Employee extends AbstractFluidBean {
>   private String firstName, lastName;
>   public String firstName() { return firstName; }
>   public Employee firstName(String firstName) {
>     this.firstName = firstName;
>     return this;
>   }
>   public String lastName() { return lastName; }
>   public Employee lastName(String lastName) {
>     this.lastName = lastName;
>     return this;
>   }
> }{code}
> Fluid beans have some limitations: you can't use indexed or mapped properties with a
fluid bean (because there's no way to disambiguate an indexed getter from a simple setter).
 I think that's OK because indexed properties are a bit silly. (Why not just return a List
or a Map?)
> But I think they have substantial readability advantages.  With a fluid bean, you can
write code like this:
> {code}
> HumanResources.hire(new Employee().firstName("Dan").lastName("Fabulich"));
> {code}
> For an example of fluid chained setters in the wild, see (for example) Effective Java
Second Edition by Joshua Bloch.  In Item 2 "Consider a builder when faced with many constructor
parameters" Bloch defines a fluid bean with chained setters, so you can use it like this:
> {code}
> NutritionFacts cocoCola = new NutritionFacts.Builder(240, 8)
>   .calories(100).sodium(35).carbohydrate(27).build();
> {code}

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message