hawkeye
hawkeye

Reputation: 35702

What is the name of the design pattern where you chain method results of the same object?

I've seen this design pattern and I can't remember the name:

class Number {
  private int myNumber;
  public Number(int arg) {this.myNumber = arg;}
  public Number add (int arg) {return new Number(myNumber + arg);}
  public Number subtract (int arg) {return new Number(myNumber - arg);}
}

So the way you use it is:

Object result = (new Number(1)).add(2).subtract(1).add(3);

The point being you can keep chaining the method results together.

My question is: What is the name of the design pattern where you chain method results of the same object?

Upvotes: 0

Views: 108

Answers (2)

Kedar Tokekar
Kedar Tokekar

Reputation: 418

Just calling methods of a class in a chain does not qualify itself to be a pattern. Design patterns are documented at design level and those solutions are implementation independent. There will be patterns whose code and UML diagram will look similar but due to its different intent they represent implementations of different patterns (e.g. State and Strategy). But calling such a chain for building object with possibility of permutation combinations of many optional parameters, is seen in Builder pattern. Where original object is created with mandatory parameters and reconfigured by builder under its strict control as per the requirement of client without breaking encapsulation. "Effective Java: A Programming Language Guide" by Joshua Bloch has demonstrated effective usage of this kind of Builder pattern in the book.

Upvotes: 0

andreybleme
andreybleme

Reputation: 689

I've heard as Fluent Interface. This makes harder to debug in some cases, the entire chain of actions may be considered a single statement so in my opinion: not such a good idea.

Upvotes: 1

Related Questions