登录
首页 >  Golang >  Go教程

Golang抽象工厂模式实例教程

时间:2025-12-13 20:27:53 248浏览 收藏

推广推荐
免费电影APP ➜
支持 PC / 移动端,安全直达

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《Golang抽象工厂模式实例化详解》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

抽象工厂模式在Go中通过接口定义产品和工厂,实现创建一系列相关对象而不指定具体类。例如,MySQL和Redis系列的数据存储分别实现UserRepo和OrderRepo接口,再通过RepoFactory接口统一创建对应系列的对象。利用工厂函数根据环境配置返回不同工厂实例,使调用方无需关心具体实现。切换数据源时只需更改工厂类型,业务逻辑不变,新增系列也无需修改原有代码,符合开闭原则。Go虽无继承,但通过接口多态和组合,能简洁高效地模拟抽象工厂,适用于多产品族场景如跨平台组件或主题系统。

Golang抽象工厂模式多系列对象实例化

在Go语言中实现抽象工厂模式,主要用于创建一系列相关或依赖对象的场景,而无需指定其具体类。这种模式特别适合需要支持多种产品族(如不同主题的UI组件、跨平台的文件处理工具等)的应用。通过接口定义工厂行为和产品行为,Go 能以简洁的方式模拟“抽象工厂”。

抽象工厂的核心思想

抽象工厂不是创建单一类型对象,而是创建一组相关对象。比如,假设有两个系列的数据存储实现:MySQL 和 Redis 系列,每个系列包含用户存储和订单存储。我们希望通过统一的工厂接口来实例化对应系列的对象,避免调用方感知具体类型。

关键点: 使用接口代替抽象类,通过工厂接口返回产品接口,实现解耦。

定义产品接口与实现

先定义产品接口,比如用户存储和订单存储:

user_repo.go

type UserRepo interface {
    Save(user interface{}) error
    FindByID(id string) (interface{}, error)
}

order_repo.go

type OrderRepo interface {
    Create(order interface{}) error
    ListByUser(userID string) ([]interface{}, error)
}

接着实现 MySQL 和 Redis 两个系列:

mysql_user_repo.go

type MysqlUserRepo struct{}

func (r *MysqlUserRepo) Save(user interface{}) error {
    // 模拟保存到 MySQL
    return nil
}

func (r *MysqlUserRepo) FindByID(id string) (interface{}, error) {
    return map[string]interface{}{"id": id, "name": "Tom"}, nil
}

redis_user_repo.go

type RedisUserRepo struct{}

func (r *RedisUserRepo) Save(user interface{}) error {
    // 模拟保存到 Redis
    return nil
}

func (r *RedisUserRepo) FindByID(id string) (interface{}, error) {
    return map[string]interface{}{"id": id, "name": "Jerry"}, nil
}

同理实现 MysqlOrderRepoRedisOrderRepo

定义抽象工厂接口

工厂接口声明创建整套产品的方法:

type RepoFactory interface {
    CreateUserRepo() UserRepo
    CreateOrderRepo() OrderRepo
}

然后为每个系列实现工厂:

mysql_factory.go

type MysqlRepoFactory struct{}

func (f *MysqlRepoFactory) CreateUserRepo() UserRepo {
    return &MysqlUserRepo{}
}

func (f *MysqlRepoFactory) CreateOrderRepo() OrderRepo {
    return &MysqlOrderRepo{}
}

redis_factory.go

type RedisRepoFactory struct{}

func (f *RedisRepoFactory) CreateUserRepo() UserRepo {
    return &RedisUserRepo{}
}

func (f *RedisRepoFactory) CreateOrderRepo() OrderRepo {
    return &RedisOrderRepo{}
}

使用方式:运行时选择系列

调用方通过配置决定使用哪个工厂,从而获得一整套协调工作的对象:

func getFactory(env string) RepoFactory {
    switch env {
    case "production":
        return &MysqlRepoFactory{}
    case "cache_only":
        return &RedisRepoFactory{}
    default:
        return &MysqlRepoFactory{}
    }
}

// 示例使用
func main() {
    factory := getFactory("production")
    
    userRepo := factory.CreateUserRepo()
    orderRepo := factory.CreateOrderRepo()

    user, _ := userRepo.FindByID("123")
    _ = orderRepo.ListByUser("123")

    fmt.Printf("User: %+v\n", user)
}

如果切换环境为 "cache_only",所有组件自动变为 Redis 实现,无需修改业务逻辑。

基本上就这些。Go 没有继承,但通过接口组合和多态,完全可以实现抽象工厂的效果。关键是把“系列”理解为一组遵循相同接口的不同实现,并由统一工厂产出。这种方式提升了可扩展性,新增一个数据源系列只需添加新实现和新工厂,不改动原有代码。

终于介绍完啦!小伙伴们,这篇关于《Golang抽象工厂模式实例教程》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>