美文网首页
设计模式之建造者 Builder

设计模式之建造者 Builder

作者: 音符纸飞机 | 来源:发表于2018-11-16 14:25 被阅读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;
        }
    }
    

    相关文章

      网友评论

          本文标题:设计模式之建造者 Builder

          本文链接:https://www.haomeiwen.com/subject/qyqufqtx.html