Build模式

Builder模式,意思是我们new 对象的时候附带构造方法,如果里面的成员变量太多的话,我们根本不知道他是代表什么意思,由此我们可引入静态内部类类Builder类,可阅读性就明显提高了。

下面是实体类的构造方式,加入build类

public class UserDTO {
    private long id;
    private String userName;
    private String loginName;
    private String sex;
    private double height;
    private double weight;

    public UserDTO() {

    }

    public UserDTO(long id, String userName, String loginName, String sex, double height, double weight) {
        this.id = id;
        this.userName = userName;
        this.loginName = loginName;
        this.sex = sex;
        this.height = height;
        this.weight = weight;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

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

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    private UserDTO(Builder builder) {
        this.id = builder.id;
        this.userName = builder.userName;
        this.loginName = builder.loginName;
        this.sex = builder.sex;
        this.height = builder.height;
        this.weight = builder.weight;
    }

    //静态内部Builder类
    static class Builder {
        private long id;
        private String userName;
        private String loginName;
        private String sex;
        private double height;
        private double weight;


        public Builder setId(long id) {
            this.id = id;
            return this;
        }

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

        public Builder setLoginName(String loginName) {
            this.loginName = loginName;
            return this;
        }


        public Builder setSex(String sex) {
            this.sex = sex;
            return this;
        }


        public Builder setHeight(double height) {
            this.height = height;
            return this;
        }


        public Builder setWeight(double weight) {
            this.weight = weight;
            return this;
        }

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


 这个时候 我们在其他地方new对象的时候就是这样子的 

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        UserDTO.Builder builder = new UserDTO.Builder();
        UserDTO userDTO = builder
                .setId(1L)
                .setUserName("user")
                .setLoginName("admin")
                .setHeight(170.00)
                .setWeight(60.00)
                .setSex("男")
                .build();
    }
}

这样子 每个成员变量是不是都比较清晰了,像OKkttp的构造方法也是类似的这个的。

当然我们也可以直接使用

AA aa=new AA();

aa.setXX(XX);这个方法了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值