0

in jQuery we often do this:

$('#el1').css('margin-left','5px').attr('title','test title').removeClass('class1'); 

in Java StringBuilder, same as above:

StringBuilder builder  = new StringBuilder();  
builder.append("str1").insert(0, 'A').deleteCharAt(2); 

So, what design pattern does this code follow?

Kurt Raschke
  • 960
  • 9
  • 19

2 Answers2

5

It's an application of the fluent interface pattern.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    ...and it was a brilliant idea. [Martin Fowler's article](http://www.martinfowler.com/bliki/FluentInterface.html) on it is somewhat more descriptive than Wikipedia. – Ryan Stewart Feb 25 '13 at 03:14
  • I though it was a Builder design pattern as sated [here](http://stackoverflow.com/a/2707195/1065197). – Luiggi Mendoza Feb 25 '13 at 04:23
  • 1
    @LuiggiMendoza [builders frequently use a fluent interface.](http://en.wikipedia.org/wiki/Builder_pattern#Useful_tips) `StringBuilder` is a fluent builder for `String`s, which should come as absolutely no surprise to you. – Matt Ball Feb 25 '13 at 04:25
  • Thanks for the informative link – Luiggi Mendoza Feb 25 '13 at 04:40
0

Its called as method chaining/named parameter idiom in OOP language. Refer http://en.wikipedia.org/wiki/Method_chaining

Jaydeep Rajput
  • 3,605
  • 17
  • 35