<rdf:RDF
    xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    xmlns:s='http://snipsnap.org/rdf/snip-schema#'
    xml:base='http://www.peerbox.com:8668/rdf'>
    <s:Snip rdf:about='http://www.peerbox.com:8668/rdf#start/2006-01-20/1'
         s:name='start/2006-01-20/1'
         s:cUser='kgr'
         s:oUser='kgr'
         s:mUser='kgr'>
        <s:content>1 Fluent Interfaces {anchor:Fluent Interfaces}&#xA;Martin Fowler has an interesting article on what he calls {link:Fluent Interfaces|http://martinfowler.com/bliki/FluentInterface.html} (FI).  Basically, an FI is an interface which allows you to chain mutator calls without having to create temporary variables.  This style is of API is very convenient to use and it allows the code structure to more accurately represent the underlying data/object structure.  Hierarchical structures like XML DOM’s, GUI Controls, HTML, and nested business objects are all good candidates for this type of interface.&#xD;&#xA;&#xD;&#xA;&#xD;&#xA;Here’s a simple example:&#xD;&#xA;Instead of doing something this this:&#xD;&#xA;{code}&#xD;&#xA;Person p = new Person();&#xD;&#xA;p.setName(“Bob”);&#xD;&#xA;Address a = new Address();&#xD;&#xA;   a.setCity(“Bobville”);&#xD;&#xA;   a.setCountry(“Canada”);&#xD;&#xA;p.setAddress(a);&#xD;&#xA;function(p)&#xD;&#xA;{code}&#xD;&#xA;You could instead do something like this:&#xD;&#xA;{code}&#xD;&#xA;function(new Person()&#xD;&#xA;   .setName(“Bob”)&#xD;&#xA;   .setAddress(new Address()&#xD;&#xA;      .setCity(“Bobville”)&#xD;&#xA;      .setCountry(“Canada”)));&#xD;&#xA;{code}&#xD;&#xA;The second FI implementation reads much nicer as you don’t have any intermediate variables to deal with and the actual structure of the code mimics the structure of the data.  It almost has an XML feel to it.&#xD;&#xA;&#xD;&#xA;The way you implement this sort of thing of course is to have your setters/mutators simply return ‘this’.  Ex.&#xD;&#xA;Instead of:&#xD;&#xA;{code}&#xD;&#xA;public void setName(String name)&#xD;&#xA;{&#xD;&#xA;   name_ = name; &#xD;&#xA;} &#xD;&#xA;{code}&#xD;&#xA;You would do:&#xD;&#xA;{code}&#xD;&#xA;public Person setName(String name)&#xD;&#xA;{&#xD;&#xA;   name_ = name;  &#xD;&#xA;&#xD;&#xA;   return this;&#xD;&#xA;} &#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;1 Constructors&#xD;&#xA;An added bonus of FI’s is that you often no longer need to accept initial values in your constructors.  Many values are often only in the constructor as a matter of convenience, but with FI’s, it is already convenient.&#xD;&#xA;Why would you want to remove initial values from your constructors?  There are three reasons that I know of:&#xD;&#xA;&#xD;&#xA;1. So that your classes can be JavaBeans.  To be a JavaBean you need a no-argument constructor.&#xD;&#xA;1. So that your client code is more resilient to change.  Something like {code}new User().setFirstName(&quot;Kevin&quot;).setLastName(&quot;Greer&quot;){code} is more resilient than {code}new User(&quot;Kevin&quot;, &quot;Greer&quot;){code} in the event that in the future I add a middle-name property to both the Person class and the Person constructor.&#xD;&#xA;1. No need for multiple constructors or constructor explosions.  In example #2 above you could reasonably argue that you could just add an extra constructor which included the middle-name but have still kept the old two-argument constructor. The problem with this is that as you add more and more properties in the future you end up with a lot of old constructor signatures to maintain.  Even if you&apos;ve never changed the class you might feel compelled to add multiple constructors in order to support each possible combination of possible initial values that a user might want to provide.&#xD;&#xA;&#xD;&#xA;1 Problems in Java&#xD;&#xA;&#xD;&#xA;There is however a problem with implementing FI’s in Java and that problem is inheritance.&#xD;&#xA;Let&apos;s extend the example above:&#xD;&#xA;{code}&#xD;&#xA;class Employee extends Person {&#xD;&#xA;…&#xD;&#xA;   Person setSalary(long salary)&#xD;&#xA;   {&#xD;&#xA;   &#9;salary_ = salary;&#xD;&#xA;&#xD;&#xA;   &#9;return this;&#xD;&#xA;   } &#xD;&#xA;}&#xD;&#xA;{code}&#xD;&#xA;And now let’s use it:&#xD;&#xA;{code}&#xD;&#xA;new Employee()&#xD;&#xA;   .setName(“Bob”)&#xD;&#xA;   .setSalary(60000);&#xD;&#xA;&#xD;&#xA;{code}&#xD;&#xA;  &#xD;&#xA;But this doesn’t work!  Why?  Because setName() returns a regular Person, not an Employee and person doesn’t have the setSalary() method.  As soon as you start adding Fluent methods in subclasses, your FI becomes broken.&#xD;&#xA;&#xD;&#xA;1 Other Languages&#xD;&#xA;&#xD;&#xA;As I’ve shown above, FI’s don’t work in Java when you use inheritance because of Java’s static type checking.  In languages which perform run-time checking instead, such as Ruby, Smalltalk, Io, etc., this isn’t a problem.  However, many of these languages actually have better language-based solutions to the problem so that you don’t need to implement FI&apos;s at the API level.&#xD;&#xA;&#xD;&#xA;1 Io&#xD;&#xA;{code}&#xD;&#xA;   p := Person clone do (&#xD;&#xA;     setName “Bob”&#xD;&#xA;     setSalary 60000&#xD;&#xA;   )&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;1 Ruby&#xD;&#xA;{code}&#xD;&#xA;p = Person.new do |m|&#xD;&#xA;  m.setName(&quot;Bob&quot;)&#xD;&#xA;  m.setSalary(60000)&#xD;&#xA;end&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;1 Smalltalk&#xD;&#xA;Smalltalk has a very nice feature called “cascading message sends”.  It lets you send multiple messages (make multiple method calls in Java terminology) to the same object without having to keep repeating the object.  In Smalltalk the period (.) is like Java’s semi-colon (;) end-of-statement marker.  This leaves the semi-colon symbol free for use in describing a new method call which is related to the current object but still separate from the previous method call.  Ex. Instead of doing this:&#xD;&#xA;{code}&#xD;&#xA;obj call1.&#xD;&#xA;obj call2&#xD;&#xA;…&#xD;&#xA;obj calln.&#xD;&#xA;{code}&#xD;&#xA;You could just do this:&#xD;&#xA;{code}&#xD;&#xA;obj call1; call2; … calln.&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;So if we rewrite our FI example in Smalltalk with cascading message sends we get this:&#xD;&#xA;{code}&#xD;&#xA;p := (Person new)&#xD;&#xA;setName “Bob” ;&#xD;&#xA;setSalary 60000 .&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;Smalltalk’s behaviour is more like English.&#xD;&#xA;&#xD;&#xA;1 Improving Java&#xD;&#xA;It is unfortunate that Java chose to stick with C’s semicolon delimiters rather than adopting Smalltalk’s use of semicolons and periods.  I guess that wasn’t the point of Java; if Java had wanted to do everything that Smalltalk did, it could have just been Smalltalk in the first place.  An approach that would work for Java would be to add something like Io’s “do” command.  Maybe it would look something like this:&#xD;&#xA;&#xD;&#xA;{code}&#xD;&#xA;   p.{&#xD;&#xA;      setName(“Bob”);&#xD;&#xA;      setSalary(60000);&#xD;&#xA;};&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;The idea being to introduce an implicit ‘this’ within a set of braces.&#xD;&#xA;&#xD;&#xA;1 Summary&#xD;&#xA;Fluent Interfaces make API usage much more convenient and natural but don’t work well with inheritance in Java.  Other languages provide better support for FI’s; often doing so at the language level without requiring API designers to add any explicit support. &#xD;&#xA;</s:content>
        <s:mTime>2006-01-20 13:37:22.78</s:mTime>
        <s:cTime>2006-01-20 13:37:22.78</s:cTime>
        <s:comments>
            <rdf:Bag>
                <rdf:li>
                    <s:Comment rdf:about='http://www.peerbox.com:8668/rdf#comment-start/2006-01-20/1-1'
                         s:name='comment-start/2006-01-20/1-1'
                         s:cUser='DaveHunter'
                         s:oUser='DaveHunter'
                         s:mUser='DaveHunter'>
                        <s:content>I don&apos;t think you mentioned that Smalltalk methods return self by default.&#xD;&#xA;Java returns nothing - I still remember having an empty feeling when I first learned Java. Java Languages would be much more fluid if the methods returned &apos;this&apos; by default.</s:content>
                        <s:mTime>2006-01-21 14:07:53.099</s:mTime>
                        <s:cTime>2006-01-21 14:07:53.023</s:cTime>
                        <s:commentedSnip rdf:resource='http://www.peerbox.com:8668/rdf#start/2006-01-20/1'/>
                    </s:Comment>
                </rdf:li>
                <rdf:li>
                    <s:Comment rdf:about='http://www.peerbox.com:8668/rdf#comment-start/2006-01-20/1-2'
                         s:name='comment-start/2006-01-20/1-2'>
                        <s:content>I recently implemented this very pattern in a web application, in which I wanted to represent the current state (in the sense of REST) in such a way that I could say&#xD;&#xA;&#xD;&#xA;{code}&#xD;&#xA;state.getNextPage().getRest()&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;to get the URL which represents &quot;the next page of results&quot;.&#xD;&#xA;&#xD;&#xA;In order to permit construction of arbitrary, unknown future subclasses, I use the factory method.&#xD;&#xA;&#xD;&#xA;{code}&#xD;&#xA;// pseudo-code&#xD;&#xA;class StateWithPage implements State {&#xD;&#xA;  public State getNextPage() {&#xD;&#xA;     return createState(this.page + 1);&#xD;&#xA;  }&#xD;&#xA;&#xD;&#xA;  protected State createState(final int page) {&#xD;&#xA;     return new StateWithPage(page);&#xD;&#xA;  }&#xD;&#xA;}&#xD;&#xA;&#xD;&#xA;class StateWithPageAndColor extends StateWithPage {&#xD;&#xA;  protected State createState(final int page) {&#xD;&#xA;    return new StateWithPageAndColor(page, this.color);&#xD;&#xA;  }&#xD;&#xA;}&#xD;&#xA;{code}</s:content>
                        <s:cUser>Jonathan Feinberg</s:cUser>
                        <s:oUser>Jonathan Feinberg</s:oUser>
                        <s:mUser>Jonathan Feinberg</s:mUser>
                        <s:mTime>2006-02-22 16:10:38.172</s:mTime>
                        <s:cTime>2006-02-22 16:10:38.044</s:cTime>
                        <s:commentedSnip rdf:resource='http://www.peerbox.com:8668/rdf#start/2006-01-20/1'/>
                    </s:Comment>
                </rdf:li>
            </rdf:Bag>
        </s:comments>
        <s:snipLinks>
            <rdf:Bag>
                <rdf:li rdf:resource='http://www.peerbox.com:8668/rdf#2006'/>
                <rdf:li rdf:resource='http://www.peerbox.com:8668/rdf#start/2006-01-22/1'/>
                <rdf:li rdf:resource='#snipsnap-index'/>
                <rdf:li rdf:resource='#DaveHunter'/>
                <rdf:li rdf:resource='#snipsnap-search'/>
                <rdf:li rdf:resource='#kgr'/>
            </rdf:Bag>
        </s:snipLinks>
        <s:attachments
             rdf:type='http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag'/>
    </s:Snip>
</rdf:RDF>
