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()
}
相关推荐
圣殿骑士-Khtangc17 小时前
Go大厂面试真题精讲之并发安全Map的实现方案
golang
运维开发笔记17 小时前
3.8 Go switch 语句学习笔记
golang
圣殿骑士-Khtangc19 小时前
Go-Channel面试精讲从底层结构到select随机性
golang
北漂燕郊杨哥19 小时前
dsh-desktop:DeepSeek Harness 的本地优先桌面应用
golang·wails·deepseek·deepseekharness
程序员爱钓鱼19 小时前
Go break、continue、goto 详解
后端·面试·go
朋克洛德的码农1 天前
Go并发-sync包四剑客:Mutex、RWMutex、WaitGroup、Once-从入门到原理
开发语言·后端·golang
艺艺生辉1 天前
从if-else到策略模式
后端·设计模式
小林ixn1 天前
单例模式:从弹窗管理到全局状态,一个模式搞定
前端·javascript·设计模式
圣殿骑士-Khtangc1 天前
Go泛型编程实战从类型约束到泛型数据结构
golang
worxfr1 天前
Go 底层心智模型:并发、内存与闭包
go