Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
. Each method returns an object (possibly the current object itself), allowing the calls to be chained together in a single statement. A method chain is also known as a train wreck due to an increasing amount of methods stacked after another in one line.
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
Fluent interface
In software engineering, a fluent interface is an implementation of an object oriented API that aims to provide for more readable code....
.
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;
}
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
ActionScript
ActionScript is an object-oriented language originally developed by Macromedia Inc. . It is a dialect of ECMAScript , and is used primarily for the development of websites and software targeting the Adobe Flash Player platform, used on Web pages in the form of...
:
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.");
}
}
}
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;