《golang设计模式》第一部分·创建型模式-05-抽象工厂模式(Abstract Factory)

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 设计

有冰箱和电视两种商品,但是我们创建的时候需要从另一个维度来分类——它的品牌。因此我们以西蜀牌和东吴牌分别创建两个具体工厂,每个具体工厂负责创建自己品牌的冰箱和电视。

  • 定义抽象产品冰箱
    • 定义具体产品西蜀牌冰箱
      • 它有品牌产品类型温度三个成员
      • 它的SetBrand()SetType()SetTemperature()三个方法分别负责设置以上三个成员
      • 它的Get()方法获取它本身的信息
    • 定义具体产品东吴牌冰箱
      • 它有品牌产品类型温度三个成员
      • 它的SetBrand()SetType()SetTemperature()三个方法分别负责设置以上三个成员
      • 它的Get()方法获取它本身的信息
  • 定义抽象产品电视
    • 定义具体产品西蜀牌电视
      • 它有品牌产品类型分辨率三个成员
      • 它的SetBrand()SetType()SetPPI()三个方法分别负责设置以上三个成员
      • 它的Get()方法获取它本身的信息
    • 定义具体产品东吴牌电视
      • 它有品牌产品类型分辨率三个成员
      • 它的SetBrand()SetType()SetPPI()三个方法分别负责设置以上三个成员
      • 它的Get()方法获取它本身的信息
  • 定义抽象工厂
    • 定义具体工厂西蜀工厂
      • 它的方法SetFridge设置具体产品冰箱
      • 它的方法SetTV设置具体产品电视
    • 定义具体工厂东吴工厂
      • 它的方法SetFridge设置具体产品冰箱
      • 它的方法SetTV设置具体产品电视
  • 定义一个函数CreateFactory(),用来创建具体工厂
  • 调用
    • CreateFactory()函数实例化 西蜀工厂
    • factory1实例化西蜀牌冰箱西蜀牌电视,并查看结果
    • CreateFactory()函数实例化 东吴工厂
    • factory2实例化东吴牌冰箱东吴牌电视,并查看结果

2.2 代码

package main

import "fmt"

// 定义抽象产品——冰箱
type Fridge interface {
	SetBrand(brand string)
	SetType(productType string)
	SetTemperature(temperature int64)
	Get()
}

// 定义实际产品蜀国牌冰箱
type FridgeShu struct {
	Brand       string
	ProductType string
	Temperature int64
}

func (c *FridgeShu) SetBrand(brand string) {
	c.Brand = brand
}

func (c *FridgeShu) SetType(productType string) {
	c.ProductType = productType
}

func (c *FridgeShu) SetTemperature(temperature int64) {
	c.Temperature = temperature
}

func (c *FridgeShu) Get() {
	fmt.Printf("%+v\n", c)
}

// 定义实际产品东吴牌冰箱
type FridgeWu struct {
	Brand       string
	ProductType string
	Temperature int64
}

func (c *FridgeWu) SetBrand(brand string) {
	c.Brand = brand
}

func (c *FridgeWu) SetType(productType string) {
	c.ProductType = productType
}

func (c *FridgeWu) SetTemperature(temperature int64) {
	c.Temperature = temperature
}

func (c *FridgeWu) Get() {
	fmt.Printf("%+v\n", c)
}

// 定义第二个抽象产品电视机
type TV interface {
	SetBrand(brand string)
	SetType(productType string)
	SetPPI(ppi string)
	Get()
}

// 定义西蜀牌电视机
type TVShu struct {
	Brand       string
	ProductType string
	PPI         string
}

func (c *TVShu) SetBrand(brand string) {
	c.Brand = brand
}

func (c *TVShu) SetType(productType string) {
	c.ProductType = productType
}

func (c *TVShu) SetPPI(ppi string) {
	c.PPI = ppi
}

func (c *TVShu) Get() {
	fmt.Printf("%+v\n", c)
}

// 定义东吴牌电视机
type TVWu struct {
	Brand       string
	ProductType string
	PPI         string
}

func (c *TVWu) SetBrand(brand string) {
	c.Brand = brand
}

func (c *TVWu) SetType(productType string) {
	c.ProductType = productType
}

func (c *TVWu) SetPPI(ppi string) {
	c.PPI = ppi
}

func (c *TVWu) Get() {
	fmt.Printf("%+v\n", c)
}

// Factory部分

// 定义抽象工厂
type Factory interface {
	SetFridge(temperature int64) Fridge
	SetTV() TV
}

// 定义具体工厂——西蜀工厂
type FactoryShu struct {
}

func (f *FactoryShu) SetFridge(temperature int64) Fridge {
	a := &FridgeShu{}
	a.SetBrand("蜀")
	a.SetTemperature(temperature)
	a.SetType("冰箱")
	return a
}

func (f *FactoryShu) SetTV() TV {
	p := &TVShu{}
	p.SetBrand("蜀")
	p.SetType("电视")
	p.SetPPI("8k")
	return p
}

// 定义具体工厂——东吴工厂
type FactoryWu struct {
}

func (f *FactoryWu) SetFridge(temperature int64) Fridge {
	a := &FridgeWu{}
	a.SetBrand("蜀")
	a.SetTemperature(temperature)
	a.SetType("冰箱")
	return a
}

func (f *FactoryWu) SetTV() TV {
	p := &TVWu{}
	p.SetBrand("蜀")
	p.SetType("电视")
	p.SetPPI("4k")
	return p
}

// 写一个创建工厂的函数
func CreateFactory(pType int64) Factory {
	switch pType {
	case 1:
		return &FactoryShu{}
	case 2:
		return &FactoryWu{}
	}
	return nil
}

func main() {
	shuFactory := CreateFactory(1)
	shuFactory.SetFridge(4).Get()
	shuFactory.SetTV().Get()

	wuFactory := CreateFactory(2)
	wuFactory.SetFridge(5).Get()
	wuFactory.SetTV().Get()
}

  • 输出
&{Brand:蜀 ProductType:冰箱 Temperature:4}
&{Brand:蜀 ProductType:电视 PPI:8k}       
&{Brand:蜀 ProductType:冰箱 Temperature:5}
&{Brand:蜀 ProductType:电视 PPI:4k}   

2.3 类图

«interface»
Fridge
+SetBrand(brand string)
+SetType(productType string)
+SetTemperature(temperature int64)
+Get()
FridgeShu
+Brand:String
+ProductType:String
+Temperature:Int64
+SetBrand(brand string)
+SetType(productType string)
+SetTemperature(temperature int64)
+Get()
FridgeWu
+Brand:String
+ProductType:String
+Temperature:Int64
+SetBrand(brand string)
+SetType(productType string)
+SetTemperature(temperature int64)
+Get()
« nterface»
TV
+SetBrand(brand string)
+SetType(productType string)
+SetPPI(ppi string)
+Get()
TVShu
+Brand:String
+ProductType:String
+PPI:String
+SetBrand(brand string)
+SetType(productType string)
+SetPPI(ppi string)
+Get()
TVWu
+Brand:String
+ProductType:String
+PPI:String
+SetBrand(brand string)
+SetType(productType string)
+SetPPI(ppi string)
+Get()
«interface»
Factory
+SetFridge(temperature int64) : Fridge
+SetTV() : TV
FactoryShu
+SetFridge(temperature int64) : Fridge
+SetTV() : TV
FactoryWu
+SetFridge(temperature int64) : Fridge
+SetTV() : TV
Client

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄德公笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值