Setters return a reference to an instance. Pattern or anti pattern?

I was thinking about code structure and thinking about setters. They were used as invalid methods, so why not use any possible return value to include some new code structure?

My idea was to change all seters properties from void to instance reference so that we can do setters or something else in sequence. Here is an example:

public class MyClass { private int foo; private String bar; public MyClass setFoo(int foo) { this.foo = foo; return this; } public MyClass setBar(String bar) { this.bar = bar; return this; } } 

Then in some other place in the code we could do:

 ... MyClass myInstance = new MyClass(); myInstance.setFoo(auxFoo).setBar(auxBar); ... 

This allows you to set all the properties of the class on a single line, useful in conversion methods.

Or even:

 ... return myInstance.setFoo(auxFoo); 

It was my goal, for example, to set the error property by returning it. This can simplify catch locking, for example.

EDIT: After some answers I need to add:

  • The question is only about setters (not about doing this in all methods), and is not limited to the chain, but for other applications, such as the return example.
  • Can a change from void to something else create any problems? For example, Java Inspects.
  • Can you see more advantages or disadvantages of this?

I was hoping to see some discussion.

+6
source share
5 answers

This is a common technique called Method Method Chain . Hibernate uses it in its Criteria classes and is present in other popular environments such as Wicket.

As a rule, you should be careful and apply it in those void methods that you are sure that you will never need to return anything. And you should not use it in your Java Beans , as already discussed in this question: Does the Java bean setter allow this? .

See this related SO question for some tips and tricks that might be helpful when using this template.

+12
source

This is a fairly common practice and definitely a pattern - not an anti-pattern. It is often called:

http://en.wikipedia.org/wiki/Fluent_interface

and

http://en.wikipedia.org/wiki/Method_chaining

Some excellent libraries use it: jQuery and joda-time.

You can also specify when the chain ends using the end method, which returns nothing or does something else completely - in the case of an internal static builder, it calls the constructor.

Why do I really like it.

+6
source

I think this template has its uses and there is nothing wrong with it.

I personally use it from time to time. Well-established libraries, such as Gson , exist . Despite the fact that Java StringBuilder.append() not strictly a setter, it is not different in spirit.

+3
source

I would not recommend doing this, as many things rely on a particular method signature for setters to allow functions like Bean. I don’t know for sure, but it can stop things like spring, or jsf, or other technologies that involve Bean-like setters.

+2
source

This applies to a number of well-known and recognized models.

The general idea of ​​getting methods returns a link in order to be able to call more methods, called Method Chain .

A more specific example of this type of method chain is when methods are used to set the properties of the instance used to enter the function. This is popular in C ++ and is called the Named Parameter Identifier .

+2
source

Source: https://habr.com/ru/post/898753/


All Articles