Hi all,

There are some places in the Click API where, IMHO, users would
benefit from the possibility of using method chaining. Briefly, this
involves returning "this" from a method so that you can call another
method on the same object.

Here is an example with the Column class:

  Table table = new Table("table");

  table.addColumn(new Column("id"));
  table.addColumn(new Column("name"));

  Column age = new Column("age");
  age.setTextAlign("right");
  table.addColumn(age);

Notice that for the third column, we want to use a different text
alignment. We have to break the flow and create a separate Column
object. We also have to make up a name for the variable, and we end up
needing 3 lines of code instead of 1.

With a "Column textAlign(String align)" method that calls
"setTextAlign(align)" and returns "this", we could write:

  Table table = new Table("table");

  table.addColumn(new Column("id"));
  table.addColumn(new Column("name"));
  table.addColumn(new Column("age").textAlign("right"));

I find this easier to write and easier to read. No need to break the
flow. What do you think?

Note that it is important *not* to change the original setter methods.
These must remain of the form "public void setXyz(value)" to respect
the JavaBean specification. The convention I've seen most often is to
name the method-chaining versions of the setter methods simply by
removing the "set" prefix.

Of course, people who prefer using the setter methods can continue
doing so. Since the method-chaining versions just call the
corresponding setter method and return "this", backward-compatibility
is preserved.

If you like this idea of adding method-chaining versions of setter
methods, of course there are likely other places in the Click API
that could benefit.

What do you think?

Thanks,
Freddy

Reply via email to