go 生成器模式的具體使用
生成器模式
生成器模式建議將對(duì)象構(gòu)造代碼從產(chǎn)品類中抽取出來, 并將其放在一個(gè)名為生成器的獨(dú)立對(duì)象中。
模型說明
- 生成器 (Builder) 接口聲明在所有類型生成器中通用的產(chǎn)品構(gòu)造步驟。
- 具體生成器 (Concrete Builders) 提供構(gòu)造過程的不同實(shí)現(xiàn)。 具體生成器也可以構(gòu)造不遵循通用接口的產(chǎn)品。
- 產(chǎn)品 (Products) 是最終生成的對(duì)象。 由不同生成器構(gòu)造的產(chǎn)品無需屬于同一類層次結(jié)構(gòu)或接口。
- 主管 (Director) 類定義調(diào)用構(gòu)造步驟的順序, 這樣你就可以創(chuàng)建和復(fù)用特定的產(chǎn)品配置。
- 客戶端 (Client) 必須將某個(gè)生成器對(duì)象與主管類關(guān)聯(lián)。 一般情況下, 你只需通過主管類構(gòu)造函數(shù)的參數(shù)進(jìn)行一次性關(guān)聯(lián)即可。 此后主管類就能使用生成器對(duì)象完成后續(xù)所有的構(gòu)造任務(wù)。 但在客戶端將生成器對(duì)象傳遞給主管類制造方法時(shí)還有另一種方式。 在這種情況下, 你在使用主管類生產(chǎn)產(chǎn)品時(shí)每次都可以使用不同的生成器。
優(yōu)缺點(diǎn)
1.優(yōu)點(diǎn)
- 你可以分步創(chuàng)建對(duì)象,暫緩創(chuàng)建步驟或遞歸運(yùn)行創(chuàng)建步驟。
- 生成不同形式的產(chǎn)品時(shí),你可以復(fù)用相同的制造代碼。
- *單一職責(zé)原則:*你可以將復(fù)雜構(gòu)造代碼從產(chǎn)品的業(yè)務(wù)邏輯中分離出來。
2.缺點(diǎn)
- 由于該模式需要新增多個(gè)類,因此代碼整體復(fù)雜程度會(huì)有所增加。
使用場景
- 使用生成器模式可避免“重疊構(gòu)造函數(shù)(telescoping constructor)”的出現(xiàn)。
- 當(dāng)你希望使用代碼創(chuàng)建不同形式的產(chǎn)品(例如石頭或木頭房屋)時(shí),可使用生成器模式。
- 使用生成器構(gòu)造組合樹或其他復(fù)雜對(duì)象。
參考代碼
使用建造普通房子以及別墅來模擬
iBuilder.go: 生成器接口
package main
type IBuilder interface {
setWindowType()
setDoorType()
setNumFloor()
getHouse() House
}
func getBuilder(builderType string) IBuilder {
if builderType == "normal" {
return newNormalBuilder()
}
if builderType == "villa" {
return newVillaBuilder()
}
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,
}
}
villaBuilder.go: 別墅生成器
package main
type VillaBuilder struct {
windowType string
doorType string
floor int
}
func newVillaBuilder() *VillaBuilder {
return &VillaBuilder{}
}
func (b *VillaBuilder) setWindowType() {
b.windowType = "Snow Window"
}
func (b *VillaBuilder) setDoorType() {
b.doorType = "Snow Door"
}
func (b *VillaBuilder) setNumFloor() {
b.floor = 1
}
func (b *VillaBuilder) 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")
villaBuilder := getBuilder("villa")
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(villaBuilder)
villa := director.buildHouse()
fmt.Printf("\nIgloo House Door Type: %s\n", villa.doorType)
fmt.Printf("Igloo House Window Type: %s\n", villa.windowType)
fmt.Printf("Igloo House Num Floor: %d\n", villa.floor)
}
output:
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
到此這篇關(guān)于go 生成器模式的具體使用的文章就介紹到這了,更多相關(guān)go 生成器模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語言實(shí)現(xiàn)多協(xié)程并發(fā)下載網(wǎng)頁內(nèi)容的完整代碼
在互聯(lián)網(wǎng)項(xiàng)目中,我們常需要批量獲取多個(gè)網(wǎng)頁的內(nèi)容,如果逐個(gè)請(qǐng)求(串行),效率將非常低下,Go天生支持高并發(fā),所以本文實(shí)戰(zhàn)演示如何使用Goroutine和Channel,實(shí)現(xiàn)多協(xié)程并發(fā)抓取網(wǎng)頁內(nèi)容,提升網(wǎng)絡(luò)請(qǐng)求效率,為構(gòu)建爬蟲、內(nèi)容聚合器、API 批量采集器打下基礎(chǔ)2025-08-08
提升編程技能:學(xué)習(xí)如何在Go語言中正確格式化時(shí)間
想知道如何在Go語言中輕松地格式化時(shí)間嗎?別再浪費(fèi)時(shí)間了!本文將帶你快速入門,讓你的代碼更加優(yōu)雅高效,快來學(xué)習(xí)吧!2024-01-01
在Visual Studio Code中配置GO開發(fā)環(huán)境的詳細(xì)教程
這篇文章主要介紹了在Visual Studio Code中配置GO開發(fā)環(huán)境的詳細(xì)教程,需要的朋友可以參考下2017-02-02
Go從defer關(guān)鍵字到鎖用法實(shí)例demo
在Go語言中,defer語句用于延遲執(zhí)行一個(gè)函數(shù)或方法,直到包含它的函數(shù)執(zhí)行完畢時(shí)才執(zhí)行,這篇文章主要介紹了Go從defer關(guān)鍵字到鎖用法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2026-06-06

