做社区生意的网站,北京朝阳区天气预报,网站保持排名,企业网络推广方案文章目录 1. 概念1.1 角色1.2 类图 2. 代码示例2.1 设计2.2 代码2.3 类图 1. 概念
1.1 角色
Builder#xff08;抽象建造者#xff09;#xff1a;给出一个抽象接口#xff0c;以规范产品对象的各个组成成分的建造。ConcreteBuilder#xff08;具体建造者#xff09;抽象建造者给出一个抽象接口以规范产品对象的各个组成成分的建造。ConcreteBuilder具体建造者它们在应用程序调用下创建产品的实例Director导演者调用具体建造者角色以创建产品对象。 它没有产品类的具体知识真正拥有产品类的具体知识的是具体建造者。 Product产品产品便是建造中的复杂对象。
1.2 类图 #mermaid-svg-ZVmpo0k18WLf3zRt {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-ZVmpo0k18WLf3zRt .error-icon{fill:#552222;}#mermaid-svg-ZVmpo0k18WLf3zRt .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ZVmpo0k18WLf3zRt .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-ZVmpo0k18WLf3zRt .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ZVmpo0k18WLf3zRt .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ZVmpo0k18WLf3zRt .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ZVmpo0k18WLf3zRt .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ZVmpo0k18WLf3zRt .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ZVmpo0k18WLf3zRt .marker.cross{stroke:#333333;}#mermaid-svg-ZVmpo0k18WLf3zRt svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ZVmpo0k18WLf3zRt g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-ZVmpo0k18WLf3zRt g.classGroup text .title{font-weight:bolder;}#mermaid-svg-ZVmpo0k18WLf3zRt .nodeLabel,#mermaid-svg-ZVmpo0k18WLf3zRt .edgeLabel{color:#131300;}#mermaid-svg-ZVmpo0k18WLf3zRt .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-ZVmpo0k18WLf3zRt .label text{fill:#131300;}#mermaid-svg-ZVmpo0k18WLf3zRt .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-ZVmpo0k18WLf3zRt .classTitle{font-weight:bolder;}#mermaid-svg-ZVmpo0k18WLf3zRt .node rect,#mermaid-svg-ZVmpo0k18WLf3zRt .node circle,#mermaid-svg-ZVmpo0k18WLf3zRt .node ellipse,#mermaid-svg-ZVmpo0k18WLf3zRt .node polygon,#mermaid-svg-ZVmpo0k18WLf3zRt .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ZVmpo0k18WLf3zRt .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-ZVmpo0k18WLf3zRt g.clickable{cursor:pointer;}#mermaid-svg-ZVmpo0k18WLf3zRt g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-ZVmpo0k18WLf3zRt g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-ZVmpo0k18WLf3zRt .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-ZVmpo0k18WLf3zRt .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-ZVmpo0k18WLf3zRt .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-ZVmpo0k18WLf3zRt .dashed-line{stroke-dasharray:3;}#mermaid-svg-ZVmpo0k18WLf3zRt #compositionStart,#mermaid-svg-ZVmpo0k18WLf3zRt .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZVmpo0k18WLf3zRt #compositionEnd,#mermaid-svg-ZVmpo0k18WLf3zRt .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZVmpo0k18WLf3zRt #dependencyStart,#mermaid-svg-ZVmpo0k18WLf3zRt .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZVmpo0k18WLf3zRt #dependencyStart,#mermaid-svg-ZVmpo0k18WLf3zRt .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZVmpo0k18WLf3zRt #extensionStart,#mermaid-svg-ZVmpo0k18WLf3zRt .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZVmpo0k18WLf3zRt #extensionEnd,#mermaid-svg-ZVmpo0k18WLf3zRt .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZVmpo0k18WLf3zRt #aggregationStart,#mermaid-svg-ZVmpo0k18WLf3zRt .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZVmpo0k18WLf3zRt #aggregationEnd,#mermaid-svg-ZVmpo0k18WLf3zRt .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-ZVmpo0k18WLf3zRt .edgeTerminals{font-size:11px;}#mermaid-svg-ZVmpo0k18WLf3zRt :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Product ConcreteBuilder *Product product BuildPartA() BuildPartB() Builder BuildPartA() BuildPartB() Director Builder builder Construct() 2. 代码示例
2.1 设计
定义一个产品Product它包含PartA、PartB两个成员定义一个抽象建造者Builder 它有BuildPartA()、BuildPartB()两个方法 定义具体建造者ConcreteBuilder 它依赖产品Product它有BuildPartA()、BuildPartB()两个方法分别用来创建Product的两个部分。 定义导演者Director 它关联了建造者Builder它的Construct()方法调用建造者创建产品 调用 实例化一个产品 product实例化一个建造者builder它依赖产品实例product实例化一个导演者director它关联建造者builder用导演者director的Construct()方法创建产品打印结果检查
2.2 代码
package mainimport fmt// 定义产品
type Product struct {PartA stringPartB string
}// 定义抽象建造者
type Builder interface {BuildPartA(p string)BuildPartB(p string)
}// 定义具体建造者
type ConcreteBuilder struct {product *Product
}func (c *ConcreteBuilder) BuildPartA(p string) {c.product.PartA p
}func (c *ConcreteBuilder) BuildPartB(p string) {c.product.PartB p
}// 定义导演者
type Director struct {builder Builder
}func (d *Director) Construct(partAInfo string, partBInfo string) {d.builder.BuildPartA(partAInfo)d.builder.BuildPartB(partBInfo)
}// 测试代码
func main() {product : Product{}builder : ConcreteBuilder{product: product}director : Director{builder: builder}director.Construct(刀头, 刀柄)fmt.Println(product)
}2.3 类图 #mermaid-svg-bTcISIgHWuHFD7yp {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-bTcISIgHWuHFD7yp .error-icon{fill:#552222;}#mermaid-svg-bTcISIgHWuHFD7yp .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-bTcISIgHWuHFD7yp .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-bTcISIgHWuHFD7yp .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-bTcISIgHWuHFD7yp .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-bTcISIgHWuHFD7yp .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-bTcISIgHWuHFD7yp .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-bTcISIgHWuHFD7yp .marker{fill:#333333;stroke:#333333;}#mermaid-svg-bTcISIgHWuHFD7yp .marker.cross{stroke:#333333;}#mermaid-svg-bTcISIgHWuHFD7yp svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-bTcISIgHWuHFD7yp g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-bTcISIgHWuHFD7yp g.classGroup text .title{font-weight:bolder;}#mermaid-svg-bTcISIgHWuHFD7yp .nodeLabel,#mermaid-svg-bTcISIgHWuHFD7yp .edgeLabel{color:#131300;}#mermaid-svg-bTcISIgHWuHFD7yp .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-bTcISIgHWuHFD7yp .label text{fill:#131300;}#mermaid-svg-bTcISIgHWuHFD7yp .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-bTcISIgHWuHFD7yp .classTitle{font-weight:bolder;}#mermaid-svg-bTcISIgHWuHFD7yp .node rect,#mermaid-svg-bTcISIgHWuHFD7yp .node circle,#mermaid-svg-bTcISIgHWuHFD7yp .node ellipse,#mermaid-svg-bTcISIgHWuHFD7yp .node polygon,#mermaid-svg-bTcISIgHWuHFD7yp .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-bTcISIgHWuHFD7yp .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-bTcISIgHWuHFD7yp g.clickable{cursor:pointer;}#mermaid-svg-bTcISIgHWuHFD7yp g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-bTcISIgHWuHFD7yp g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-bTcISIgHWuHFD7yp .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-bTcISIgHWuHFD7yp .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-bTcISIgHWuHFD7yp .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-bTcISIgHWuHFD7yp .dashed-line{stroke-dasharray:3;}#mermaid-svg-bTcISIgHWuHFD7yp #compositionStart,#mermaid-svg-bTcISIgHWuHFD7yp .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-bTcISIgHWuHFD7yp #compositionEnd,#mermaid-svg-bTcISIgHWuHFD7yp .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-bTcISIgHWuHFD7yp #dependencyStart,#mermaid-svg-bTcISIgHWuHFD7yp .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-bTcISIgHWuHFD7yp #dependencyStart,#mermaid-svg-bTcISIgHWuHFD7yp .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-bTcISIgHWuHFD7yp #extensionStart,#mermaid-svg-bTcISIgHWuHFD7yp .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-bTcISIgHWuHFD7yp #extensionEnd,#mermaid-svg-bTcISIgHWuHFD7yp .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-bTcISIgHWuHFD7yp #aggregationStart,#mermaid-svg-bTcISIgHWuHFD7yp .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-bTcISIgHWuHFD7yp #aggregationEnd,#mermaid-svg-bTcISIgHWuHFD7yp .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-bTcISIgHWuHFD7yp .edgeTerminals{font-size:11px;}#mermaid-svg-bTcISIgHWuHFD7yp :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Product String PartA String PartB ConcreteBuilder *Product product BuildPartA(p:string) BuildPartB(p:string) Builder BuildPartA(p:string) BuildPartB(p:string) Director Builder builder Construct()