Skip to content

Latest commit

 

History

History
168 lines (130 loc) · 4.38 KB

02.factory.mode.md

File metadata and controls

168 lines (130 loc) · 4.38 KB

工厂方法模式

概念

  • 工厂方法模式又称为工厂模式,也叫虚拟构造器模式或者多态工厂模式,它属于类创建型模式。
  • 在工厂方法模式中,工厂父类负责定义创建产品对象的公共接口,而工厂子类则负责生成具体的产品对象。

结构

njhn6YbNu0MOPUz

模拟推演

// ------------------------------------------------------------------------
// 定义一个抽象的产品
// ------------------------------------------------------------------------
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{})}
}

总结

  • 优点:保持了简单工厂模式的优点,而且克服了它的缺点。
  • 缺点:在添加新产品时,在一定程度上增加了系统的复杂度。
  • 适合:客户端不需要知道具体产品类的类名,只需要知道所对应的工厂即可。