Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the pattern where you chain methods in your object by returning a reference to itself have a name?

In most modern OO languages chaining methods together is common, and IMHO elegant, practice. In jquery, for example, you often see code like:

$('div').addClass('container').css('color', 'white').length

Does writing your objects to allow this have a name?

like image 581
thomasmalt Avatar asked Sep 24 '10 12:09

thomasmalt


People also ask

What is the other name of chain of a responsibility design pattern?

The chain-of-responsibility pattern is structurally nearly identical to the decorator pattern, the difference being that for the decorator, all classes handle the request, while for the chain of responsibility, exactly one of the classes in the chain handles the request.

What is a method chain?

Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.

What is the correct situation for the use of a chain of responsibility pattern?

Chain of responsibility pattern is used to achieve loose coupling in software design where a request from client is passed to a chain of objects to process them.

What is called as chain of responsibility?

Chain of Responsibility is a behavioral design pattern that lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.


2 Answers

Method Chaining is the core concept behind building a Fluent Interface

like image 141
willcodejavaforfood Avatar answered Nov 06 '22 05:11

willcodejavaforfood


This is called a Fluent Interface.

Resources

  • FluentInterface on Martin Fowler's bliki
  • Fluent interface on Wikipedia
like image 21
Pascal Thivent Avatar answered Nov 06 '22 06:11

Pascal Thivent