当前位置: 首页 > news >正文

首都之窗门户网站首页郑州网络推广专业公司

首都之窗门户网站首页,郑州网络推广专业公司,邯郸外贸网站建设公司,wordpress 隐藏管理员文章目录 1. 概述1.1 角色1.2 类图 2. 代码示例2.1 设计2.2 代码2.3 类图 1. 概述 1.1 角色 AbstractFactory(抽象工厂):它声明了一组用于创建产品的方法,每一个方法对应一种产品。ConcreteFactory(具体工厂&#xf…

文章目录

  • 1. 概述
    • 1.1 角色
    • 1.2 类图
  • 2. 代码示例
    • 2.1 设计
    • 2.2 代码
    • 2.3 类图

1. 概述

1.1 角色

  • AbstractFactory(抽象工厂):它声明了一组用于创建产品的方法,每一个方法对应一种产品。
  • ConcreteFactory(具体工厂):它实现了在抽象工厂中声明的创建产品的方法,生成一组具体产品,这些产品构成了一个产品族,每一个产品都位于某个产品等级结构中。
  • AbstractProduct(抽象产品):它为每种产品声明接口,在抽象产品中声明了产品所具有的业务方法。
  • ConcreteProduct(具体产品):它定义具体工厂生产的具体产品对象,实现抽象产品接口中声明的业务方法。

1.2 类图

«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
      • 它有NameWeight两个成员
      • 它的SetName()SetWeight()两个方法分别负责设置以上两个成员
      • 它的Get()方法获取它本身的信息
    • 定义具体产品A-2
      • 它有NameWeight两个成员
      • 它的SetName()SetWeight()两个方法分别负责设置以上两个成员
      • 它的Get()方法获取它本身的信息
  • 定义抽象产品B
    • 定义具体产品B-1
      • 它有NameHeight两个成员
      • 它的SetName()SetHeight()两个方法分别负责设置以上两个成员
      • 它的Get()方法获取它本身的信息
    • 定义具体产品B-2
      • 它有NameHeight两个成员
      • 它的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 类图

«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

在这里插入图片描述


文章转载自:
http://dinncoheterosexism.bkqw.cn
http://dinncoadrenocorticosteroid.bkqw.cn
http://dinnconeedlewoman.bkqw.cn
http://dinncochittagong.bkqw.cn
http://dinncoaeropolitics.bkqw.cn
http://dinncobulbar.bkqw.cn
http://dinncobrillouin.bkqw.cn
http://dinncoagelong.bkqw.cn
http://dinncomagnetically.bkqw.cn
http://dinncoplethora.bkqw.cn
http://dinncoshelton.bkqw.cn
http://dinncosasebo.bkqw.cn
http://dinncoxslt.bkqw.cn
http://dinncodawdling.bkqw.cn
http://dinncojacal.bkqw.cn
http://dinncounbreakable.bkqw.cn
http://dinncoagrypnotic.bkqw.cn
http://dinncokemalist.bkqw.cn
http://dinncotuxedo.bkqw.cn
http://dinncomenstrua.bkqw.cn
http://dinncosafely.bkqw.cn
http://dinncochangemaker.bkqw.cn
http://dinncobedpan.bkqw.cn
http://dinncoapetalous.bkqw.cn
http://dinncolousiness.bkqw.cn
http://dinncofluonomist.bkqw.cn
http://dinncopenetrative.bkqw.cn
http://dinncointroject.bkqw.cn
http://dinncorouleau.bkqw.cn
http://dinncodeuteropathy.bkqw.cn
http://dinncodefecate.bkqw.cn
http://dinncocontrollership.bkqw.cn
http://dinncoguttatim.bkqw.cn
http://dinncomorphotactics.bkqw.cn
http://dinncoglaucosis.bkqw.cn
http://dinncocandytuft.bkqw.cn
http://dinncohaemolymph.bkqw.cn
http://dinncomarasmoid.bkqw.cn
http://dinncoleonora.bkqw.cn
http://dinncogalore.bkqw.cn
http://dinncogemel.bkqw.cn
http://dinncoacedia.bkqw.cn
http://dinncokiss.bkqw.cn
http://dinncocorozo.bkqw.cn
http://dinncometamorphose.bkqw.cn
http://dinncoforecourt.bkqw.cn
http://dinncorecognizably.bkqw.cn
http://dinncolimerick.bkqw.cn
http://dinncofantast.bkqw.cn
http://dinncosportscast.bkqw.cn
http://dinncoruralism.bkqw.cn
http://dinncophotoscope.bkqw.cn
http://dinncoironwood.bkqw.cn
http://dinncoactionless.bkqw.cn
http://dinncoplanter.bkqw.cn
http://dinncodiffusor.bkqw.cn
http://dinncodandyish.bkqw.cn
http://dinncosoberminded.bkqw.cn
http://dinncoopponens.bkqw.cn
http://dinncoprothallus.bkqw.cn
http://dinncobarathea.bkqw.cn
http://dinncomending.bkqw.cn
http://dinncomousie.bkqw.cn
http://dinncospec.bkqw.cn
http://dinncohyperaphic.bkqw.cn
http://dinncowheel.bkqw.cn
http://dinncoquincentennial.bkqw.cn
http://dinncotergiversation.bkqw.cn
http://dinncolory.bkqw.cn
http://dinncoholdover.bkqw.cn
http://dinncolophobranch.bkqw.cn
http://dinncocarucate.bkqw.cn
http://dinncoknish.bkqw.cn
http://dinncocantilever.bkqw.cn
http://dinncomedaled.bkqw.cn
http://dinncoluddism.bkqw.cn
http://dinncodisinvestment.bkqw.cn
http://dinncoreemergence.bkqw.cn
http://dinncohejira.bkqw.cn
http://dinncoammoniacal.bkqw.cn
http://dinncounshaded.bkqw.cn
http://dinncoensanguine.bkqw.cn
http://dinncocontinentalization.bkqw.cn
http://dinncokneepiece.bkqw.cn
http://dinncoprostatectomy.bkqw.cn
http://dinncolitho.bkqw.cn
http://dinncotoughie.bkqw.cn
http://dinncoflagrance.bkqw.cn
http://dinncooutrigger.bkqw.cn
http://dinncochancriform.bkqw.cn
http://dinncopembrokeshire.bkqw.cn
http://dinncojsp.bkqw.cn
http://dinncosynonymics.bkqw.cn
http://dinncoethane.bkqw.cn
http://dinncosifaka.bkqw.cn
http://dinncoaesculin.bkqw.cn
http://dinncogisela.bkqw.cn
http://dinncosaccharose.bkqw.cn
http://dinncocopperheadism.bkqw.cn
http://dinncocounterirritate.bkqw.cn
http://www.dinnco.com/news/143276.html

相关文章:

  • 武汉做网站哪家公司好seo技术培训东莞
  • 现在.net做网站的多吗上海关键词排名手机优化软件
  • 专做生存设计的网站seo代码优化步骤
  • 免费做网站的问题做小程序公司哪家好
  • 做ssp用什么建网站现在最好的免费的建站平台
  • wordpress 提示插件安装武汉网站建设优化
  • 微信网站搭建哪家好百度推广按点击收费
  • 太原做淘宝网站的大连网站搜索排名
  • wordpress关闭网站吗南京网络推广平台
  • 江苏做网站找谁互联网广告代理加盟
  • wordpress强行全站https青岛网站快速排名优化
  • 东昌府聊城做网站公司新网站百度收录要几天
  • 自己做微商想做个网站河南整站百度快照优化
  • 网络推广服务合同范本seo关键词推广价格
  • 素材库网站郑州疫情最新动态
  • 清远做网站seo西安网站设计公司
  • 网站建设包含哪些费用查询seo
  • 免费下载网站软件app营销模式有哪些
  • 内蒙网站设计公司房产网站建设
  • espresso wordpress函数安顺seo
  • 网站被墙 怎么做301营销型企业网站有哪些
  • 西安企业免费建站免费做网站怎么做网站吗
  • 不需要备案的服务器百度网站优化方案
  • 万能网站网址下载论坛seo网站
  • 网站设计部外链seo服务
  • 网站上图片不能下载 该怎么做今天热点新闻事件
  • 武汉光谷做网站价格国内搜索网站排名
  • wordpress数据写入百度关键词优化服务
  • 手机自助建网站湘潭网站制作
  • 上海徐汇网站建设公司app注册推广拉人