commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles <gil...@harfang.homelinux.org>
Subject Re: [Math] Fluent API, inheritance and immutability
Date Wed, 07 Aug 2013 23:13:28 GMT
On Wed, 7 Aug 2013 18:04:39 -0400, Evan Ward wrote:
> Hi,
>
> As a grateful user of the library, I would prefer the Fluent API and
> immutability. I have implemented this pattern in some of my own 
> projects
> and it makes the API very easy to use.
>
> Most of the time, when the interface is small, inheritance isn't 
> really
> necessary and I just implement the withXxx methods directly. A one 
> line
> 'return new ...' isn't hard to maintain with IDE support for renames 
> and
> add/remove parameters.
>
> When inheritance is part of the API you can parametrize the base 
> class
> and add an abstract create method, like so:
>
> abstract class Parent<T extends Parent> implements Interface{
>     protected abstract T create(...);
>     public T withXxx(x){
>         return create(x, ...);
>     }
> }
> class Child extends Parent<Child> {
>     protected T create(...){
>         return new Child(...);
>     }
> }

Did I understand correctly that the base class "Parent" is assumed to
have complete knowledge of all its subclass "Child": the signature of
"create" must contain the arguments for fields declared in "Child"?

Gilles

> The create method forces the implementing class to have an 
> appropriate
> constructor. The withXxx methods are implemented only once and the
> inheriting class only has to implement one more method. The type
> parameterization is invisible to the user if they use the Child class 
> or
> the Interface. The down side is that Child can not be subclassed
> correctly, but this enforces the rule "design for inheritance or 
> prevent
> it". This is similar to the pattern used in MarkupBuilder from 
> jatl.[1]
> (jatl uses a mutable base class)
>
> Regards,
> Evan
>
> [1]
> 
> http://code.google.com/p/jatl/source/browse/src/main/java/com/googlecode/jatl/MarkupBuilder.java
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message