• Go 语言 设计模式-生成器模式


    Go 语言 设计模式-生成器模式

    生成器是一种创建型设计模式, 使你能够分步骤创建复杂对象。

    与其他创建型模式不同, 生成器不要求产品拥有通用接口。 这使得用相同的创建过程生成不同的产品成为可能。

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b6RrsAOd-1663209453022)(media/16632086574682/16632087064995.jpg)]

    概念示例

    当所需产品较为复杂且需要多个步骤才能完成时, 也可以使用生成器模式。 在这种情况下, 使用多个构造方法比仅仅使用一个复杂可怕的构造函数更简单。 分为多个步骤进行构建的潜在问题是, 构建不完整的和不稳定的产品可能会被暴露给客户端。 生成器模式能够在产品完成构建之前使其处于私密状态。

    在下方的代码中, 我们可以看到 igloo­Builder冰屋生成器nor­mal­Builder普通房屋生成器可建造不同类型房屋, 即 igloo冰屋nor­mal­House普通房屋 。 每种房屋类型的建造步骤都是相同的。 主管 (可选) 结构体可对建造过程进行组织。

    iBuilder.go: 生成器接口

    package main
    
    type IBuilder interface {
        setWindowType()
        setDoorType()
        setNumFloor()
        getHouse() House
    }
    
    func getBuilder(builderType string) IBuilder {
        if builderType == "normal" {
            return newNormalBuilder()
        }
    
        if builderType == "igloo" {
            return newIglooBuilder()
        }
        return nil
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    normalBuilder.go: 具体生成器

    package main
    
    type NormalBuilder struct {
        windowType string
        doorType   string
        floor      int
    }
    
    func newNormalBuilder() *NormalBuilder {
        return &NormalBuilder{}
    }
    
    func (b *NormalBuilder) setWindowType() {
        b.windowType = "Wooden Window"
    }
    
    func (b *NormalBuilder) setDoorType() {
        b.doorType = "Wooden Door"
    }
    
    func (b *NormalBuilder) setNumFloor() {
        b.floor = 2
    }
    
    func (b *NormalBuilder) getHouse() House {
        return House{
            doorType:   b.doorType,
            windowType: b.windowType,
            floor:      b.floor,
        }
    }
    
    • 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

    iglooBuilder.go: 具体生成器

    package main
    
    type IglooBuilder struct {
        windowType string
        doorType   string
        floor      int
    }
    
    func newIglooBuilder() *IglooBuilder {
        return &IglooBuilder{}
    }
    
    func (b *IglooBuilder) setWindowType() {
        b.windowType = "Snow Window"
    }
    
    func (b *IglooBuilder) setDoorType() {
        b.doorType = "Snow Door"
    }
    
    func (b *IglooBuilder) setNumFloor() {
        b.floor = 1
    }
    
    func (b *IglooBuilder) getHouse() House {
        return House{
            doorType:   b.doorType,
            windowType: b.windowType,
            floor:      b.floor,
        }
    }
    
    • 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

    house.go: 产品

    package main

    type House struct {
        windowType string
        doorType   string
        floor      int
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    director.go: 主管

    package main
    
    type Director struct {
        builder IBuilder
    }
    
    func newDirector(b IBuilder) *Director {
        return &Director{
            builder: b,
        }
    }
    
    func (d *Director) setBuilder(b IBuilder) {
        d.builder = b
    }
    
    func (d *Director) buildHouse() House {
        d.builder.setDoorType()
        d.builder.setWindowType()
        d.builder.setNumFloor()
        return d.builder.getHouse()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    main.go: 客户端代码

    package main
    
    import "fmt"
    
    func main() {
        normalBuilder := getBuilder("normal")
        iglooBuilder := getBuilder("igloo")
    
        director := newDirector(normalBuilder)
        normalHouse := director.buildHouse()
    
        fmt.Printf("Normal House Door Type: %s\n", normalHouse.doorType)
        fmt.Printf("Normal House Window Type: %s\n", normalHouse.windowType)
        fmt.Printf("Normal House Num Floor: %d\n", normalHouse.floor)
    
        director.setBuilder(iglooBuilder)
        iglooHouse := director.buildHouse()
    
        fmt.Printf("\nIgloo House Door Type: %s\n", iglooHouse.doorType)
        fmt.Printf("Igloo House Window Type: %s\n", iglooHouse.windowType)
        fmt.Printf("Igloo House Num Floor: %d\n", iglooHouse.floor)
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    output.txt: 执行结果

    Normal House Door Type: Wooden Door
    Normal House Window Type: Wooden Window
    Normal House Num Floor: 2
    
    Igloo House Door Type: Snow Door
    Igloo House Window Type: Snow Window
    Igloo House Num Floor: 1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    Vue3 + ts 开发一个ProTable
    记录发生同一个实体用在多个不同的controller接口展示不同的字段报空指针的问题。
    「学编程常见问题」学Java要准备什么配置的电脑?
    怎么做加密文件二维码?简单技巧快速做二维码
    springboot+vue+nodejs+java卤菜品销售商城系统
    KMP算法的一些注意事项
    信息系统项目管理师核心考点(二十八)项目进度计划输出(成果)
    Windows AD共享权限管理工具
    微软出品自动化神器【Playwright+Java】系列(十)元素定位详解
    Android使用glide时报错“ ����: �޷�����Fragment Glide.with(getContext()) ^ �Ҳ���and”
  • 原文地址:https://blog.csdn.net/weixin_43064185/article/details/126866597