微信关联网站,青岛网站建设 上流,多人在线网站开发,合肥住房和建设厅网站首页文章目录 1. 概述1.1 角色1.2 类图 2. 代码示例2.1 设计2.2 代码2.3 类图 1. 概述
1.1 角色
AbstractFactory#xff08;抽象工厂#xff09;#xff1a;它声明了一组用于创建产品的方法#xff0c;每一个方法对应一种产品。ConcreteFactory#xff08;具体工厂#xf… 文章目录 1. 概述1.1 角色1.2 类图 2. 代码示例2.1 设计2.2 代码2.3 类图 1. 概述
1.1 角色
AbstractFactory抽象工厂它声明了一组用于创建产品的方法每一个方法对应一种产品。ConcreteFactory具体工厂它实现了在抽象工厂中声明的创建产品的方法生成一组具体产品这些产品构成了一个产品族每一个产品都位于某个产品等级结构中。AbstractProduct抽象产品它为每种产品声明接口在抽象产品中声明了产品所具有的业务方法。ConcreteProduct具体产品它定义具体工厂生产的具体产品对象实现抽象产品接口中声明的业务方法。
1.2 类图 #mermaid-svg-w6Wb5CqOiuehJgY8 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-w6Wb5CqOiuehJgY8 .error-icon{fill:#552222;}#mermaid-svg-w6Wb5CqOiuehJgY8 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-w6Wb5CqOiuehJgY8 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-w6Wb5CqOiuehJgY8 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-w6Wb5CqOiuehJgY8 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-w6Wb5CqOiuehJgY8 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-w6Wb5CqOiuehJgY8 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-w6Wb5CqOiuehJgY8 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-w6Wb5CqOiuehJgY8 .marker.cross{stroke:#333333;}#mermaid-svg-w6Wb5CqOiuehJgY8 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-w6Wb5CqOiuehJgY8 g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-w6Wb5CqOiuehJgY8 g.classGroup text .title{font-weight:bolder;}#mermaid-svg-w6Wb5CqOiuehJgY8 .nodeLabel,#mermaid-svg-w6Wb5CqOiuehJgY8 .edgeLabel{color:#131300;}#mermaid-svg-w6Wb5CqOiuehJgY8 .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-w6Wb5CqOiuehJgY8 .label text{fill:#131300;}#mermaid-svg-w6Wb5CqOiuehJgY8 .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-w6Wb5CqOiuehJgY8 .classTitle{font-weight:bolder;}#mermaid-svg-w6Wb5CqOiuehJgY8 .node rect,#mermaid-svg-w6Wb5CqOiuehJgY8 .node circle,#mermaid-svg-w6Wb5CqOiuehJgY8 .node ellipse,#mermaid-svg-w6Wb5CqOiuehJgY8 .node polygon,#mermaid-svg-w6Wb5CqOiuehJgY8 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-w6Wb5CqOiuehJgY8 .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-w6Wb5CqOiuehJgY8 g.clickable{cursor:pointer;}#mermaid-svg-w6Wb5CqOiuehJgY8 g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-w6Wb5CqOiuehJgY8 g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-w6Wb5CqOiuehJgY8 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-w6Wb5CqOiuehJgY8 .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-w6Wb5CqOiuehJgY8 .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-w6Wb5CqOiuehJgY8 .dashed-line{stroke-dasharray:3;}#mermaid-svg-w6Wb5CqOiuehJgY8 #compositionStart,#mermaid-svg-w6Wb5CqOiuehJgY8 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w6Wb5CqOiuehJgY8 #compositionEnd,#mermaid-svg-w6Wb5CqOiuehJgY8 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w6Wb5CqOiuehJgY8 #dependencyStart,#mermaid-svg-w6Wb5CqOiuehJgY8 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w6Wb5CqOiuehJgY8 #dependencyStart,#mermaid-svg-w6Wb5CqOiuehJgY8 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w6Wb5CqOiuehJgY8 #extensionStart,#mermaid-svg-w6Wb5CqOiuehJgY8 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w6Wb5CqOiuehJgY8 #extensionEnd,#mermaid-svg-w6Wb5CqOiuehJgY8 .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w6Wb5CqOiuehJgY8 #aggregationStart,#mermaid-svg-w6Wb5CqOiuehJgY8 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w6Wb5CqOiuehJgY8 #aggregationEnd,#mermaid-svg-w6Wb5CqOiuehJgY8 .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-w6Wb5CqOiuehJgY8 .edgeTerminals{font-size:11px;}#mermaid-svg-w6Wb5CqOiuehJgY8 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} «interface» ProductA ProductA1 ProductA2 «interface» ProductB ProductB1 ProductB2 Client «interface» AbstractFactory CreateProductA() : ProductA CreateProductB() : ProductB ConcreteFactory1 CreateProductA() : ProductA CreateProductB() : ProductB ConcreteFactory2 CreateProductA() : ProductA CreateProductB() : ProductB 2. 代码示例
2.1 设计
定义抽象产品A 定义具体产品A-1 它有Name、Weight两个成员它的SetName()、SetWeight()两个方法分别负责设置以上两个成员它的Get()方法获取它本身的信息 定义具体产品A-2 它有Name、Weight两个成员它的SetName()、SetWeight()两个方法分别负责设置以上两个成员它的Get()方法获取它本身的信息 定义抽象产品B 定义具体产品B-1 它有Name、Height两个成员它的SetName()、SetHeight()两个方法分别负责设置以上两个成员它的Get()方法获取它本身的信息 定义具体产品B-2 它有Name、Height两个成员它的SetName()、SetHeight()两个方法分别负责设置以上两个成员它的Get()方法获取它本身的信息 定义抽象工厂 定义具体工厂1 它的方法SetProductA()设置具体产品A-1它的方法SetProductB()设置具体产品B-1 定义具体工厂2 它的方法SetProductA()设置具体产品A-2它的方法SetProductB()设置具体产品B-2 定义一个函数CreateFactory()用来创建具体工厂调用 用CreateFactory()函数实例化 具体工厂1——factory1用factory1实例化产品A-1和产品B-1并查看结果用CreateFactory()函数实例化 具体工厂2——factory2用factory2实例化产品A-2和产品B-2并查看结果
2.2 代码
package mainimport fmt// 定义抽象产品A
type AbstractProductA interface {SetName(name string)SetWeight(weight int64)Get()
}// 定义具体产品A-1
type ConcreteProductA1 struct {Name stringWeight int64
}func (c *ConcreteProductA1) SetName(name string) {c.Name name
}func (c *ConcreteProductA1) SetWeight(weight int64) {c.Weight weight
}func (c *ConcreteProductA1) Get() {fmt.Printf(%v\n, c)
}// 定义具体产品A-2
type ConcreteProductA2 struct {Name stringWeight int64
}func (c *ConcreteProductA2) SetName(name string) {c.Name name
}func (c *ConcreteProductA2) SetWeight(weight int64) {c.Weight weight
}
func (c *ConcreteProductA2) Get() {fmt.Printf(%v\n, c)
}// 定义抽象产品B
type AbstractProductB interface {SetName(name string)SetHeight(height int64)Get()
}// 定义具体抽象产品B-1
type ConcreteProductB1 struct {Name stringHeight int64
}func (c *ConcreteProductB1) SetName(name string) {c.Name name
}func (c *ConcreteProductB1) SetHeight(height int64) {c.Height height
}
func (c *ConcreteProductB1) Get() {fmt.Printf(%v\n, c)
}// 定义具体产品B-2
type ConcreteProductB2 struct {Name stringHeight int64
}func (c *ConcreteProductB2) SetName(name string) {c.Name name
}func (c *ConcreteProductB2) SetHeight(height int64) {c.Height height
}
func (c *ConcreteProductB2) Get() {fmt.Printf(%v\n, c)
}// Factory部分// 定义抽象工厂
type Factory interface {SetProductA(name string, weight int64) AbstractProductASetProductB(name string, height int64) AbstractProductB
}// 定义具体工厂1
type Factory1 struct {
}func (f *Factory1) SetProductA(name string, weight int64) AbstractProductA {a : ConcreteProductA1{}a.SetName(name)a.SetWeight(weight)return a
}func (f *Factory1) SetProductB(name string, height int64) AbstractProductB {a : ConcreteProductB1{}a.SetName(name)a.SetHeight(height)return a
}// 定义具体工厂2
type Factory2 struct {
}func (f *Factory2) SetProductA(name string, weight int64) AbstractProductA {a : ConcreteProductA2{}a.SetName(name)a.SetWeight(weight)return a
}func (f *Factory2) SetProductB(name string, height int64) AbstractProductB {a : ConcreteProductB2{}a.SetName(name)a.SetHeight(height)return a
}// 写一个创建工厂的函数
func CreateFactory(pType int64) Factory {switch pType {case 1:return Factory1{}case 2:return Factory2{}}return nil
}func main() {factory1 : CreateFactory(1)factory1.SetProductA(A1, 3).Get()factory1.SetProductB(B1, 3).Get()factory2 : CreateFactory(2)factory2.SetProductA(A2, 4).Get()factory2.SetProductB(B2, 4).Get()
}
输出
{A1 3}
{B1 3}
{A2 4}
{B2 4}2.3 类图 #mermaid-svg-AB2nzvVqhfT0VZWD {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-AB2nzvVqhfT0VZWD .error-icon{fill:#552222;}#mermaid-svg-AB2nzvVqhfT0VZWD .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-AB2nzvVqhfT0VZWD .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-AB2nzvVqhfT0VZWD .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-AB2nzvVqhfT0VZWD .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-AB2nzvVqhfT0VZWD .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-AB2nzvVqhfT0VZWD .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-AB2nzvVqhfT0VZWD .marker{fill:#333333;stroke:#333333;}#mermaid-svg-AB2nzvVqhfT0VZWD .marker.cross{stroke:#333333;}#mermaid-svg-AB2nzvVqhfT0VZWD svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-AB2nzvVqhfT0VZWD g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-AB2nzvVqhfT0VZWD g.classGroup text .title{font-weight:bolder;}#mermaid-svg-AB2nzvVqhfT0VZWD .nodeLabel,#mermaid-svg-AB2nzvVqhfT0VZWD .edgeLabel{color:#131300;}#mermaid-svg-AB2nzvVqhfT0VZWD .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-AB2nzvVqhfT0VZWD .label text{fill:#131300;}#mermaid-svg-AB2nzvVqhfT0VZWD .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-AB2nzvVqhfT0VZWD .classTitle{font-weight:bolder;}#mermaid-svg-AB2nzvVqhfT0VZWD .node rect,#mermaid-svg-AB2nzvVqhfT0VZWD .node circle,#mermaid-svg-AB2nzvVqhfT0VZWD .node ellipse,#mermaid-svg-AB2nzvVqhfT0VZWD .node polygon,#mermaid-svg-AB2nzvVqhfT0VZWD .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-AB2nzvVqhfT0VZWD .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-AB2nzvVqhfT0VZWD g.clickable{cursor:pointer;}#mermaid-svg-AB2nzvVqhfT0VZWD g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-AB2nzvVqhfT0VZWD g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-AB2nzvVqhfT0VZWD .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-AB2nzvVqhfT0VZWD .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-AB2nzvVqhfT0VZWD .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-AB2nzvVqhfT0VZWD .dashed-line{stroke-dasharray:3;}#mermaid-svg-AB2nzvVqhfT0VZWD #compositionStart,#mermaid-svg-AB2nzvVqhfT0VZWD .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-AB2nzvVqhfT0VZWD #compositionEnd,#mermaid-svg-AB2nzvVqhfT0VZWD .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-AB2nzvVqhfT0VZWD #dependencyStart,#mermaid-svg-AB2nzvVqhfT0VZWD .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-AB2nzvVqhfT0VZWD #dependencyStart,#mermaid-svg-AB2nzvVqhfT0VZWD .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-AB2nzvVqhfT0VZWD #extensionStart,#mermaid-svg-AB2nzvVqhfT0VZWD .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-AB2nzvVqhfT0VZWD #extensionEnd,#mermaid-svg-AB2nzvVqhfT0VZWD .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-AB2nzvVqhfT0VZWD #aggregationStart,#mermaid-svg-AB2nzvVqhfT0VZWD .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-AB2nzvVqhfT0VZWD #aggregationEnd,#mermaid-svg-AB2nzvVqhfT0VZWD .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-AB2nzvVqhfT0VZWD .edgeTerminals{font-size:11px;}#mermaid-svg-AB2nzvVqhfT0VZWD :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} «interface» ProductA SetName(name string) SetWeight(weight int64) Get() ProductA1 -Name string -Weight int64 SetName(name string) SetWeight(weight int64) Get() ProductA2 -String Name -Int64 Weight SetName(name string) SetWeight(weight int64) Get() «interface» ProductB SetName(name string) SetHeight(height int64) Get() ProductB1 -String Name -Int64 Height SetName(name string) SetHeight(height int64) Get() ProductB2 -String Name -Int64 Height SetName(name string) SetHeight(height int64) Get() Client «interface» AbstractFactory SetProductA(name string, weight int64) : AbstractProductA SetProductB(name string, height int64) : AbstractProductB ConcreteFactory1 SetProductA(name string, weight int64) : AbstractProductA SetProductB(name string, height int64) : AbstractProductB ConcreteFactory2 SetProductA(name string, weight int64) : AbstractProductA SetProductB(name string, height int64) : AbstractProductB