views:

118

answers:

3

Using Fluent Interface design here

if i call something like

 dog.Train("Running").Train("Eating").Do("Running").Do("Eating"); 

what is the name of this pattern ? is it chain-of-responsibility or there any specific design pattern name associated with it?

+4  A: 

Method chaining

EDIT:

From wikipedia article:

a fluent interface (as first coined by Eric Evans and Martin Fowler) is a way of implementing an object oriented API in a way that aims to provide for more readable code.

A fluent interface is normally implemented by using method chaining to relay the instruction context of a subsequent call (but a fluent interface entails more than just method chaining).

bniwredyc
I'm a bit skeptical about the relationship between "chaining" and "readability". I personally love chaining, because it's cool, and produces few lines of code... but is it really more readable when you have a big chain of events, than a sequence of operations where it's clearly defined what operation is being performed on what?
Mark
@Mark: I agree. Method chaining isn't always more readable. I think readable method chaining is a part of the fluent interface style.
bniwredyc
The point of method chaining (as used above) is that you can effectively use the language and the chained methods to implement a mini language, with the method calls taking the place of a lexer/parser. The other type of method chaining (`Foo.GetBar().GetBaz().GetQux().DoSomething()`) is often called a trainwreck, and is about as disastrous :)
kyoryu
+1  A: 

Chain of responsibility is more of a multiple class/object level pattern, where messages are passed down a hierarchy of objects, and each object can decide whether or not it can deal with the message or pass it on.

I think the pattern you show here is basically just a "Fluent Interface," like you said.

Andy White
Just wanted to know, Fluent interface itself is a design pattern or it is a technique?
I feel this is just a way to extend the concept of Method Chaining to the Interface level. That's it.
Amit
+1  A: 

I don't know that it's really a "pattern." It's more of an idiom.

At any rate, I'd call it either a fluent interface, or an internal Domain Specific Language.

kyoryu