Java複雑なオブジェクトの作成方法


Javaでは、多くのオブジェクトを作成して、名前や年齢、電話、住所などの他の必須属性を作成することがよくあります.一般的な方法は、java beanのようなjava beanを定義することです.
public class People {

    private String  name;
    private Integer age;
    private String  gender;
    private String  address;
    private String  phone;

    public People(String name, Integer age){
        this.name = name;
        this.age = age;
    }

    public People(String name, Integer age, String gender, String address, String phone){
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.address = address;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}

 
次に、メソッドとsetメソッドを構築して、必要なインスタンスを生成します.
People p = new People("zhang san", 12);
p.setAddress("China");
p.setPhone("1234567");

 
オブジェクトが複雑で属性が多い場合は、多くの構造方法を書くか、多くのset方法を呼び出す必要がある.
 
改善された方法は、
public class People {

    private String  name;
    private Integer age;
    private String  gender;
    private String  address;
    private String  phone;

    public People(PeopleBuilder builder){
        this.name = builder.name;
        this.age = builder.age;
        this.gender = builder.gender;
        this.address = builder.address;
        this.phone = builder.phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public static class PeopleBuilder {

        private String  name;
        private Integer age;
        private String  gender;
        private String  address;
        private String  phone;

        public PeopleBuilder(String name, Integer age){
            this.name = name;
            this.age = age;
        }

        public PeopleBuilder gender(String gender) {
            this.gender = gender;
            return this;
        }

        public PeopleBuilder address(String address) {
            this.address = address;
            return this;
        }

        public PeopleBuilder phone(String phone) {
            this.phone = phone;
            return this;
        }
        
        public People build() {
            People p = new People(this);
            return p;
        }

    }

}

 
Peopleインスタンスの生成:
People p = new People.PeopleBuilder("li si", 32).address("USA").phone("12345").build();