手擼golang 建立型設計模式 簡單工廠

手擼golang 建立型設計模式 簡單工廠

緣起

最近複習設計模式
拜讀譚勇德的<<設計模式就該這樣學>>
該書以java語言演繹了常見設計模式
本系列筆記擬採用golang練習之java

簡單工廠

簡單工廠模式(Simple Factory Pattern)又叫做靜態工廠方法模式(Static Factory Method Pattern),簡單來講,簡單工廠模式有一個具體的工廠類,能夠生成多個不一樣的產品,屬於建立型設計模式。
_golang

場景

  • 某智能家居場景, 須要經過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

定義智能燈的統一接口app

package simple_factory

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

    Open() error
    Close() error
}

ILightFactory.go

定義智能燈的建立工廠oop

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"的智能燈的控制code

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):是簡單工廠模式的建立目標。對象

相關文章
相關標籤/搜索