Builder构建者模式,多参数构造函数的替代

/**
 * <pre>
 * BuilderExample.java
 * @author kanpiaoxue<br>
 * @version 1.0
 * Create Time 2014年7月13日 上午9:56:24<br>
 * Description : Builder Example
 * </pre>
 */
public class BuilderExample {

    public static void main(String[] args) {
        Person.Builder builder = Person.createBuilder();
        Person person = builder.setName("kanpiaoxue").setAge(22).setSex(1)
                .setCompany("kanpiaoxue.org").setCompanyAddress("Beiing")
                .setAddress("Haidian of the Beijing").build();
        System.out.println(person);
    }
}

 

/**
 * <pre>
 * Person.java
 * @author kanpiaoxue<br>
 * @version 1.0
 * Create Time 2014年7月13日 上午10:04:24<br>
 * Description : Person
 * </pre>
 */
public class Person {
    private final String name;
    private final int age;
    private final int sex;
    private final String address;
    private final String company;
    private final String companyAddress;

    private Person(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
        this.address = builder.address;
        this.sex = builder.sex;
        this.company = builder.company;
        this.companyAddress = builder.companyAddress;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getSex() {
        return sex;
    }

    public String getAddress() {
        return address;
    }

    public String getCompany() {
        return company;
    }

    public String getCompanyAddress() {
        return companyAddress;
    }

    public static Builder createBuilder() {
        return new Builder();
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", sex=" + sex
                + ", address=" + address + ", company=" + company
                + ", companyAddress=" + companyAddress + "]";
    }

    public static class Builder {
        private String name;
        private int age;
        private int sex;
        private String address;
        private String company;
        private String companyAddress;

        private Builder() {
            super();
        }

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

        public Builder setAge(int age) {
            this.age = age;
            return this;
        }

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

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

        public Builder setCompany(String company) {
            this.company = company;
            return this;
        }

        public Builder setCompanyAddress(String companyAddress) {
            this.companyAddress = companyAddress;
            return this;
        }

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

    }
}

 

你可能感兴趣的:(builder)