- 工厂方法模式又称为工厂模式,也叫虚拟构造器模式或者多态工厂模式,它属于类创建型模式。
- 在工厂方法模式中,工厂父类负责定义创建产品对象的公共接口,而工厂子类则负责生成具体的产品对象。
![njhn6YbNu0MOPUz](https://camo.githubusercontent.com/cce30cbcfd1123db7a2b840314704035e521200ca036f69ff36ed43db7a0f9d9/68747470733a2f2f696d6167652d7374617469632e77756d6f78692e636f6d2f61727469636c652f6e6a686e3659624e75304d4f50557a)
// ------------------------------------------------------------------------
// 定义一个抽象的产品
// ------------------------------------------------------------------------
type Product interface {
SetName(name string)
GetName() string
}
// ------------------------------------------------------------------------
// 实现具体的产品:产品1
// ------------------------------------------------------------------------
type Product1 struct {
name string
}
func (p *Product1) SetName(name string) {
p.name = name
}
func (p *Product1) GetName() string {
return "产品1的名称:" + p.name
}
// ------------------------------------------------------------------------
// 实现具体的产品:产品2
// ------------------------------------------------------------------------
type Product2 struct {
name string
}
func (p *Product2) SetName(name string) {
p.name = name
}
func (p *Product2) GetName() string {
return "产品2的名称:" + p.name
}
// ------------------------------------------------------------------------
// 定义一个抽象的产品工厂
// ------------------------------------------------------------------------
type ProductFactory interface {
Create() Product
}
// ------------------------------------------------------------------------
// 实现具体的产品工厂
// ------------------------------------------------------------------------
type Product1Factory struct{}
func (p *Product1Factory) Create() Product {
return &Product1{}
}
type Product2Factory struct{}
func (p *Product2Factory) Create() Product {
return &Product2{}
}
// ------------------------------------------------------------------------
// 定义一个抽象的Cache接口
// ------------------------------------------------------------------------
type Cache interface {
Set(key string, value interface{})
Get(key string) (interface{}, error)
}
// ------------------------------------------------------------------------
// 实现具体的Cache: RedisCache
// ------------------------------------------------------------------------
type RedisCache struct {
data map[string]interface{}
mux sync.RWMutex
}
func (r *RedisCache) Set(key string, value interface{}) {
r.mux.Lock()
defer r.mux.Unlock()
r.data[key] = value
return
}
func (r *RedisCache) Get(key string) (interface{}, error) {
r.mux.RLock()
defer r.mux.RUnlock()
if v, ok := r.data[key]; ok {
return v, nil
}
return nil, errors.New("key is not found")
}
// ------------------------------------------------------------------------
// 实现具体的Cache: Memcache
// ------------------------------------------------------------------------
type Memcache struct {
data map[string]interface{}
mux sync.RWMutex
}
func (m *Memcache) Set(key string, value interface{}) {
m.mux.Lock()
defer m.mux.Unlock()
m.data[key] = value
return
}
func (m *Memcache) Get(key string) (interface{}, error) {
m.mux.RLock()
defer m.mux.RUnlock()
if v, ok := m.data[key]; ok {
return v, nil
}
return nil, errors.New("key is not found")
}
// ------------------------------------------------------------------------
// 定义一个抽象的缓存工厂
// ------------------------------------------------------------------------
type CacheFactory interface {
Create() Cache
}
// ------------------------------------------------------------------------
// 实现具体的缓存工厂
// ------------------------------------------------------------------------
type RedisCacheFactory struct{}
func (r *RedisCacheFactory) Create() Cache {
return &RedisCache{data: make(map[string]interface{})}
}
type MemcacheFactory struct{}
func (m *MemcacheFactory) Create() Cache {
return &Memcache{data: make(map[string]interface{})}
}
- 优点:保持了简单工厂模式的优点,而且克服了它的缺点。
- 缺点:在添加新产品时,在一定程度上增加了系统的复杂度。
- 适合:客户端不需要知道具体产品类的类名,只需要知道所对应的工厂即可。