最近複習設計模式
拜讀譚勇德的<<設計模式就該這樣學>>
該書以java語言演繹了常見設計模式
本系列筆記擬採用golang練習之java
簡單工廠模式(Simple Factory Pattern)又叫做靜態工廠方法模式(Static Factory Method Pattern),簡單來講,簡單工廠模式有一個具體的工廠類,能夠生成多個不一樣的產品,屬於建立型設計模式。
_golang
單元測試設計模式
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
定義智能燈的統一接口app
package simple_factory type ILight interface { ID() int Name() string Open() error Close() error }
定義智能燈的建立工廠oop
package simple_factory type ILightFactory interface { Create(info *LightInfo) (error, ILight) }
封裝智能燈的配置信息單元測試
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 }
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 } }
適配廠商爲"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 }
適配廠商爲"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):是簡單工廠模式的建立目標。對象