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

目录

抽象工厂模式(Abstract Factory Pattern)

抽象工厂模式的核心角色

优缺点

代码实现


抽象工厂模式(Abstract Factory Pattern)

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

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

抽象工厂模式的核心角色

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

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

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

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

优缺点

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

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

代码实现

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()
}

相关推荐

  1. GO设计模式——3抽象工厂模式创建

    2023-12-11 18:04:02       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-11 18:04:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-11 18:04:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-11 18:04:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-11 18:04:02       20 阅读

热门阅读

  1. Vue学习笔记-Vue3中setup函数注意点

    2023-12-11 18:04:02       45 阅读
  2. 风扭转对风力发电机的影响

    2023-12-11 18:04:02       37 阅读
  3. Linux下文本三剑客:grep、awk、sed之对比

    2023-12-11 18:04:02       40 阅读
  4. 力扣labuladong一刷day35天

    2023-12-11 18:04:02       28 阅读
  5. ABAP 选择屏幕创建按钮,并执行

    2023-12-11 18:04:02       33 阅读
  6. python取前两位数字

    2023-12-11 18:04:02       35 阅读
  7. Qt Creator制作动画 编程

    2023-12-11 18:04:02       35 阅读
  8. from torch_geometric.utils import k_hop_subgraph和subgraph

    2023-12-11 18:04:02       36 阅读
  9. 【CF245H】Queries for Number of Palindromes(字符串区间dp)

    2023-12-11 18:04:02       35 阅读