设计模式之建造者 Builder

2018-11-16  本文已影响1人  音符纸飞机

重点

适用于构建复杂的类,属性很多的类

简化版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;
    }
}
上一篇下一篇

猜你喜欢

热点阅读