• Golang语言设计模式的实现—工厂模式


    简单工厂模式

    由于 Go 本身是没有构造函数的,一般而言我们采用 New() 的方式创建对象/接口,当它返回的是接口的时候,其实就是简单工厂模式

    1.类图如下

    在这里插入图片描述
    工厂角色:负责实现创建所有实例的内部逻辑,工厂类可以被外界直接调用,创建所需的产品对象
    抽象产品角色:简单工厂模式所创建的所有对象的父类,负责描述所有实例共有的公共接口
    具体产品角色:简单工厂模式所创建的具体实例对象

    2.实现代码

    package main
    
    import "fmt"
    
    // Fruit 抽象层
    type Fruit interface {
    	Show()
    }
    
    //实现层
    type Apple struct {
    }
    
    func (a *Apple) Show() {
    	fmt.Println("我是苹果")
    }
    
    type Banana struct {
    }
    
    func (b *Banana) Show() {
    	fmt.Println("我是香蕉")
    }
    
    type Pear struct {
    }
    
    func (p *Pear) Show() {
    	fmt.Println("我是梨")
    }
    
    //工厂模式
    type Factory struct {
    }
    
    // CreatFruit 工厂的生产器返回的是抽象的方法
    func (f *Factory) CreatFruit(name string) Fruit {
    	var fruit Fruit
    	if name == "apple" {
    		fruit = new(Apple)
    	} else if name == "banana" {
    		fruit = new(Banana)
    	} else if name == "pear" {
    		fruit = new(Pear)
    	}
    	return fruit
    }
    //业务逻辑层
    func main() {
    	factory := new(Factory)
    	apple := factory.CreatFruit("apple")
    	apple.Show()
    	banana := factory.CreatFruit("banana")
    	banana.Show()
    	pear := factory.CreatFruit("pear")
    	pear.Show()
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    3.优缺点分析

    优点:
    1.实现了对象创建和使用的分离。
    2.不需要记住具体类名,记住参数即可

    缺点:
    1.增加系统中类的个数,复杂度和理解度增加。
    2.违反“开闭原则”,添加新产品需要修改工厂逻辑,工厂越来越复杂。

    适用场景:
    1.工厂类负责创建的对象比较少,由于创建的对象较少,不会造成工厂方法中的业务逻辑太过复杂。
    2.客户端只知道传入工厂类的参数,对于如何创建对象并不关心

    工厂方法模式

    当对象的创建逻辑比较复杂,不只是简单的 new 一下就可以,而是要组合其他类对象,做各种初始化操作的时候,推荐使用工厂方法模式,将复杂的创建逻辑拆分到多个工厂类中,让每个工厂类都不至于过于复杂

    简单工厂模式  +  "开闭原则" = 工厂方法模式
    
    • 1

    1.类图如下

    在这里插入图片描述
    和简单工厂模式的区别:将工厂抽取出来,形成一个抽象的工厂,返回一个抽象的产品类
    抽象工厂下有具体工厂,具体工厂实现抽象工厂的方法,返回抽象产品
    业务层只需要和抽象的工厂和抽象的产品打交道

    2.实现代码

    package main
    
    import "fmt"
    
    //简单工厂+开闭原则=工厂模式
    
    type Fruit interface {
    	Show()
    }
    
    type Apple struct {
    }
    
    func (a *Apple) Show() {
    	fmt.Println("我是苹果")
    }
    
    type Banana struct {
    }
    
    func (b *Banana) Show() {
    	fmt.Println("我是香蕉")
    }
    
    type Pear struct {
    }
    
    func (p *Pear) Show() {
    	fmt.Println("我是梨")
    }
    
    type Factory interface {
    	CreatFruit() Fruit
    }
    type AppleFactory struct {
    }
    
    func (a *AppleFactory) CreatFruit() Fruit {
    	var apple Fruit
    	apple = new(Apple)
    	return apple
    }
    
    type BananaFactory struct {
    }
    
    func (b *BananaFactory) CreatFruit() Fruit {
    	var banana Fruit
    	banana = new(Apple)
    	return banana
    }
    
    type PearFactory struct {
    }
    
    func (p *PearFactory) CreatFruit() Fruit {
    	var pear Fruit
    	pear = new(Apple)
    	return pear
    }
    func main() {
    	//需求:需要一个具体的苹果对象
    	//需要一个具体的苹果工厂
    	appleFactory := new(AppleFactory)
    	//生产一个具体的苹果
    	apple := appleFactory.CreatFruit()
    	apple.Show()
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    3.优缺点分析

    优点:
    1.实现了对象创建和使用的分离
    2.系统的可扩展性也就变得非常好,无需修改接口和原类
    3.对于新产品的创建,符合开闭原则

    缺点:
    1.增加系统中类的个数,复杂度和理解度增加
    2.增加了系统的抽象性和理解难度。

    适用场景:
    1.客户端不知道它所需要的对象的类。
    2.抽象工厂类通过其子类来指定创建哪个对象

  • 相关阅读:
    无涯教程-JavaScript - AND函数
    BUUCTF 金三 1
    系统性能测试工具
    函数默认参数避坑指南
    从输入URL到页面展示这中间发生了什么?
    java多线程并发环境下为什么使用while而不用if
    理解C/C++中的链接
    Python 发展趋势:与 Rust 深度融合、更易于编写 Web 应用
    接口数据源变更,用 DeepDiff 测
    LabView 总结 持续追加
  • 原文地址:https://blog.csdn.net/Qiaoshurui/article/details/127847165