码迷,mamicode.com
首页 > 其他好文 > 详细

builder模式实例

时间:2018-05-29 22:58:21      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:code   set   err   uil   static   pst   ati   ebean   int   

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

  

builder模式实例

标签:code   set   err   uil   static   pst   ati   ebean   int   

原文地址:https://www.cnblogs.com/heapStark/p/9108083.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!