GO设计模式——3、抽象工厂模式(创建型)

目录

[抽象工厂模式(Abstract Factory Pattern)](#抽象工厂模式(Abstract Factory Pattern))

抽象工厂模式的核心角色

优缺点

代码实现


抽象工厂模式(Abstract Factory Pattern)

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。抽象工厂模式提供了一种创建一系列相关或相互依赖对象的接口,而无需指定具体实现类。通过使用抽象工厂模式,可以将客户端与具体产品的创建过程解耦,使得客户端可以通过工厂接口来创建一族产品。

抽象工厂模式基于工厂方法模式。两者的区别在于:工厂方法模式是创建出一种产品,而抽象工厂模式是创建出一类产品。这二种都属于工厂模式,在设计上是相似的。

抽象工厂模式的核心角色

1、抽象工厂(Abstract Factory):声明了一组用于创建产品对象的方法,每个方法对应一种产品类型。抽象工厂可以是接口或抽象类。

2、具体工厂(Concrete Factory):实现了抽象工厂接口,负责创建具体产品对象的实例。

3、抽象产品(Abstract Product):定义了一组产品对象的共同接口或抽象类,描述了产品对象的公共方法。

4、具体产品(Concrete Product):实现了抽象产品接口,定义了具体产品的特定行为和属性。

优缺点

(1)优点**:**当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。

(2)缺点**:**产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码

代码实现

Go 复制代码
package main

import "fmt"

// (抽象工厂) 能够生产tv、手机、Ipad
type AbstractFactory interface {
    CreateTelevision() Television
    CreateIpad() Ipad
    CreateCellphone() Cellphone
}

// (抽象产品) -- Television
type Television interface {
    Watch()
}

// (抽象产品) -- Ipad
type Ipad interface {
    Play()
}

// (抽象产品) -- Cellphone
type Cellphone interface {
    Callphone()
}

// (具体工厂) 华为工厂
type HuaweiFactory struct {
}

func (hf *HuaweiFactory) CreateTelevision() Television {
    return &HuaweiTelevision{}
}
func (hf *HuaweiFactory) CreateIpad() Ipad {
    return &HuaweiIpad{}
}
func (hf *HuaweiFactory) CreateCellphone() Cellphone {
    return &HuaweiCellphone{}
}

// (具体产品)华为Television,实现了Television接口
type HuaweiTelevision struct {
}

func (hc *HuaweiTelevision) Watch() {
    fmt.Println("Watch Huawei Television")
}

// (具体产品)华为Ipad,实现了Ipad接口
type HuaweiIpad struct {
}

func (hc *HuaweiIpad) Play() {
    fmt.Println("Play Huawei Ipad")
}

// (具体产品)华为Cellphone,实现了Cellphone接口
type HuaweiCellphone struct {
}

func (hc *HuaweiCellphone) Callphone() {
    fmt.Println("Call Huawei Cellphone")
}

// (具体工厂) 小米工厂
type MiFactory struct {
}

func (mf *MiFactory) CreateTelevision() Television {
    return &MiTelevision{}
}
func (mf *MiFactory) CreateIpad() Ipad {
    return nil
}
func (mf *MiFactory) CreateCellphone() Cellphone {
    return &MiCellphone{}
}

// (具体产品)小米Television,实现了Television接口
type MiTelevision struct {
}

func (mt *MiTelevision) Watch() {
    fmt.Println("Watch Mi Television")
}

// (具体产品)小米Cellphone,实现了Cellphone接口
type MiCellphone struct {
}

func (mc *MiCellphone) Callphone() {
    fmt.Println("Call Mi Cellphone")
}

// 超级工厂类 获取超级工厂实例
type HyperFactory struct {
}

func (hf *HyperFactory) CreateFactory(factoryName string) AbstractFactory {
    switch factoryName {
    case "Huawei":
       return &HuaweiFactory{}
    case "Mi":
       return &MiFactory{}
    default:
       return nil
    }
}

func main() {
    hfactoey := HyperFactory{}
    hwfactory := hfactoey.CreateFactory("Huawei")
    hwfactory.CreateTelevision().Watch()
    hwfactory.CreateIpad().Play()
    hwfactory.CreateCellphone().Callphone()

    mifactory := hfactoey.CreateFactory("Mi")
    mifactory.CreateTelevision().Watch()
    if mifactory.CreateIpad() == nil {
       fmt.Println("不支持")
    }
    
    mifactory.CreateCellphone().Callphone()
}
相关推荐
心若自由何处是束缚5 小时前
适配器模式:从原理到实战的完整指南
设计模式·适配器模式
JWASX6 小时前
【Go】map 源码解析 - group
后端·golang·map·源码分析
是2的10次方啊8 小时前
设计模式与生活⑮:看到哪儿了,得有个地方替你记着
设计模式
小满zs9 小时前
Go语言第十八章(架构设计)
后端·go
2601_967979121 天前
设计模式 - 访问者模式
设计模式·访问者模式·应用场景·对象结构·操作分离
联犀1 天前
纯 Go 的 CAD 开源库 go-cad:直接解析 DWG/DXF,现在也能导出 PDF
开发语言·golang·pdf
FungLeo1 天前
成为全栈·Go 后端篇·三轮代码审阅之后:把可读性和证据做成工程规则
golang·go·ast·静态检查·成为全栈·代码审阅
是2的10次方啊1 天前
设计模式与生活⑭:一栋楼,共用一份热
设计模式
我不会起名字3221 天前
Redis 从 0 到 1(二十八):Redis 抖一下,重试把它打挂了——超时/重试/熔断的 Go 封装
redis·go·超时·熔断·重试
联犀1 天前
联犀 Go SIMD 实战:视频转码从 16.5ms 到 5.46ms
开发语言·golang·音视频