关于golang:手撸golang-创建型设计模式-简单工厂

11次阅读

共计 3024 个字符,预计需要花费 8 分钟才能阅读完成。

手撸 golang 创立型设计模式 简略工厂

缘起

最近温习设计模式
拜读谭勇德的 << 设计模式就该这样学 >>
该书以 java 语言演绎了常见设计模式
本系列笔记拟采纳 golang 练习之

简略工厂

简略工厂模式(Simple Factory Pattern)又叫作动态工厂办法模式(Static Factory Method Pattern),简略来说,简略工厂模式有一个具体的工厂类,能够生成多个不同的产品,属于创立型设计模式。
_

场景

  • 某智能家居场景, 须要通过 app 对立管制智能照明灯的开关
  • 智能灯能够关上 – Open(), 或敞开 – Close()
  • 智能灯可能来自不同厂商, 管制驱动不一样, 具体信息保留在配置文件中

设计

  • 定义 ILight 接口, 示意智能灯
  • 定义 ILightFactory 接口, 示意创立智能灯的简略工厂
  • 定义 LightInfo 类, 保留不同灯的配置信息
  • 定义 LightFactory 类, 依据配置信息, 创立具体的智能灯实例

simple_factory_test.go

单元测试

package patterns_test

import "testing"
import (sf "learning/gooop/creational_patterns/simple_factory")

func Test_SimpleFactory(t *testing.T) {config := make([]*sf.LightInfo, 0)
    config = append(config, sf.NewLightInfo(1, "客厅灯", "mijia", "L-100"))
    config = append(config, sf.NewLightInfo(2, "餐厅灯", "redmi", "L-45"))

    factory := sf.DefaultLightFactory
    for _,info := range config {e, light := factory.Create(info)
        if e != nil {t.Error(e.Error())
        } else {_ = light.Open()
            _ = light.Close()}
    }
}

测试输入

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

ILight.go

定义智能灯的对立接口

package simple_factory

type ILight interface {ID() int
    Name() string

    Open() error
    Close() error}

ILightFactory.go

定义智能灯的创立工厂

package simple_factory

type ILightFactory interface {Create(info *LightInfo) (error, ILight)
}

LightInfo.go

封装智能灯的配置信息

package simple_factory

type LightInfo struct {
    iID int
    sName string
    sVendor string
    sModel string
}

func NewLightInfo(id int, name string, vendor string, model string) *LightInfo {
    return &LightInfo{id, name, vendor, model,}
}

func (me *LightInfo) ID() int {return me.iID}

func (me *LightInfo) Name() string {return me.sName}

LightFactory.go

tLightFactory 是实现 ILightFactory 的简略工厂, 通过输出参数创立不同的 ILight 实例

package simple_factory

import (
    "errors"
    "fmt"
    "strings"
)

var DefaultLightFactory = newLightFactory()

type tLightFactory struct {
}

func newLightFactory() ILightFactory {return &tLightFactory{}
}

func (me *tLightFactory) Create(info *LightInfo) (error, ILight) {switch strings.ToLower(info.sVendor) {
    case "mijia":
        return nil, newMijiaLight(info)
    case "redmi":
        return nil, newRedmiLight(info)
    default:
        return errors.New(fmt.Sprintf("unsupported vendor: %s", info.sVendor)), nil
    }
}

MijiaLight.go

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

package simple_factory

import "fmt"

type tMijiaLight struct {LightInfo}


func newMijiaLight(info *LightInfo) *tMijiaLight {
    return &tMijiaLight{*info,}
}

func (me *tMijiaLight) Open() error {fmt.Printf("MijiaLight.open, %v\n", &me.LightInfo)
    return nil
}

func (me *tMijiaLight) Close() error {fmt.Printf("MijiaLight.Close, %v\n", &me.LightInfo)
    return nil
}

RedmiLight.go

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

package simple_factory

import "fmt"

type tRedmiLight struct {LightInfo}

func newRedmiLight(info *LightInfo) *tRedmiLight {
    return &tRedmiLight{*info,}
}

func (me *tRedmiLight) Open() error {fmt.Printf("tRedmiLight.Open, %v\n", &me.LightInfo)
    return nil
}

func (me *tRedmiLight) Close() error {fmt.Printf("tRedmiLight.Close, %v\n", &me.LightInfo)
    return nil
}

小结

简略工厂模式次要蕴含 3 个角色。
(1)简略工厂(SimpleFactory x 1):是简略工厂模式的外围,负责实现创立所有实例的外部逻辑。工厂类的创立产品类的办法能够被外界间接调用,创立所需的产品对象。
(2)形象产品(IProduct x 1):是简略工厂创立的所有对象的父类,负责形容所有实例共有的公共接口。
(3)具体产品(ConcreteProduct x N):是简略工厂模式的创立指标。

正文完
 0