- Method chaining
-
Method chaining is a common technique for invoking multiple method calls in object-oriented programming languages. Each method returns an object (possibly the current object itself), allowing the calls to be chained together in a single statement.[1] A method chain is also known as a train wreck due to an increasing amount of methods stacked after another in one line.[2]
- C#
Object.DoSomething().DoSomethingElse().DoAnotherThing();
- PHP
$object->do()->something()->else()
Method chaining is not required. It only potentially improves readability and reduces the amount of source code. It is the core concept behind building a fluent interface.
Contents
Examples
These examples illustrate how method chaining might be implemented and used:
C#
class Person { private string _name; private byte _age; public Person SetName(string name) { _name = name; return this; } public Person SetAge(byte age) { _age = age; return this; } public Person Introduce() { Console.WriteLine("Hello my name is {0} and I am {1} years old.", _name, _age); return this; } } //Usage: static void Main() { Person user = new Person(); // Output of this sequence will be: Hello my name is Peter and I am 21 years old. user.SetName("Peter").SetAge(21).Introduce(); }
C++
class Person { const char* name; int age; public: Person& SetName(const char* name) { Person::name = name; return *this; } Person& SetAge(int age) { Person::age = age; return *this; } void Introduce() { std::cout << "Hello, my name is " << name << " and I am " << age << " years old." << std::endl; } }; //Usage: int main() { Person user; // Output of this sequence will be: Hello my name is Peter and I am 21 years old. user.SetName("Peter").SetAge(21).Introduce(); }
Java
class Person { private final String name; private int age; public Person setName(final String name) { this.name = name; return this; } public Person setAge(final int AGE) { this.age = AGE; return this; } public void introduce() { System.out.println("Hello, my name is " + name + " and I am " + age + " years old."); } // Usage: public static void main(String[] args) { Person person = new Person(); // Output of this sequence will be: Hello, my name is Peter and I am 21 years old. person.setName("Peter").setAge(21).introduce(); } }
Actionscript 3
The following is an example of a class with method chaining implemented in Actionscript:
package examples.methodchaining { public class Person { private var _name:String; private var _age:uint; public function setName(value:String):Person { this._name = value; return this; } public function setAge(value:String):Person { this._age = value; return this; } public function introduce():void { trace("Hello, my name is " + _name + " and I am " + _age + " years old."); } } }
Basic usage:
package { import examples.methodchaining public class Main { public function Main():void { var person:Person = new Person(); // Output of this sequence will be: Hello, my name is Peter and I am 21 years old. person.setName("Peter").setAge(21).introduce(); } } }
PHP
class Person { private $_name; private $_age; public function setName($name) { $this->_name = $name; return $this; } public function setAge($age) { $this->_age = $age; return $this; } public function introduce() { printf( 'Hello my name is %s and I am %d years old.', $this->_name, $this->_age); } } $user = new Person(); // Output of this sequence will be: Hello my name is Peter and I am 21 years old. $user->setName('Peter')->setAge(21)->introduce();
See also
References
- ^ "Applying Method Chaining". http://firstclassthoughts.co.uk/: First Class Thoughts. http://firstclassthoughts.co.uk/java/method_chaining.html. Retrieved 2011-04-13. "In order to simplify repeated object interactions on the same object the old trick Method Chaining originating the world of Smalltalk should be enforced. The idea is to let methods return this rather than void, thus affecting especially set() and add() methods. Method chaining arose during the designers of Smalltalk persuit to minimize the number of keywords in the language, which lead to the discovery that void is an unnecesarry keyword!."
- ^ Martin, Robert Cecil (2008). Clean Code: A Handbook of Agile Software Craftsmanship. Prentice Hall. ISBN 0-13-235088-2.
External links
Categories:
Wikimedia Foundation. 2010.