Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the term for design ala "object.method1().method2().method3()"?

Tags:

c++

What's the term for this design?

object.method1().method2().method3()

..when all methods return *this?

I found the term for this a while ago, but lost it meanwhile. I have no clue how to search for this on google :) Also if anyone can think of a better title for the question, feel free to change it.

Thanks

Update-Gishu: After reading about it, I feel that your question is misleading w.r.t. code snippet provided.. (Feel free to rollback)

Method Chaining

object.method1().method2().method3()

Fluent Interfaces

private void makeFluent(Customer customer) {
        customer.newOrder()
                .with(6, "TAL")
                .with(5, "HPK").skippable()
                .with(3, "LGV")
                .priorityRush();
    }
like image 507
Prody Avatar asked Oct 15 '08 11:10

Prody


4 Answers

Looks to me like you are describing a fluent interface. Ive also heard it referred to as pipelineing or chaining.

Update-Gishu: http://martinfowler.com/bliki/FluentInterface.html

like image 141
Joel Cunningham Avatar answered Nov 10 '22 11:11

Joel Cunningham


It chains these method calls, which is why this is called method chaining

like image 35
PW. Avatar answered Nov 10 '22 10:11

PW.


It's usually called method chaining. An example of its application is the Named Parameter Idiom.

As an aside, I find it amusing that searching in Google for "object method1 method2" comes up with exactly the page you were looking for. :)

like image 8
Greg Hewgill Avatar answered Nov 10 '22 11:11

Greg Hewgill


It's called Method Chaining. As an example, there's a boost library that provided a chaining way of assigning into a container before brace-initialization came around (Boost.Assignment):

vector<int> v; 
v += 1,2,3,4,5,6,7,8,9;

typedef pair< string,string > str_pair;
deque<str_pair> deq;
push_front( deq )( "foo", "bar")( "boo", "far" ); 

Typically though, you see it more in other languages to do things like providing a fluent interface:

 FluentGlutApp(argc, argv)
     .withDoubleBuffer().withRGBA().withAlpha().withDepth()
     .at(200, 200).across(500, 500)
     .named("My OpenGL/GLUT App")
     .create();

I don't see it that much in C++ personally, outside of streaming.

like image 3
Barry Avatar answered Nov 10 '22 09:11

Barry