Link Details

Link 9680 thumbnail
User 212444 avatar

By bscarr
via martinfowler.com
Published: Dec 22 2006 / 16:07

"A few months ago I attended a workshop with Eric Evans, and he talked about a certain style of interface which we decided to name a fluent interface. It's not a common style, but one we think should be better known. Probably the best way to describe it is by example."
  • 10
  • 0
  • 1864
  • 500

Comments

Add your comment
User 111696 avatar

bloid replied ago:

0 votes Vote down Vote up Reply

This is the original document from last year that this link references:

http://www.dzone.com/links/fluent_interfaces_in_php.html

User 179375 avatar

Ricky Clarkson replied ago:

1 votes Vote down Vote up Reply

I find it a bit odd. The code really depends on the formatting:

customer.newOrder()
    .with(6, "TAL")
    .with(5, "HPK").skippable()
    .with(3, "LGV")
    .priorityRush();

If that was all in one line, would it be obvious that skippable() applied only to (5, "HPK")? I don't think so.

I'd prefer with(5, skippable("HPK")) or something.

User 211272 avatar

clintonforbes replied ago:

0 votes Vote down Vote up Reply

Also, you would never build a customer order object in this way in a real application. You would be looping through the user's input and adding each line-item to the order object, setting the skippable property if selected by the user.

I think it is admirable to try to find new and better ways of coding, but having to return 'this' from each method so you can string together chains of calls like this seems a bit strange. In C++ we do this type of thing with the << operator so we can chain stream output like "cout << "Fred is " << personType << "!" << endl;". I'm not sure it works as well in this 'Fluent Interfaces' example.

User 203994 avatar

Mark Thomas replied ago:

0 votes Vote down Vote up Reply

I think it can be powerful and make it obvious what the code is doing. As one small example, in Ruby on Rails you can say 2.weeks.ago and there are lots of little things like that built in. It's very cool.

User 179375 avatar

Ricky Clarkson replied ago:

0 votes Vote down Vote up Reply

"Also, you would never build a customer order object in this way in a real application."

You might be for automated tests.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (10)



Voters Against This Link (0)