让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类。


当前回答

第一眼看到:“可怕!”

进一步思考

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

实际上更不容易出错

Employee anEmployee = new Employee();
anEmployee.setName("xxx");
...
list.add(anEmployee);

非常有趣。添加想法toolbag…

其他回答

这可能可读性较差

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

或者这个

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

这比:

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

我同意所有声称这破坏了JavaBeans规范的帖子。有理由保留这一点,但我也觉得使用这个构建器模式(上面提到的)有它的一席之地;只要不是到处都用,就应该是可以接受的。对我来说,“It’s Place”的终点是对“build()”方法的调用。

There are other ways of setting all these things of course, but the advantage here is that it avoids 1) many-parameter public constructors and 2) partially-specified objects. Here, you have the builder collect what's needed and then call its "build()" at the end, which can then ensure that a partially-specified object is not constructed, since that operation can be given less-than-public visibility. The alternative would be "parameter objects", but that IMHO just pushes the problem back one level.

我不喜欢多形参构造函数,因为它们更有可能传入大量相同类型的实参,从而更容易将错误的实参传递给形参。我不喜欢使用大量的setter,因为对象可以在完全配置之前使用。此外,使用“build()”方法更好地实现基于先前选择的默认值的概念。

总之,我认为这是一个很好的实践,如果使用得当。

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

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

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

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;
    }
}

如果我正在编写一个API,我使用“return this”来设置只会设置一次的值。如果我有任何其他值,用户应该能够改变,我使用标准的void setter代替。

然而,在我看来,这真的是一个偏好问题,链式设置器看起来确实很酷。