Golang设计模式之生成器模式讲解和代码示例

 更新时间:2023年06月21日 09:26:05   作者:demo007x  
生成器是一种创建型设计模式,使你能够分步骤创建复杂对象,与其他创建型模式不同,生成器不要求产品拥有通用接口,这使得用相同的创建过程生成不同的产品成为可能,本文就通过代码示例为大家详细介绍Golang生成器模式,感兴趣的同学可以参考下

Go 生成器模式讲解和代码示例

概念示例

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

在下方的代码中, 我们可以看到 igloo­Builder冰屋生成器与 normal­Builder普通房屋生成器可建造不同类型房屋, 即 igloo冰屋和 normal­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
}

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,
    }
}

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,
    }
}

house.go: 产品

package main
type House struct {
    windowType string
    doorType   string
    floor      int
}

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()
}

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)
}

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

以上就是Golang设计模式之生成器模式讲解和代码示例的详细内容,更多关于Golang 生成器模式的资料请关注脚本之家其它相关文章!

相关文章

  • 8个Elasticsearch高频面试题和答案整理

    8个Elasticsearch高频面试题和答案整理

    这篇文章为大家精选了8道Elasticsearch高频面试题和答案,并且给出了这些知识点的应用场景、也给出了解决这些问题的思路,希望对大家有所帮助
    2023-06-06
  • Go调度器学习之协作与抢占详解

    Go调度器学习之协作与抢占详解

    如果某个G执行时间过长,其他的G如何才能被正常调度,这就引出了接下来的话题:协作与抢占。本文将通过一些示例为大家详细讲讲调度器中协作与抢占的相关知识,需要的可以参考一下
    2023-04-04
  • Go并发与锁的两种方式该如何提效详解

    Go并发与锁的两种方式该如何提效详解

    如果没有锁,在我们的项目中,可能会存在多个goroutine同时操作一个资源(临界区),这种情况会发生竞态问题(数据竞态),下面这篇文章主要给大家介绍了关于Go并发与锁的两种方式该如何提效的相关资料,需要的朋友可以参考下
    2022-12-12
  • Golang中的[]byte与16进制(String)之间的转换方式

    Golang中的[]byte与16进制(String)之间的转换方式

    这篇文章主要介绍了Golang中的[]byte与16进制(String)之间的转换方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • 彻底理解golang中什么是nil

    彻底理解golang中什么是nil

    这篇文章主要介绍了golang中的nil用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • 基于Go语言实现的简易api网关的示例代码

    基于Go语言实现的简易api网关的示例代码

    本文主要介绍了基于Go语言实现的简易api网关,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • 一文详解Go语言中的Option设计模式

    一文详解Go语言中的Option设计模式

    这篇文章主要为大家详细介绍了Go语言中Option设计模式的相关知识,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的可以了解一下
    2023-05-05
  • Go语言中获取IP地址的方法小结

    Go语言中获取IP地址的方法小结

    这篇文章主要为大家详细介绍了Go语言中获取IP地址的常用方法,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-12-12
  • Go Java算法最大单词长度乘积示例详解

    Go Java算法最大单词长度乘积示例详解

    这篇文章主要为大家介绍了Go Java算法最大单词长度乘积示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Go中http超时问题的排查及解决方法

    Go中http超时问题的排查及解决方法

    这篇文章主要介绍了Go中http超时问题的排查及解决方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-10-10

最新评论