builder模式实例

package heapStark.blogCode.designPattern.builder;

public class BaseBean {
    private  int age;
    private  String name;

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "BaseBean{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public static class BaseBuilder<T extends BaseBuilder,P extends BaseBean>{
        protected P bean;

        public BaseBuilder(P bean) {
            this.bean = bean;
        }
        public int getAge() {
            return bean.getAge();
        }

        public T setAge(int age) {
            this.bean.setAge(age);
            return (T)this;
        }

        public String getName() {
            return bean.getName();
        }

        public T setName(String name) {
            this.bean.setName(name);
            return (T)this;
        }

        public P builder(){
            return bean;
        }
    }
}

  子类

package heapStark.blogCode.designPattern.builder;

public class Bean extends BaseBean {
    private String id;

    public String getId() {
        return id;
    }

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

    @Override
    public String toString() {
        return "Bean{" +
                "id='" + id + '\'' +
                "} " + super.toString();
    }

    public static class BeanBuilder extends BaseBean.BaseBuilder<BeanBuilder,Bean>{

        public BeanBuilder(Bean bean) {
            super(bean);
        }
        public BaseBuilder setId(String id) {
            this.bean.setId(id);
            return this;
        }
    }
}

  

posted @ 2018-05-29 21:39  heapStark  阅读(310)  评论(0编辑  收藏  举报