重点
适用于构建复杂的类,属性很多的类
简化版UML
Builder一般Builder类会作为Product的静态内部类
class Product{
String attributeA;
String attributeB;
public static class Builder{
Product product = new Product();
public Product setAttributeA(String a){
product.setAttributeA(a);
return product;
}
public Product setAttributeB(String b){
product.setAttributeB(b);
return product;
}
public Product build(){
return product;
}
}
public String getAttributeA() {
return attributeA;
}
public void setAttributeA(String attributeA) {
this.attributeA = attributeA;
}
public String getAttributeB() {
return attributeB;
}
public void setAttributeB(String attributeB) {
this.attributeB = attributeB;
}
}
网友评论