共计 4529 个字符,预计需要花费 12 分钟才能阅读完成。
手撸 golang 创立型设计模式 工厂办法
缘起
最近温习设计模式
拜读谭勇德的 << 设计模式就该这样学 >>
该书以 java 语言演绎了常见设计模式
本系列笔记拟采纳 golang 练习之
工厂办法
工厂办法模式(Factory Method Pattern)又叫作多态性工厂模式,指定义一个创建对象的接口,但由实现这个接口的类来决定实例化哪个类,工厂办法把类的实例化推延到子类中进行。
在工厂办法模式中,不再由繁多的工厂类生产产品,而是由工厂类的子类实现具体产品的创立。因而,当减少一个产品时,只需减少一个相应的工厂类的子类, 以_解决简略工厂生产太多产品导致其外部代码臃肿(switch … case 分支过多)的问题_。
场景
- 某智能家居场景, 须要通过 app 对立管制智能照明灯的开关
- 智能灯能够关上 – Open(), 或敞开 – Close()
- 智能灯可能来自不同厂商, 管制驱动不一样, 具体信息保留在配置文件中
- 当智能灯的种类越来越多当前, 简略工厂办法迅速收缩, 变得难以保护, 因而须要革新为工厂办法
设计
- 定义 ILight 接口, 示意智能灯
- 定义 ILightFactory 接口, 示意创立智能灯的形象工厂
- 定义 LightInfo 类, 保留不同灯的配置信息
- 定义 FactoryRegistry 类, 用于承受不同厂商的工厂子类
- 不同厂商各自实现形象工厂和形象产品
factory_method_test.go
单元测试
package patterns | |
import ( | |
fm "learning/gooop/creational_patterns/factory_method" | |
"testing" | |
// 引入 mijia 并主动注册 | |
_ "learning/gooop/creational_patterns/factory_method/mijia" | |
// 引入 redmi 并主动注册 | |
_ "learning/gooop/creational_patterns/factory_method/redmi" | |
) | |
func Test_FactoryMethod(t *testing.T) {config := make([]*fm.LightInfo, 0) | |
config = append(config, fm.NewLightInfo(1, "客厅灯", "mijia", "L-100")) | |
config = append(config, fm.NewLightInfo(2, "餐厅灯", "redmi", "L-45")) | |
for _,info := range config {factory := fm.DefaultFactoryRegistry.Get(info.Vendor()) | |
if factory == nil {t.Errorf("unsupported vendor: %s", info.Vendor()) | |
} else {e, light := factory.Create(info) | |
if e != nil {t.Error(e.Error()) | |
} else {_ = light.Open() | |
_ = light.Close()} | |
} | |
} | |
} |
测试输入
$ go test -v factory_method_test.go | |
=== RUN Test_FactoryMethod | |
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_FactoryMethod (0.00s) | |
PASS | |
ok command-line-arguments 0.002s |
ILight.go
定义智能灯的接口
package factory_method | |
type ILight interface {ID() int | |
Name() string | |
Open() error | |
Close() error} |
ILightFactory.go
定义智能灯工厂的接口
package factory_method | |
type ILightFactory interface {Create(info *LightInfo) (error, ILight) | |
} |
_
LightInfo.go
封装智能灯的配置信息
package factory_method | |
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} | |
func (me *LightInfo) Vendor() string {return me.sVendor} |
FactoryRegistry.go
提供从厂商名称到该厂商的智能灯工厂实例的注册表
package factory_method | |
var DefaultFactoryRegistry = newFactoryRegistry() | |
type IFactoryRegistry interface {Set(vendor string, factory ILightFactory) | |
Get(vendor string) ILightFactory | |
} | |
type tSimpleFactoryRegistry struct {mFactoryMap map[string]ILightFactory | |
} | |
func newFactoryRegistry() IFactoryRegistry { | |
return &tSimpleFactoryRegistry{mFactoryMap: make(map[string]ILightFactory, 0), | |
} | |
} | |
func (me *tSimpleFactoryRegistry) Set(vendor string, factory ILightFactory) {me.mFactoryMap[vendor] = factory | |
} | |
func (me *tSimpleFactoryRegistry) Get(vendor string) ILightFactory {it,ok := me.mFactoryMap[vendor] | |
if ok {return it} | |
return nil | |
} |
MijiaLightFactory.go
位于 ”mijia” 子目录, 实现 ILightFactory 接口, 提供对 ”mijia” 产品的创立
package mijia | |
import (fm "learning/gooop/creational_patterns/factory_method") | |
func init() {fm.DefaultFactoryRegistry.Set("mijia", newMijiaLightFactory()) | |
} | |
type tMijiaLightFactory struct { | |
} | |
func newMijiaLightFactory() fm.ILightFactory {return &tMijiaLightFactory{} | |
} | |
func (me *tMijiaLightFactory) Create(info *fm.LightInfo) (error, fm.ILight) {return nil, NewMijiaLight(info) | |
} |
MijiaLight.go
位于 ”mijia” 子目录, 实现 ILight 接口, 提供对 ”mijia” 智能灯的实现
package mijia | |
import "fmt" | |
import (fm "learning/gooop/creational_patterns/factory_method") | |
type tMijiaLight struct {fm.LightInfo} | |
func NewMijiaLight(info *fm.LightInfo) *tMijiaLight { | |
return &tMijiaLight{*info,} | |
} | |
func (me *tMijiaLight) Open() error {fmt.Printf("tMijiaLight.open, %v\n", &me.LightInfo) | |
return nil | |
} | |
func (me *tMijiaLight) Close() error {fmt.Printf("tMijiaLight.Close, %v\n", &me.LightInfo) | |
return nil | |
} |
RedmiLightFactory.go
位于 ”redmi” 子目录, 实现 ILightFactory 接口, 提供对 ”redmi” 产品的创立
package redmi | |
import (fm "learning/gooop/creational_patterns/factory_method") | |
func init() {fm.DefaultFactoryRegistry.Set("redmi", newRedmiLightFactory()) | |
} | |
type tRedmiLightFactory struct { | |
} | |
func newRedmiLightFactory() fm.ILightFactory {return &tRedmiLightFactory{} | |
} | |
func (me *tRedmiLightFactory) Create(info *fm.LightInfo) (error, fm.ILight) {return nil, newRedmiLight(info) | |
} |
RedmiLight.go
位于 ”redmi” 子目录, 实现 ILight 接口, 提供对 ”redmi” 智能灯的实现
package redmi | |
import "fmt" | |
import (fm "learning/gooop/creational_patterns/factory_method") | |
type tRedmiLight struct {fm.LightInfo} | |
func newRedmiLight(info *fm.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 | |
} |
小结
工厂办法模式的长处
(1)灵活性加强,对于新产品的创立,只需多写一个相应的工厂类。
(2)典型的解耦框架。高层模块只须要晓得产品的抽象类,毋庸关怀其余实现类,满足迪米特法令、依赖倒置准则和里氏替换准则。
工厂办法模式的毛病
(1)类的个数容易过多,减少复杂度。
(2)减少了零碎的抽象性和了解难度。
(3)形象产品只能生产一种产品,此弊病可应用形象工厂模式解决。