public class Product {
private String aaa;
private String bbb;
public String getAaa() {
return aaa;
}
public void setAaa(String aaa) {
this.aaa = aaa;
}
public String getBbb() {
return bbb;
}
public void setBbb(String bbb) {
this.bbb = bbb;
}
@Override
public String toString() {
return "Product [aaa=" + aaa + ", bbb=" + bbb + "]";
}
}
public interface ProductBuilder {
void buildAaa();
void buildBbb();
Product getProduct();
}
public class AProduct implements ProductBuilder{
private Product product = new Product();
@Override
public void buildAaa() {
product.setAaa("AProduct-aaa");
}
@Override
public void buildBbb() {
product.setBbb("AProduct-bbb");
}
@Override
public Product getProduct() {
return product;
}
}
public class BProduct implements ProductBuilder{
private Product product = new Product();
@Override
public void buildAaa() {
product.setAaa("BProduct-aaa");
}
@Override
public void buildBbb() {
product.setBbb("BProduct-bbb");
}
@Override
public Product getProduct() {
return product;
}
}
public class Director {
public Product build(ProductBuilder productBuilder) {
productBuilder.buildAaa();
productBuilder.buildBbb();
return productBuilder.getProduct();
}
}
public class Client {
public static void main(String[] args) {
AProduct aProduct = new AProduct();
BProduct bProduct = new BProduct();
Director director = new Director();
Product aObj = director.build(aProduct);
Product bObj = director.build(bProduct);
System.out.println("aObj==>" + aObj);
System.out.println("bObj==>" + bObj);
}
}
网友评论