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

在Go语言中实现抽象工厂模式,主要用于创建一系列相关或依赖对象的场景,而无需指定其具体类。这种模式特别适合需要支持多种产品族(如不同主题的UI组件、跨平台的文件处理工具等)的应用。通过接口定义工厂行为和产品行为,Go 能以简洁的方式模拟“抽象工厂”。
抽象工厂不是创建单一类型对象,而是创建一组相关对象。比如,假设有两个系列的数据存储实现:MySQL 和 Redis 系列,每个系列包含用户存储和订单存储。我们希望通过统一的工厂接口来实例化对应系列的对象,避免调用方感知具体类型。
关键点: 使用接口代替抽象类,通过工厂接口返回产品接口,实现解耦。先定义产品接口,比如用户存储和订单存储:
user_repo.go
立即学习“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
}
同理实现 MysqlOrderRepo 和 RedisOrderRepo。
工厂接口声明创建整套产品的方法:
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抽象工厂模式多系列对象实例化的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号