- 建造者抽象类或接口
public abstract class AbstractBuilder {
public abstract void setPart(List list);
public abstract AbstractProduct builderProduct();
}
- 建造者抽象类或接口实现类
public class ConcreteBuilder extends AbstractBuilder {
AbstractProduct product = new ConcreteProduct();
@Override
public void setPart(List list) {
//产品的组装顺序等内部逻辑
product.doSomething();
}
@Override
public AbstractProduct builderProduct() {
return this.product;
}
}
- 产品抽象类或接口
public abstract class AbstractProduct {
public abstract void doSomething();
}
- 产品抽象类或接口实现类
public class ConcreteProduct extends AbstractProduct {
@Override
public void doSomething() {
//具体操作
}
}
- 使用
public class Director {
private AbstractBuilder builder = new ConcreteBuilder();
public AbstractProduct getProduct() {
//设置不同的组成部分及顺序
builder.setPart(new ArrayList());
//返回创建的对象
return builder.builderProduct();
}
}
网友评论