boxmoe_header_banner_img

Hello! 欢迎来到悠悠畅享网!

文章导读

Golang中的工厂模式有哪些类型 详解Golang工厂模式分类及代码示例


avatar
站长 2025年8月14日 1

golang中工厂模式分为简单工厂、工厂方法和抽象工厂三种类型。1. 简单工厂通过一个工厂函数集中创建不同对象,适用于类型不多的小项目,但扩展性差;2. 工厂方法定义接口由子类决定实例化哪个类,适合产品种类可能扩展的场景,符合开闭原则;3. 抽象工厂用于创建一组相关或依赖对象家族,适合多维度组合的产品结构,功能强大但实现复杂。

Golang中的工厂模式有哪些类型 详解Golang工厂模式分类及代码示例

在Golang中,工厂模式是一种常用的创建型设计模式,用于解耦对象的创建和使用。根据实现方式的不同,可以将工厂模式分为三类:简单工厂模式、工厂方法模式和抽象工厂模式。它们各自适用于不同的场景,下面分别介绍其特点及代码示例。

Golang中的工厂模式有哪些类型 详解Golang工厂模式分类及代码示例


简单工厂模式(Simple Factory)

适用场景:当你只需要一个集中的地方来创建不同类型的对象,并且类型数量不多时,适合用简单工厂。

Golang中的工厂模式有哪些类型 详解Golang工厂模式分类及代码示例

特点:通过一个“工厂函数”接收参数,返回具体的结构体实例。这种方式不严格符合开闭原则,因为新增产品类型需要修改工厂函数。

立即学习go语言免费学习笔记(深入)”;

示例代码:

Golang中的工厂模式有哪些类型 详解Golang工厂模式分类及代码示例

type Animal interface {     Speak() }  type Dog struct{} func (d *Dog) Speak() {     fmt.Println("Woof!") }  type Cat struct{} func (c *Cat) Speak() {     fmt.Println("Meow!")  type AnimalFactory struct{}  func (f *AnimalFactory) CreateAnimal(animalType string) Animal {     switch animalType {     case "dog":         return &Dog{}     case "cat":         return &Cat{}     default:         return nil     } }

说明

  • CreateAnimal

    是核心工厂方法,根据传入的字符串决定返回哪种对象。

  • 如果后续增加新动物类型,需要修改这个函数,不够灵活。

工厂方法模式(Factory Method)

适用场景:当你的系统希望延迟到子类去决定具体创建哪一个对象时,使用工厂方法模式。

特点:定义一个用于创建对象的接口,但让子类决定实例化哪一个类。工厂方法把实例化的责任推迟到子类。

示例代码:

type Animal interface {     Speak() }  type Dog struct{} func (d *Dog) Speak() { fmt.Println("Woof!") }  type Cat struct{} func (c *Cat) Speak() { fmt.Println("Meow!") }  // 工厂接口 type AnimalFactory interface {     CreateAnimal() Animal }  type DogFactory struct{} func (df *DogFactory) CreateAnimal() Animal {     return &Dog{} }  type CatFactory struct{} func (cf *CatFactory) CreateAnimal() Animal {     return &Cat{} }

说明

  • 每个具体工厂负责创建特定的对象。
  • 更加符合开闭原则,新增产品只需添加新工厂,不需要修改已有逻辑。
  • 适用于产品种类可能扩展的场景。

抽象工厂模式(Abstract Factory)

适用场景:当你需要创建一组相关或依赖对象家族,而不想指定具体类的时候,使用抽象工厂。

特点:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

示例代码:

type Animal interface {     Speak() }  type ColorfulAnimal interface {     ShowColor() }  type Dog struct{} func (d *Dog) Speak() { fmt.Println("Woof!") }  type RedDog struct{ Dog } func (rd *RedDog) ShowColor() { fmt.Println("I'm red") }  type BlueDog struct{ Dog } func (bd *BlueDog) ShowColor() { fmt.Println("I'm blue") }  type AnimalFactory interface {     CreateAnimal() Animal     CreateColorfulAnimal() ColorfulAnimal }  type RedAnimalFactory struct{} func (rf *RedAnimalFactory) CreateAnimal() Animal {     return &Dog{} } func (rf *RedAnimalFactory) CreateColorfulAnimal() ColorfulAnimal {     return &RedDog{} }  type BlueAnimalFactory struct{} func (bf *BlueAnimalFactory) CreateAnimal() Animal {     return &Dog{} } func (bf *BlueAnimalFactory) CreateColorfulAnimal() ColorfulAnimal {     return &BlueDog{} }

说明

  • 抽象工厂不仅创建一种对象,而是创建一整套相关的对象。
  • 更适合多个维度变化的产品族,比如颜色 + 动物种类。
  • 实现复杂度较高,适用于需求明确、结构复杂的项目。

总结一下

  • 简单工厂:适合小项目,快速实现,缺点是不易扩展。
  • 工厂方法:适合产品种类可能增加的项目,更灵活。
  • 抽象工厂:适合多维度组合的产品结构,功能强大但实现复杂。

基本上就这些了。选择哪一种,主要看你要处理的对象结构有多复杂,以及是否需要应对未来的变化。



评论(已关闭)

评论已关闭