views:

211

answers:

5

jQuery uses this pattern. Essentially it involves every method returning a reference to the same object on which the method was called.

myClassInstance
    .DoMethodA()
    .DoMethodB()
    .DoMethodC()
    .CleanUp();

What's this design pattern called?

UPDATE The accepted answer is correct, and here is the link to the wikipedia entry for it - less informative than the link provided in the answer though :P http://en.wikipedia.org/wiki/Method_chaining

+10  A: 

method chaining

dfa
+4  A: 

Fluent interface

mghie
Almost correct - turns out Fluent Interface is an application of method chaining, which is the correct answer. Thanks for the link though; great info there.
Nathan Ridley
+1  A: 

its called a Fluent Interface

John Nolan
A: 

I've never seen a widely used name for the practice, but personally I call it daisy chaining.

anon
A: 

I call it a train wreck.

G B
I call opinionated comments pointless when unaccompanied by any kind of explanation.
Nathan Ridley
-1: This pattern is sometimes extremely useful.
Brian MacKay
My *personal opinion*:- It can be useful, but never necessary. It's some sort of syntactic sugar.- It's easy to abuse. It's extremely anti-functional, and relies on methods which only have side-effects (setters, at least 99% of the times).- This particular example is not very "fluent". You can write fluent interfaces without method chaining, and use method chaining in non-fluent interfaces (Java 1.5 has varargs, you don't need to mock them anymore)- The main problem: this is a single line of code, hard to debug.- This has nothing to do with *design* patterns.
G B