从JavaBeans更改为Builder模式

时间:2015-03-12 18:26:24

标签: java design-patterns javabuilders

我在我的应用程序中使用Retrofit,我有用于发出请求的POJO类并解析响应。目前我正在使用JavaBeans模式,并为所有POJO类的类中的所有变量提供getter和setter。

这是我的用户类

public class User {
    @SerializedName("user_name")
    private String userName;
    @SerializedName("password")
    private String userPassword;
    @SerializedName("address_info")
    private AddressInfo AddressInfo;
    @SerializedName("contact_info")
    private ContactInfo ContactInfo;

    /* 
      *@return The userName
    */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName The userName
     */

    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return The userPassword
     */

    public String getPassword() {
        return userPassword;
    }

    /**
     * @param userPassword The userPassword
     */

    public void setPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    /// so on for the AddressInfo and ContactInfo
}

这很有效。我正在尝试使用构建器模式而不是JavaBeans模式。

我将User类更改为:

public class User {
    @SerializedName("user_name")
    private String userName;
    @SerializedName("password")
    private String userPassword;
    @SerializedName("address_info")
    private AddressInfo AddressInfo;
    @SerializedName("contact_info")
    private ContactInfo ContactInfo;


    public static class UserBuilder {

        private String userName;
        private String userPassword;
        private AddressInfo AddressInfo;
        private ContactInfo ContactInfo;

        //builder methods for setting property
        public UserBuilder userName(String user_name) {
            this.userName = user_name;
            return this;
        }

        public UserBuilder userPassword(String user_password) {
            this.userPassword = user_password;
            return this;
        }

        public UserBuilder AddressInfo(AddressInfo addressInfo) {
            this.AddressInfo = addressInfo;
            return this;
        }

        public UserBuilder ContactInfo(ContactInfo contactInfo) {
            this.ContactInfo = contactInfo;
            return this;
        }

        //return fully build object
        public User build() {
            return new User(this);
        }
    }

    //private constructor to enforce object creation through builder

    private User(UserBuilder builder) {
        this.userName = builder.userName;
        this.userPassword = builder.userPassword;
        this.AddressInfo = builder.AddressInfo;
        this.ContactInfo = builder.ContactInfo;
    }

//Getters for userName,userPassword,AddressInfo and ContactInfo
}

AddressInfo类

public class AddressInfo {
    @SerializedName("address_1")
    private String address1;
    @SerializedName("city")
    private String city;
    @SerializedName("state")
    private String state;
    @SerializedName("zip_code")
    private String zipCode;

    /**
     * @return The address1
     */
    public String getAddress1() {
        return address1;
    }

    /**
     * @return The city
     */
    public String getCity() {
        return city;
    }

    /**
     * @return The state
     */
    public String getState() {
        return state;
    }

    /**
     * @return The zip code
     */
    public String getZipCode() {
        return zipCode;
    }
}

问题: 1.我从AddressInfo和ContactInfo POJO类中删除了setter。我是否还需要在AddressInfo和ContactInfo类中实现构建器模式?这是模式的工作原理。

  1. 我怎样才能做到这一点:

      User user = new User.UserBuilder().userName("test").userPassword("******").address("100 Townsend St").city("San Francisco").zip("94107").build();
    

2 个答案:

答案 0 :(得分:1)

  1. 您可以为所有课程实施该课程,包括 AddressInfo ContactInfo 。这种模式的想法是控制创建对象的过程,并避免当对象可能存在错误状态时(例如,没有名字和姓氏的用户)。可以通过在创建对象之前添加验证阶段来完成。

  2. 您可以按照以下所示实现它:

    static class Inner {
    
        String innerValue;
    
        static class Builder {
            String innerValue;
    
            Builder innerVal(final String innerValue) {
                this.innerValue = innerValue;
                return this;
            }
    
            Inner build() {
                Inner inner = new Inner();
                inner.innerValue = this.innerValue;
                return  inner;
            }
        }
    }
    
    static class Outer {
    
    String outerValue;
    
    Inner inner;
    
    static class Builder {
    
        Inner.Builder innerBuilder;
    
        String outerValue;
    
        Builder() {
            this.innerBuilder = new Inner.Builder();
        }
    
        Builder outerVal(final String outerValue) {
            this.outerValue = outerValue;
            return this;
        }
    
        Builder innerVal(final String innerValue) {
            this.innerBuilder.innerVal(innerValue);
            return this;
        }
    
        Outer build() {
            Outer outer = new Outer();
            outer.outerValue = this.outerValue;
            outer.inner = this.innerBuilder.build();
            return  outer;
        }
    }
    }
    
  3. 用法:

        final Outer outer = (new Outer.Builder())
                .innerVal("innerValue")
                .outerVal("outerValue")
                .build();
    

    但我个人试图将不同类的构建器分开,因为它简化了设计。

答案 1 :(得分:1)

User user = new User.UserBuilder().userName("test")
    .userPassword("******").address("100 Townsend St")
    .city("San Francisco").zip("94107").build();

你会把东西混在一起。 UserBuildr应负责使用其地址/联系信息构建用户,而不是构建用户。您应该在AddressInfo类中为ContactInfoUser的实例设置getter和setter,并且还有在UserBuilder中设置它们的方法。为每个人创建一个构建器并像这样使用它们。

User user = new User.UserBuilder().userName("test")
    .userPassword("******").addressInfo.(new AdressInfo.Builder()
        .address1("100 Townsend St")
        .city("San Francisco").zip("94107").build()).build();

ContactInfo执行相同操作。

如果您不希望外部可以访问AddressInfoContactInfo的setter,则可以使它们受到保护,构建器仍然可以访问它们,因为它是内部类。