Does Java bean's setter permit return this?

后端 未结 10 548
一向
一向 2020-12-06 10:02

can I define setter method to return this rather than void?

Like:

ClassA setItem1() {
      return this;
}

ClassA setItem2() {
      return this;
}
         


        
相关标签:
10条回答
  • 2020-12-06 10:53

    No reason you couldn't do that. Personally, if the setters are being used during object creation, I'd name them withItem1() and withItem2().

    ClassA obj = new ClassA().withItem1(item1).withItem2(item2);
    

    Makes it a bit clearer (to me anyway) what the intent of the methods are.

    0 讨论(0)
  • 2020-12-06 10:53

    The Builder pattern is generally used for constructing immutable objects. Even though JavaBeans by nature aren't immutable, I have frequently used the builder pattern on my JavaBeans because it provides a fluent interface that I can use in my tests. The two are easily compatible with each other without breaking the JavaBean specification. You can check out it out on Stack Overflow at Builder Pattern in Effective Java

    You just need to make sure you include a default constructor as well as the private Builder constructor, and then put your standard getters and setters in the JavaBean object.

    This is much cleaner than constructor chaining, and easier to read as well.

    0 讨论(0)
  • 2020-12-06 10:58

    The JavaBeans Specification describes a JavaBean as:

    A Java Bean is a reusable software component that can be manipulated visually in a builder tool

    They are required to provide introspection, customization, events and persistence among other properties (Section 2.1: What is a bean?)

    It is common to call a "Java Bean" to a Plain Old Java Object with accessor methods following the JavaBeans Specification (Section 7.1 and 8.3). The truth is that such object could still be far from being compliant with all the requirements.

    If the object your are defining in this class is actually a JavaBean then your method must return void according to JavaBean Specification, section 7.1 where accessor methods are described as follows:

    void setFoo(PropertyType value); // simple setter
    PropertyType getFoo(); // simple getter
    

    The section 8.3 named designed patterns for properties says:

    By default, we use design patterns to locate properties by looking for methods of the form:

    public <PropertyType> get<PropertyName>();
    public void set<PropertyName>(<PropertyType> a);
    

    In addition, for boolean properties, we allow a getter method to match the pattern:

    public boolean is<PropertyName>();
    

    However, if your class is just a POJO then there is nothing wrong with using your method chaining strategy because you are allowed to deviate from the specification since you are not actually building a JavaBean. Not all the classes you define are supposed to be JavaBeans after all, right?

    Your might like to take a look at the Oracle JavaBeans Tutorial.

    0 讨论(0)
  • 2020-12-06 11:02

    Just to add that for people using Spring 3.1+ this is not an issue anymore

    see http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/new-in-3.1.html

    0 讨论(0)
提交回复
热议问题