Golang如何使用工厂方法模式创建对象_Golang Factory Method模式实践

工厂方法模式通过接口和工厂函数封装对象创建过程,客户端无需关心具体类型,只需调用工厂获取实例。1. 定义Product接口,包含GetName和Use方法;2. 实现ConcreteProductA和ConcreteProductB结构体并实现接口;3. 创建ProductFactory类型及CreateProductA、CreateProductB工厂函数,或使用GetProduct根据参数返回对应实例;4. 主函数中通过工厂函数或类型选择创建对象,实现解耦与可扩展性。新增产品时只需添加新类型和工厂逻辑,符合开闭原则。Go通过接口与函数组合替代继承,简洁实用。

在Go语言中,虽然没有类和继承的概念,但通过结构体、接口和函数的组合,可以很好地实现设计模式。工厂方法模式(Factory Method Pattern)是一种创建型设计模式,它定义一个用于创建对象的接口,但让子类决定实例化哪一个类。Go中不支持继承,因此我们通过接口和函数来模拟这一行为。

什么是工厂方法模式

工厂方法模式的核心是将对象的创建过程封装起来,客户端不需要知道具体创建的是哪个类型,只需要通过工厂提供的方法获取实例。这种方式降低了代码耦合,提升了可扩展性。

典型使用场景包括:

  • 需要根据不同配置创建不同类型的对象
  • 对象初始化逻辑复杂,希望统一管理创建过程
  • 未来可能扩展新的类型,避免修改已有代码

定义产品接口和具体产品

首先定义一个接口作为“产品”的抽象,然后实现多个具体的产品结构体。

// Product 接口定义对象的行为
type Product interface {
    GetName() string
    Use() string
}

// ConcreteProductA 具体产品A
type ConcreteProductA struct{}

func (p *ConcreteProductA) GetName() string {
    return "ProductA"
}

func (p *ConcreteProductA) Use() string {
    return "Using Product A"
}

// ConcreteProductB 具体产品B
type ConcreteProductB struct{}

func (p *ConcreteProductB) GetName() string {
    return "ProductB"
}

func (p *ConcreteProductB) Use() string {
    return "Using Product B"
}

定义工厂函数

Go中没有抽象方法或虚函数,但我们可以通过返回接口类型的函数来实现工厂方法。

// ProductFactory 是工厂函数类型
type ProductFactory func() Product

// CreateProductA 工厂函数:创建 ProductA
func CreateProductA() Product {
    return &ConcreteProductA{}
}

// CreateProductB 工厂函数:创建 ProductB
func CreateProductB() Product {
    return &ConcreteProductB{}
}

也可以写成一个通用工厂函数,根据参数决定返回哪种产品:

// GetProduct 根据类型名创建产品
func GetProduct(productType string) (Product, error) {
    switch productType {
    case "A":
        return &ConcreteProductA{}, nil
    case "B":
        return &ConcreteProductB{}, nil
    default:
        return nil, fmt.Errorf("unknown product type: %s", productType)
    }
}

使用工厂创建对象

客户端代码不再直接初始化结构体,而是通过工厂获取实例。

func main() {
    // 方式一:使用具体工厂函数
    factory := CreateProductA
    product := factory()
    fmt.Println(product.Use())

    // 方式二:使用类型选择工厂
    product, err := GetProduct("B")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(product.Use())
}

输出结果:

Product A
Product B

这样做的好处是,当新增产品类型时,只需添加新的结构体和对应的创建逻辑,主流程无需修改,符合开闭原则。

基本上就这些。Go的工厂方法不依赖继承,而是靠接口和函数组合实现,简洁且实用。