亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Golang設計模式之生成器模式講解和代碼示例

 更新時間:2023年06月21日 09:26:05   作者:demo007x  
生成器是一種創(chuàng)建型設計模式,使你能夠分步驟創(chuàng)建復雜對象,與其他創(chuàng)建型模式不同,生成器不要求產(chǎn)品擁有通用接口,這使得用相同的創(chuàng)建過程生成不同的產(chǎn)品成為可能,本文就通過代碼示例為大家詳細介紹Golang生成器模式,感興趣的同學可以參考下

Go 生成器模式講解和代碼示例

概念示例

當所需產(chǎn)品較為復雜且需要多個步驟才能完成時, 也可以使用生成器模式。 在這種情況下, 使用多個構(gòu)造方法比僅僅使用一個復雜可怕的構(gòu)造函數(shù)更簡單。 分為多個步驟進行構(gòu)建的潛在問題是, 構(gòu)建不完整的和不穩(wěn)定的產(chǎn)品可能會被暴露給客戶端。 生成器模式能夠在產(chǎn)品完成構(gòu)建之前使其處于私密狀態(tài)。

在下方的代碼中, 我們可以看到 igloo­Builder冰屋生成器與 normal­Builder普通房屋生成器可建造不同類型房屋, 即 igloo冰屋和 normal­House普通房屋 。 每種房屋類型的建造步驟都是相同的。 主管 (可選) 結(jié)構(gòu)體可對建造過程進行組織。

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: 產(chǎn)品

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: 執(zhí)行結(jié)果

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設計模式之生成器模式講解和代碼示例的詳細內(nèi)容,更多關于Golang 生成器模式的資料請關注腳本之家其它相關文章!

相關文章

  • 深入了解GoLang中的工廠設計模式

    深入了解GoLang中的工廠設計模式

    這篇文章主要介紹了深入了解GoLang中的工廠設計模式,工廠模式是一種常用的設計模式,它屬于創(chuàng)建型模式,它的主要目的是封裝對象的創(chuàng)建過程,將對象的創(chuàng)建過程與對象的使用過程分離,從而提高代碼的可維護性和可擴展性,需要詳細了解可以參考下文
    2023-05-05
  • Go語言中的自定義類型你了解嗎

    Go語言中的自定義類型你了解嗎

    自定義類型是 Go 語言中非常重要的概念之一,通過自定義類型,我們可以更好地封裝數(shù)據(jù)、組織代碼,提高程序的可讀性和可維護性。本文將從以下幾個方面介紹 Go 自定義類型的相關知識,感興趣的可以了解一下
    2023-04-04
  • 詳解golang開發(fā)中http請求redirect的問題

    詳解golang開發(fā)中http請求redirect的問題

    這篇文章主要介紹了詳解golang開發(fā)中http請求redirect的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • Golang基于Vault實現(xiàn)敏感數(shù)據(jù)加解密

    Golang基于Vault實現(xiàn)敏感數(shù)據(jù)加解密

    數(shù)據(jù)加密是主要的數(shù)據(jù)安全防護技術之一,敏感數(shù)據(jù)應該加密存儲在數(shù)據(jù)庫中,降低泄露風險,本文將介紹一下利用Vault實現(xiàn)敏感數(shù)據(jù)加解密的方法,需要的可以參考一下
    2023-07-07
  • 源碼剖析Golang如何fork一個進程

    源碼剖析Golang如何fork一個進程

    創(chuàng)建一個新進程分為兩個步驟,一個是fork系統(tǒng)調(diào)用,一個是execve?系統(tǒng)調(diào)用,本文將從源碼的角度帶大家剖析一下Golang是如何fork一個進程的
    2023-06-06
  • 簡單聊聊為什么說Go語言字符串是不可變的

    簡單聊聊為什么說Go語言字符串是不可變的

    最近有讀者留言說,平時在寫代碼的過程中,是會對字符串進行修改的,但網(wǎng)上都說 Go 語言字符串是不可變的,這是為什么呢,本文就來和大家簡單講講
    2023-05-05
  • Golang中fsnotify包監(jiān)聽文件變化的原理詳解

    Golang中fsnotify包監(jiān)聽文件變化的原理詳解

    Golang提供了一個強大的fsnotify包,它能夠幫助我們輕松實現(xiàn)文件系統(tǒng)的監(jiān)控,本文將深入探討fsnotify包的原理,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-12-12
  • golang實現(xiàn)RPC模塊的示例

    golang實現(xiàn)RPC模塊的示例

    本文詳細介紹了在Go語言中如何實現(xiàn)RPC模塊,包括RPC服務端和客戶端的構(gòu)建及代碼實現(xiàn),同時提到了使用JSON-RPC的方法,通過簡單的步驟,可以實現(xiàn)跨進程的遠程過程調(diào)用,感興趣的可以了解一下
    2024-09-09
  • golangci-lint安裝與Goland集成問題

    golangci-lint安裝與Goland集成問題

    這篇文章主要介紹了golangci-lint安裝與Goland集成,本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • Go 請求兔子識別接口實現(xiàn)流程示例詳解

    Go 請求兔子識別接口實現(xiàn)流程示例詳解

    這篇文章主要為大家介紹了Go 請求兔子識別接口實現(xiàn)流程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04

最新評論