golang 创建型设计模式 抽象工厂
缘起
最近复习设计模式
拜读谭勇德的<<设计模式就该这样学>>
本系列笔记拟采用golang练习之
抽象工厂
抽象工厂模式(Abstract Factory Pattern)指提供一个创建一系列相关或相互依赖对象的接口,无须指定它们具体的类。意思是客户端不必指定产品的具体类型,创建多个产品族中的产品对象。
_
抽象工厂vs工厂方法
- 抽象工厂是创建一系列对象, 且这些对象之间可能存在相关性. 工厂方法通常是创建单一类别的对象
场景
- 某智能家居场景, 需要通过app统一控制各种设备
- 目前支持智能灯(ILight)和空调(IAirConditioner)
- 不同厂家, 通过提供不同的抽象工厂实现, 接入系统, 提供各种设备的控制
设计
- 定义ISmartDevice接口, 作为所有智能设备的基础接口
- 定义ILight, 抽象智能灯的接口
- 定义IAirConditioner, 抽象空调的接口
- 定义ISmartFactory, 抽象工厂接口, 定义创建智能灯 - CreateLight, 和创建空调 - CreateAirConditioner的方法
- 定义并实现IFactoryRegistry, 提供厂商标识符到抽象工厂实例的全局注册表
- 创建"mijia"子目录, 接入"mijia"厂商的智能灯和空调
- tMijiaLight, 实现ILight接口
- tMijiaAirConditioner, 实现IAirConditioner接口
- tMijiaFactory, 实现ISmartFactory接口, 并在init方法中注册到全局的工厂注册表
- 创建"redmi"子目录, 接入"redmi"厂商的智能灯和空调
abstract_factory_test.go
单元测试
package patterns
import (
af "learning/gooop/creational_patterns/abstract_factory"
"testing"
// 引入并注册厂商"mijia"
_ "learning/gooop/creational_patterns/abstract_factory/mijia"
// 引入并注册厂商"redmi"
_ "learning/gooop/creational_patterns/abstract_factory/redmi"
)
func Test_AbstractFactory(t *testing.T) {
config := make([]*af.DeviceInfo, 0)
config = append(config, af.NewDeviceInfo(1, "客厅灯", af.DeviceTypeLight, "mijia", "ML-100"))
config = append(config, af.NewDeviceInfo(2, "餐厅灯", af.DeviceTypeLight, "redmi", "RL-45"))
config = append(config, af.NewDeviceInfo(11, "主卧空调", af.DeviceTypeAirConditioner, "mijia", "MA-15"))
config = append(config, af.NewDeviceInfo(12, "次卧空调", af.DeviceTypeAirConditioner, "redmi", "RA-10"))
for _,info := range config {
factory := af.DefaultFactoryRegistry.Get(info.Vendor())
if factory == nil {
t.Errorf("unsupported vendor: %s", info.Vendor())
} else {
switch info.GetType() {
case af.DeviceTypeLight:
e,light := factory.CreateLight(info)
if e != nil {
t.Error(e.Error())
} else {
_ = light.Open()
_,_ = light.GetLightMode()
_ = light.SetLightMode(1)
_,_ = light.GetLightMode()
_ = light.Close()
}
break
case af.DeviceTypeAirConditioner:
e,ac := factory.CreateAirConditioner(info)
if e != nil {
t.Error(e.Error())
} else {
_ = ac.Open()
_,_ = ac.GetTemperature()
_ = ac.SetTemperature(26.5)
_,_ = ac.GetTemperature()
_ = ac.Close()
}
break
}
}
}
}
测试输出
$ go test -v abstract_factory_test.go
=== RUN Test_AbstractFactory
tMijiaLight.Open, &{1 客厅灯 Light mijia ML-100}
tMijiaLight.GetLightMode, 0
tMijiaLight.SetLightMode, 1
tMijiaLight.GetLightMode, 1
tMijiaLight.Close, &{1 客厅灯 Light mijia ML-100}
tRedmiLight.Open, &{2 餐厅灯 Light redmi RL-45}
tRedmiLight.GetLightMode, 0
tRedmiLight.SetLightMode, 1
tRedmiLight.GetLightMode, 1
tRedmiLight.Close, &{2 餐厅灯 Light redmi RL-45}
tMijiaLight.Open, &{11 主卧空调 AirConditioner mijia MA-15}
tMijiaAirConditioner.GetTemperature, 0
tMijiaAirConditioner.SetTemperature, 26.5
tMijiaAirConditioner.GetTemperature, 26.5
tMijiaLight.Close, &{11 主卧空调 AirConditioner mijia MA-15}
tRedmiAirConditioner.Open, &{12 次卧空调 AirConditioner redmi RA-10}
tRedmiAirConditioner.GetTemperature, 0
tRedmiAirConditioner.SetTemperature, 26.5
tRedmiAirConditioner.GetTemperature, 26.5
tRedmiAirConditioner.Close, &{12 次卧空调 AirConditioner redmi RA-10}
--- PASS: Test_AbstractFactory (0.00s)
PASS
ok command-line-arguments 0.002s
ISmartDevice.go
抽象智能设备的基础接口
package abstract_factory
type ISmartDevice interface {
ID() int
Name() string
Open() error
Close() error
}
ILight.go
抽象智能灯的接口, 继承ISmartDevice接口, 并添加灯光模式的控制方法
package abstract_factory
type ILight interface {
ISmartDevice
GetLightMode() (error, int)
SetLightMode(mode int) error
}
IAirConditioner.go
抽象空调的接口, 继承ISmartDevice接口, 并添加温度控制的方法
package abstract_factory
type IAirConditioner interface {
ISmartDevice
GetTemperature() (error, float64)
SetTemperature(t float64) error
}
DeviceInfo.go
智能设备的配置信息
package abstract_factory
type DeviceInfo struct {
iID int
sName string
sType DeviceType
sVendor string
sModel string
}
type DeviceType string
const DeviceTypeLight DeviceType = "Light"
const DeviceTypeAirConditioner DeviceType = "AirConditioner"
func NewDeviceInfo(id int, name string, devType DeviceType, vendor string, model string) *DeviceInfo {
return &DeviceInfo{
id, name, devType, vendor, model,
}
}
func (me *DeviceInfo) ID() int {
return me.iID
}
func (me *DeviceInfo) Name() string {
return me.sName
}
func (me *DeviceInfo) Vendor() string {
return me.sVendor
}
func (me *DeviceInfo) GetType() DeviceType {
return me.sType
}
FactoryRegistry.go
从工厂标识符到抽象工厂实例的全局注册表
package abstract_factory
var DefaultFactoryRegistry = newFactoryRegistry()
type IFactoryRegistry interface {
Set(vendor string, factory ISmartFactory)
Get(vendor string) ISmartFactory
}
type tSimpleFactoryRegistry struct {
mFactoryMap map[string]ISmartFactory
}
func newFactoryRegistry() IFactoryRegistry {
return &tSimpleFactoryRegistry{
mFactoryMap: make(map[string]ISmartFactory, 0),
}
}
func (me *tSimpleFactoryRegistry) Set(vendor string, factory ISmartFactory) {
me.mFactoryMap[vendor] = factory
}
func (me *tSimpleFactoryRegistry) Get(vendor string) ISmartFactory {
it,ok := me.mFactoryMap[vendor]
if ok {
return it
}
return nil
}
mijia/MijiaFactory.go
"mijia"厂商实现的工厂类, 在init方法中自动注册到全局注册表
package mijia
import (af "learning/gooop/creational_patterns/abstract_factory")
func init() {
af.DefaultFactoryRegistry.Set("mijia", newMijiaFactory())
}
type tMijiaFactory struct {
}
func newMijiaFactory() af.ISmartFactory {
return &tMijiaFactory{}
}
func (me *tMijiaFactory) CreateLight(info *af.DeviceInfo) (error, af.ILight) {
return nil, newMijiaLight(info)
}
func (me *tMijiaFactory) CreateAirConditioner(info *af.DeviceInfo) (error, af.IAirConditioner) {
return nil, newMijiaAirConditioner(info)
}
mijia/MijiaLight.go
"mijia"厂商的智能灯实现类
package mijia
import (
"fmt"
af "learning/gooop/creational_patterns/abstract_factory"
)
type tMijiaLight struct {
af.DeviceInfo
iMode int
}
func newMijiaLight(info *af.DeviceInfo) *tMijiaLight {
return &tMijiaLight{
*info, 0,
}
}
func (me *tMijiaLight) Open() error {
fmt.Printf("tMijiaLight.Open, %v\n", &me.DeviceInfo)
return nil
}
func (me *tMijiaLight) Close() error {
fmt.Printf("tMijiaLight.Close, %v\n", &me.DeviceInfo)
return nil
}
func (me *tMijiaLight) GetLightMode() (error, int) {
fmt.Printf("tMijiaLight.GetLightMode, %v\n", me.iMode)
return nil, me.iMode
}
func (me *tMijiaLight) SetLightMode(mode int) error {
fmt.Printf("tMijiaLight.SetLightMode, %v\n", mode)
me.iMode = mode
return nil
}
mijia/MijiaAirConditioner
"mijia"厂商的空调实现类
package mijia
import (
"fmt"
af "learning/gooop/creational_patterns/abstract_factory"
)
type tMijiaAirConditioner struct {
af.DeviceInfo
fTemperature float64
}
func newMijiaAirConditioner(info *af.DeviceInfo) *tMijiaAirConditioner {
return &tMijiaAirConditioner{
*info, 0,
}
}
func (me *tMijiaAirConditioner) Open() error {
fmt.Printf("tMijiaLight.Open, %v\n", &me.DeviceInfo)
return nil
}
func (me *tMijiaAirConditioner) Close() error {
fmt.Printf("tMijiaLight.Close, %v\n", &me.DeviceInfo)
return nil
}
func (me *tMijiaAirConditioner) GetTemperature() (error, float64) {
fmt.Printf("tMijiaAirConditioner.GetTemperature, %v\n", me.fTemperature)
return nil, me.fTemperature
}
func (me *tMijiaAirConditioner) SetTemperature(t float64) error {
me.fTemperature = t
fmt.Printf("tMijiaAirConditioner.SetTemperature, %v\n", me.fTemperature)
return nil
}
redmi/RedmiFactory.go
"redmi"厂商实现的工厂类, 在init方法中自动注册到全局注册表. 代码略
redmi/RedmiLight.go
"redmi"厂商的智能灯实现类. 代码略.
redmin/RedmiAirConditioner.go
"redmin"厂商的空调实现类. 代码略.
抽象工厂模式小结
抽象工厂模式的优点
(1)当需要产品族时,抽象工厂可以保证客户端始终只使用同一个产品的产品族。
(2)抽象工厂增强了程序的可扩展性,对于新产品族的增加,只需实现一个新的具体工厂即可,不需要对已有代码进行修改,符合开闭原则。
抽象工厂模式的缺点
(1)规定了所有可能被创建的产品集合,产品族中扩展新的产品困难,需要修改抽象工厂的接口。
(2)增加了系统的抽象性和理解难度。
_
(end)
有疑问加站长微信联系(非本文作者)