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();
}
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
It chains these method calls, which is why this is called method chaining
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. :)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With