builder模式实例
Posted heapStark
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } } }
以上是关于builder模式实例的主要内容,如果未能解决你的问题,请参考以下文章