SoFunction
Updated on 2025-03-05

Golang design pattern generator pattern explanation and code example

Go GeneratorPattern explanation and code examples

Concept example

Generator mode can also be used when the required product is complex and requires multiple steps to complete. In this case, using multiple constructors is easier than just using one complex and terrifying constructor. The potential problem with building in multiple steps is that building incomplete and unstable products can be exposed to the client. Generator mode enables the product to be private before it is built.

In the code below, we can seeigloo­BuilderIcehouse generator andnormal­BuilderThe ordinary house generator can build different types of houses, i.e.iglooIcehouse andnormal­HouseOrdinary house. The construction steps for each house type are the same. Supervisor (Optional) The structure can organize the construction process.

:Generator interface

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
}

:Specific generator

package main
type NormalBuilder struct {
    windowType string
    doorType   string
    floor      int
}
func newNormalBuilder() *NormalBuilder {
    return &NormalBuilder{}
}
func (b *NormalBuilder) setWindowType() {
     = "Wooden Window"
}
func (b *NormalBuilder) setDoorType() {
     = "Wooden Door"
}
func (b *NormalBuilder) setNumFloor() {
     = 2
}
func (b *NormalBuilder) getHouse() House {
    return House{
        doorType:   ,
        windowType: ,
        floor:      ,
    }
}

:Specific generator

package main
type IglooBuilder struct {
    windowType string
    doorType   string
    floor      int
}
func newIglooBuilder() *IglooBuilder {
    return &IglooBuilder{}
}
func (b *IglooBuilder) setWindowType() {
     = "Snow Window"
}
func (b *IglooBuilder) setDoorType() {
     = "Snow Door"
}
func (b *IglooBuilder) setNumFloor() {
     = 1
}
func (b *IglooBuilder) getHouse() House {
    return House{
        doorType:   ,
        windowType: ,
        floor:      ,
    }
}

:product

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

:director

package main
type Director struct {
    builder IBuilder
}
func newDirector(b IBuilder) *Director {
    return &Director{
        builder: b,
    }
}
func (d *Director) setBuilder(b IBuilder) {
     = b
}
func (d *Director) buildHouse() House {
    ()
    ()
    ()
    return ()
}

:Client code

package main
import "fmt"
func main() {
    normalBuilder := getBuilder("normal")
    iglooBuilder := getBuilder("igloo")
    director := newDirector(normalBuilder)
    normalHouse := ()
    ("Normal House Door Type: %s\n", )
    ("Normal House Window Type: %s\n", )
    ("Normal House Num Floor: %d\n", )
    (iglooBuilder)
    iglooHouse := ()
    ("\nIgloo House Door Type: %s\n", )
    ("Igloo House Window Type: %s\n", )
    ("Igloo House Num Floor: %d\n", )
}

:Execution results

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

The above is the detailed content of the generator mode explanation and code example of Golang design pattern. For more information about the Golang generator mode, please pay attention to my other related articles!