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."
Comments
bloid replied ago:
This is the original document from last year that this link references:
http://www.dzone.com/links/fluent_interfaces_in_php.html
Ricky Clarkson replied ago:
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.
clintonforbes replied ago:
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.
Mark Thomas replied ago:
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.
Ricky Clarkson replied ago:
"Also, you would never build a customer order object in this way in a real application."
You might be for automated tests.
Voters For This Link (10)
Voters Against This Link (0)