让java中的setter返回“this”是好还是坏?

public Employee setName(String name){
   this.name = name;
   return this;
}

这个模式很有用,因为你可以像这样设置链:

list.add(new Employee().setName("Jack Sparrow").setId(1).setFoo("bacon!"));

而不是这样:

Employee e = new Employee();
e.setName("Jack Sparrow");
...and so on...
list.add(e);

...但这有点违背标准惯例。我认为这是值得的因为它可以让setter做一些其他有用的事情。我曾在一些地方看到过这种模式(例如JMock、JPA),但它似乎并不常见,而且通常只用于定义良好的api,在这些api中这种模式随处可见。

更新:

我所描述的显然是有效的,但我真正想要的是一些关于这是否被普遍接受的想法,以及是否存在任何陷阱或相关的最佳实践。我知道Builder模式,但它比我所描述的要复杂一些——正如Josh Bloch所描述的,有一个用于对象创建的相关静态Builder类。


当前回答

它不仅打破了getter /setter的惯例,还打破了Java 8方法参考框架。MyClass::setMyValue是一个BiConsumer<MyClass,MyValue>, myInstance::setMyValue是一个Consumer<MyValue>。如果你让你的setter返回这个,那么它就不再是Consumer<MyValue>的有效实例,而是Function<MyValue,MyClass>,并且会导致任何使用这些setter的方法引用(假设它们是void方法)的事情中断。

其他回答

这个方案(双关语),称为“流畅界面”,现在变得非常流行。还可以接受,但不是我的菜。

我不懂Java,但我用c++做过。 也有人说这会让文字变得很长,很难读懂, 但我这样做过很多次:

list.add(new Employee()
    .setName("Jack Sparrow")
    .setId(1)
    .setFoo("bacon!"));

这个更好:

list.add(
    new Employee("Jack Sparrow")
    .Id(1)
    .foo("bacon!"));

至少我是这么认为的。但如果你愿意,欢迎你给我投反对票,称我为糟糕的程序员。我不知道Java中是否允许这样做。

如果在整个应用程序中使用相同的约定,则似乎没有问题。

另一方面,如果应用程序的现有部分使用标准约定,我会坚持下去,并向更复杂的类添加构建器

public class NutritionalFacts {
    private final int sodium;
    private final int fat;
    private final int carbo;

    public int getSodium(){
        return sodium;
    }

    public int getfat(){
        return fat;
    }

    public int getCarbo(){
        return carbo;
    }

    public static class Builder {
        private int sodium;
        private int fat;
        private int carbo;

        public Builder sodium(int s) {
            this.sodium = s;
            return this;
        }

        public Builder fat(int f) {
            this.fat = f;
            return this;
        }

        public Builder carbo(int c) {
            this.carbo = c;
            return this;
        }

        public NutritionalFacts build() {
            return new NutritionalFacts(this);
        }
    }

    private NutritionalFacts(Builder b) {
        this.sodium = b.sodium;
        this.fat = b.fat;
        this.carbo = b.carbo;
    }
}

是的,我认为这是个好主意。

如果我能补充点什么,关于这个问题:

class People
{
    private String name;
    public People setName(String name)
    {
        this.name = name;
        return this;
    }
}

class Friend extends People
{
    private String nickName;
    public Friend setNickName(String nickName)
    {
        this.nickName = nickName;
        return this;
    }
}

这是可行的:

new Friend().setNickName("Bart").setName("Barthelemy");

Eclipse将不接受这一点!:

new Friend().setName("Barthelemy").setNickName("Bart");

这是因为setName()返回People而不是Friend,并且没有PeoplesetNickName。

我们如何编写setter来返回SELF类而不是类名呢?

这样就可以了(如果SELF关键字存在的话)。这真的存在吗?

class People
{
    private String name;
    public SELF setName(String name)
    {
        this.name = name;
        return this;
    }
}

我过去更喜欢这种方法,但我已经决定不这么做了。

原因:

可读性。将每个setFoo()放在单独的行上可以使代码更具可读性。通常情况下,您阅读代码的次数要比编写代码的次数多得多。 副作用:setFoo()应该只设置字段foo,没有其他。返回this是一个额外的“WHAT was that”。

我看到的Builder模式没有使用setFoo(foo). setbar (bar)约定,而是使用了更多的foo(foo).bar(bar)。也许正是出于这些原因。

这总是一个品味问题。我只是喜欢“最少惊喜”的方法。