1

我在我的应用程序中使用 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 模式。

我将我的用户类更改为:

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
}

地址信息类

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();
    
4

2 回答 2

2

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

你会把东西混在一起。UserBuildr应该负责使用其地址/联系信息构建用户,而不是构建它们。您应该为类的实例AddressInfoContactInfoUser类中具有 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外部可访问,则可以使它们受到保护,构建器仍然可以访问它们,因为它是内部类。

于 2015-03-14T15:19:54.397 回答
1
  1. 您可以为所有类实现它,包括AddressInfoContactInfo。这种模式的想法是控制创建对象的过程并避免对象可能以错误状态存在的情况(例如,没有名字和姓氏的用户)。可以通过在创建对象之前添加验证阶段来完成。

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

用法:

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

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

于 2015-03-14T14:34:56.710 回答