Golang设计模式——02抽象工厂模式

简介: Golang设计模式——02抽象工厂模式

抽象工厂模式

优点

  1. 提供一个创建一系列相关或者依赖对象的接口,而无需指定他们具体的类。
  2. 解决接口选择的问题。
  3. 抽象工厂模式不可以增加产品,但是可以增加产品族
  4. 增加新的具体工厂和产品族很方便,无须修改已有系统,符合“开闭原则”。
  5. 如果同一家工厂需要生产不止一种产品,那么使用工厂模式会造成工厂过多的情况,使用抽象工厂模式可以使同一个工厂组成一个“产品族”,即可以生产多种产品。

缺点

  1. 产品族的扩展将是一件十分费力的事情,假如产品族中需要增加一个新的产品,则几乎所有的工厂类都需要进行修改。所以使用抽象工厂模式时,对产品等级结构的划分是非常重要的。

场景

当需要创建的对象是一系列相互关联或相互依赖的产品族时,便可以使用抽象工厂模式。说的更明白一点,就是一个继承体系中,如果存在着多个等级结构(即存在着多个抽象类),并且分属各个等级结构中的实现类之间存在着一定的关联或者约束,就可以使用抽象工厂模式。假如各个等级结构中的实现类之间不存在关联或约束,则使用多个独立的工厂来对产品进行创建,则更合适一点。

代码

package Abstract_Factory
import "fmt"
type mouse interface {
  click()
}
type AsusMouse struct {
}
func (a *AsusMouse) click() {
  fmt.Println("Asus Mouse Check screen")
}
type LogitechMouse struct {
}
func (l *LogitechMouse) click() {
  fmt.Println("Logitech Mouse Check screen")
}
type keyboard interface {
  press()
}
type AsusKeyboard struct {
}
func (a *AsusKeyboard) press() {
  fmt.Println("Asus keyboard press")
}
type LogitechKeyboard struct {
}
func (l *LogitechKeyboard) press() {
  fmt.Println("Logitech Keyboard press")
}
type AbstractFactory interface {
  produceMouse(brand string) mouse
  produceKeyboard(brand string) keyboard
}
type MouseFactory struct {
}
func (m *MouseFactory) produceMouse(brand string) mouse {
  switch brand {
  case "Asus":
    return &AsusMouse{}
  case "Logitech":
    return &LogitechMouse{}
  }
  return nil
}
func (m *MouseFactory) produceKeyboard(brand string) keyboard {
  return nil
}
type KeyboardFactory struct {
}
func (k KeyboardFactory) produceMouse(brand string) mouse {
  return nil
}
func (k KeyboardFactory) produceKeyboard(brand string) keyboard {
  switch brand {
  case "Asus":
    return &AsusKeyboard{}
  case "Logitech":
    return &LogitechKeyboard{}
  }
  return nil
}
func NewFactory(factory string) AbstractFactory {
  switch factory {
  case "mouse":
    return &MouseFactory{}
  case "keyboard":
    return &KeyboardFactory{}
  }
  return nil
}


package Abstract_Factory
import "testing"
func TestNewFactory(t *testing.T) {
  factory := NewFactory("mouse")
  AsusMouse := factory.produceMouse("Asus")
  AsusMouse.click()
}

其他设计模式

设计模式Git源代码

00简单工厂模式

01工厂方法模式

02抽象工厂模式

03外观模式

04建造者模式

05桥接模式

06命令模式

07迭代器模式

08模板模式

09访问者模式

10备忘录模式

11责任链模式

12中介模式

13原型模式

14状态模式

15策略模式

16享元模式

17组合模式

18解释器模式

19单例模式

20适配器模式

21代理模式

22装饰器模式

23观察者模式


目录
相关文章
|
3月前
|
设计模式 Go 开发工具
Golang设计模式——12中介模式
Golang设计模式——12中介模式
26 0
|
3月前
|
设计模式 Go 开发工具
Golang设计模式——04建造者模式
Golang设计模式——04建造者模式
23 0
|
7天前
|
设计模式 Java
【设计模式系列笔记】抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是一种设计模式,属于创建型模式之一。它提供了一种方式来创建一系列相关或相互依赖的对象,而无需指定它们具体的类。抽象工厂模式通过引入抽象的工厂接口,使得客户端代码可以使用抽象的接口来创建一组相关的产品,而不关心这些产品的具体实现。
103 4
|
23天前
|
设计模式 Java Windows
23种设计模式,抽象工厂模式的概念优缺点以及JAVA代码举例
【4月更文挑战第10天】抽象工厂模式是一种创建型设计模式,它提供了一个接口用于创建相关或依赖对象的家族,而不需要指定具体类。该模式允许客户端在不知道具体类的情况下,通过其共同的接口来创建一组产品。
29 7
|
3月前
|
设计模式 Oracle 关系型数据库
设计模式 | 抽象工厂模式
设计模式 | 抽象工厂模式
25 0
|
3月前
|
设计模式 消息中间件 Go
Golang设计模式——23观察者模式
Golang设计模式——23观察者模式
23 0
|
3月前
|
设计模式 Go 网络安全
Golang设计模式——21代理模式
Golang设计模式——21代理模式
23 0
|
3月前
|
设计模式 Go 开发工具
Golang设计模式——19单例模式
Golang设计模式——19单例模式
26 0
|
3月前
|
设计模式 Go 开发工具
Golang设计模式——18解释器模式
Golang设计模式——18解释器模式
22 0
Golang设计模式——18解释器模式
|
3月前
|
设计模式 Go 开发工具
Golang设计模式——17组合模式
Golang设计模式——17组合模式
13 1