缘起

最近复习设计模式
拜读谭勇德的<<设计模式就该这样学>>
本系列笔记拟采用golang练习之

简单工厂

简单工厂模式(Simple Factory Pattern)又叫作静态工厂方法模式(Static Factory Method Pattern),简单来说,简单工厂模式有一个具体的工厂类,可以生成多个不同的产品,属于创建型设计模式。
_

场景

  • 某智能家居场景, 需要通过app统一控制智能照明灯的开关
  • 智能灯可以打开 - Open(), 或关闭 - Close()
  • 智能灯可能来自不同厂商, 控制驱动不一样, 具体信息保存在配置文件中

设计

  • 定义ILight接口, 表示智能灯
  • 定义ILightFactory接口, 表示创建智能灯的简单工厂
  • 定义LightInfo类, 保存不同灯的配置信息
  • 定义LightFactory类, 根据配置信息, 创建具体的智能灯实例

simple_factory_test.go

单元测试

  1. package patterns_test
  2. import "testing"
  3. import (sf "learning/gooop/creational_patterns/simple_factory")
  4. func Test_SimpleFactory(t *testing.T) {
  5. config := make([]*sf.LightInfo, 0)
  6. config = append(config, sf.NewLightInfo(1, "客厅灯", "mijia", "L-100"))
  7. config = append(config, sf.NewLightInfo(2, "餐厅灯", "redmi", "L-45"))
  8. factory := sf.DefaultLightFactory
  9. for _,info := range config {
  10. e, light := factory.Create(info)
  11. if e != nil {
  12. t.Error(e.Error())
  13. } else {
  14. _ = light.Open()
  15. _ = light.Close()
  16. }
  17. }
  18. }

测试输出

  1. $ go test -v simple_factory_test.go
  2. === RUN Test_SimpleFactory
  3. tMijiaLight.Open, &{1 客厅灯 mijia L-100}
  4. tMijiaLight.Close, &{1 客厅灯 mijia L-100}
  5. tRedmiLight.Open, &{2 餐厅灯 redmi L-45}
  6. tRedmiLight.Close, &{2 餐厅灯 redmi L-45}
  7. --- PASS: Test_SimpleFactory (0.00s)
  8. PASS
  9. ok command-line-arguments 0.004s

ILight.go

定义智能灯的统一接口

  1. package simple_factory
  2. type ILight interface {
  3. ID() int
  4. Name() string
  5. Open() error
  6. Close() error
  7. }

ILightFactory.go

定义智能灯的创建工厂

  1. package simple_factory
  2. type ILightFactory interface {
  3. Create(info *LightInfo) (error, ILight)
  4. }

LightInfo.go

封装智能灯的配置信息

  1. package simple_factory
  2. type LightInfo struct {
  3. iID int
  4. sName string
  5. sVendor string
  6. sModel string
  7. }
  8. func NewLightInfo(id int, name string, vendor string, model string) *LightInfo {
  9. return &LightInfo{
  10. id, name, vendor, model,
  11. }
  12. }
  13. func (me *LightInfo) ID() int {
  14. return me.iID
  15. }
  16. func (me *LightInfo) Name() string {
  17. return me.sName
  18. }

LightFactory.go

tLightFactory是实现ILightFactory的简单工厂, 通过输入参数创建不同的ILight实例

  1. package simple_factory
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. )
  7. var DefaultLightFactory = newLightFactory()
  8. type tLightFactory struct {
  9. }
  10. func newLightFactory() ILightFactory {
  11. return &tLightFactory{}
  12. }
  13. func (me *tLightFactory) Create(info *LightInfo) (error, ILight) {
  14. switch strings.ToLower(info.sVendor) {
  15. case "mijia":
  16. return nil, newMijiaLight(info)
  17. case "redmi":
  18. return nil, newRedmiLight(info)
  19. default:
  20. return errors.New(fmt.Sprintf("unsupported vendor: %s", info.sVendor)), nil
  21. }
  22. }

MijiaLight.go

适配厂商为”mijia”的智能灯的控制

  1. package simple_factory
  2. import "fmt"
  3. type tMijiaLight struct {
  4. LightInfo
  5. }
  6. func newMijiaLight(info *LightInfo) *tMijiaLight {
  7. return &tMijiaLight{
  8. *info,
  9. }
  10. }
  11. func (me *tMijiaLight) Open() error {
  12. fmt.Printf("MijiaLight.open, %v\n", &me.LightInfo)
  13. return nil
  14. }
  15. func (me *tMijiaLight) Close() error {
  16. fmt.Printf("MijiaLight.Close, %v\n", &me.LightInfo)
  17. return nil
  18. }

RedmiLight.go

适配厂商为”redmi”的智能灯的控制

  1. package simple_factory
  2. import "fmt"
  3. type tRedmiLight struct {
  4. LightInfo
  5. }
  6. func newRedmiLight(info *LightInfo) *tRedmiLight {
  7. return &tRedmiLight{
  8. *info,
  9. }
  10. }
  11. func (me *tRedmiLight) Open() error {
  12. fmt.Printf("tRedmiLight.Open, %v\n", &me.LightInfo)
  13. return nil
  14. }
  15. func (me *tRedmiLight) Close() error {
  16. fmt.Printf("tRedmiLight.Close, %v\n", &me.LightInfo)
  17. return nil
  18. }

小结

简单工厂模式主要包含3个角色。
(1)简单工厂(SimpleFactory x 1):是简单工厂模式的核心,负责实现创建所有实例的内部逻辑。工厂类的创建产品类的方法可以被外界直接调用,创建所需的产品对象。
(2)抽象产品(IProduct x 1):是简单工厂创建的所有对象的父类,负责描述所有实例共有的公共接口。
(3)具体产品(ConcreteProduct x N):是简单工厂模式的创建目标。