本文讨论了 Abstract Factory Pattern 抽象工厂模式的 Go 实现
Abstract Factory Pattern 抽象工厂
- 有利于产品族的扩展,不利于当个产品族内的产品扩展
接口实现
- 抽象工厂接口
1 | package afbase |
- color 接口
1 | package afbase |
- shape 接口
1 | package afbase |
产品族的实现
产品族01
- 产品族01的具体工厂实现
1 | package product01 |
- 产品族01的产品:rectangle 和 blue
1 | type rectangle struct { |
1 | type blue struct { |
产品族02
- 产品族02的具体工厂实现
1 | package product02 |
- 产品族02的产品:circle 和 green
1 | type circle struct { |
1 | type green struct { |
产品族03
- 产品族03的具体工厂实现
1 | package product03 |
- 产品族03的产品:square 和 color
1 | type square struct { |
1 | type red struct { |
使用
1 | func main() { |