抽象工厂模式在Go中通过接口定义产品族、工厂接口统一创建逻辑来实现,支持跨平台UI组件的系列化创建与扩展。

抽象工厂模式用于创建一系列相关或相互依赖的对象,而无需指定它们的具体类。在 Go 中,由于没有传统面向对象的继承和抽象类,我们通过接口组合 + 工厂函数(或结构体方法)来实现这一模式,核心是“用接口定义产品族,用工厂接口统一创建逻辑”。
定义产品族接口
先为每个产品角色定义接口,比如一个跨平台 UI 库可能有 Button 和 Checkbox:
Button 和 Checkbox 是两个产品角色,各自在不同平台(Windows/macOS)有具体实现:
// 产品接口
type Button interface {
Render() string
}
type Checkbox interface {
Render() string
}
// 具体产品:Windows 实现
type WinButton struct{}
func (w WinButton) Render() string { return "Rendering Windows button" }
type WinCheckbox struct{}
func (w WinCheckbox) Render() string { return "Rendering Windows checkbox" }
// 具体产品:macOS 实现
type MacButton struct{}
func (m MacButton) Render() string { return "Rendering macOS button" }
type MacCheckbox struct{}
func (m MacCheckbox) Render() string { return "Rendering macOS checkbox" }
定义抽象工厂接口
声明一个工厂接口,包含创建每类产品的方法。它不关心具体实现,只约定能力:
立即学习“go语言免费学习笔记(深入)”;
type GUIFactory interface {
CreateButton() Button
CreateCheckbox() Checkbox
}
然后为每个平台实现该接口:
// Windows 工厂
type WinFactory struct{}
func (w WinFactory) CreateButton() Button { return WinButton{} }
func (w WinFactory) CreateCheckbox() Checkbox { return WinCheckbox{} }
// macOS 工厂
type MacFactory struct{}
func (m MacFactory) CreateButton() Button { return MacButton{} }
func (m MacFactory) CreateCheckbox() Checkbox { return MacCheckbox{} }
客户端使用抽象工厂
客户端只依赖 GUIFactory 接口,运行时传入具体工厂实例即可生成整套一致的产品:
func Application(factory GUIFactory) string {
button := factory.CreateButton()
checkbox := factory.CreateCheckbox()
return button.Render() + "\n" + checkbox.Render()
}
// 使用示例
func main() {
winApp := Application(WinFactory{})
fmt.Println(winApp)
// 输出:
// Rendering Windows button
// Rendering Windows checkbox
macApp := Application(MacFactory{})
fmt.Println(macApp)
// 输出:
// Rendering macOS button
// Rendering macOS checkbox
}
扩展性与实际建议
- 工厂可封装为结构体(如带配置、日志、缓存),不只是空结构;
- 若产品族变多(比如新增 TextBox),只需扩展 GUIFactory 接口和所有具体工厂实现,符合开闭原则;
- 避免过度设计:Go 更倾向小接口、组合、函数式构造;只有当存在明确的“系列约束”(如必须 Windows 按钮配 Windows 复选框)时才用抽象工厂;
- 可配合选项模式(functional options)初始化工厂,提升灵活性。










