Golang設計模式之生成器模式講解和代碼示例
Go 生成器模式講解和代碼示例
概念示例
當所需產(chǎn)品較為復雜且需要多個步驟才能完成時, 也可以使用生成器模式。 在這種情況下, 使用多個構(gòu)造方法比僅僅使用一個復雜可怕的構(gòu)造函數(shù)更簡單。 分為多個步驟進行構(gòu)建的潛在問題是, 構(gòu)建不完整的和不穩(wěn)定的產(chǎn)品可能會被暴露給客戶端。 生成器模式能夠在產(chǎn)品完成構(gòu)建之前使其處于私密狀態(tài)。
在下方的代碼中, 我們可以看到 iglooBuilder
冰屋生成器與 normalBuilder
普通房屋生成器可建造不同類型房屋, 即 igloo
冰屋和 normalHouse
普通房屋 。 每種房屋類型的建造步驟都是相同的。 主管 (可選) 結(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: 2Igloo House Door Type: Snow Door
Igloo House Window Type: Snow Window
Igloo House Num Floor: 1
以上就是Golang設計模式之生成器模式講解和代碼示例的詳細內(nèi)容,更多關于Golang 生成器模式的資料請關注腳本之家其它相關文章!
相關文章
詳解golang開發(fā)中http請求redirect的問題
這篇文章主要介紹了詳解golang開發(fā)中http請求redirect的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10Golang基于Vault實現(xiàn)敏感數(shù)據(jù)加解密
數(shù)據(jù)加密是主要的數(shù)據(jù)安全防護技術之一,敏感數(shù)據(jù)應該加密存儲在數(shù)據(jù)庫中,降低泄露風險,本文將介紹一下利用Vault實現(xiàn)敏感數(shù)據(jù)加解密的方法,需要的可以參考一下2023-07-07Golang中fsnotify包監(jiān)聽文件變化的原理詳解
Golang提供了一個強大的fsnotify包,它能夠幫助我們輕松實現(xiàn)文件系統(tǒng)的監(jiān)控,本文將深入探討fsnotify包的原理,感興趣的小伙伴可以跟隨小編一起學習一下2023-12-12