Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Go Builder pattern, [go Design pattern]

Shulou Source: shulou.com Published: 2022-06-03 03:32:08 10月02日 Update

Direct code:

Ackage mainimport "fmt" / / now there are classrooms and dormitories, with two classrooms, windows two meters high and a dormitory door. The window is one meter high type Room struct {doorNum int windowHeight int} func (r * Room) showRoomInfo () {fmt.Println ("door num:", r.doorNum, "door windowHeight:", r.windowHeight)} type RoomBuilder interface {build () * Room} func BuildRoom (builder RoomBuilder) * Room {return builder.build ()} type Cla***oomBuilder struct {} func (* Cla***oomBuilder) build () * Room {return & Room {2 2}} type DormitoryBuilder struct {} func (* DormitoryBuilder) build () * Room {return & Room {1,1}} func main () {room: = BuildRoom (& Cla***oomBuilder {}) room.showRoomInfo () room = BuildRoom (& DormitoryBuilder {}) room.showRoomInfo () / similarly, we need a living room, a door without windows. You can write a living room builder / / the example here is very simple, but in practice, the build function may deal with very complex things. Through the builder mode, you can separate the specific / / instance from the creation process, so it is easy to extend and replace the creation process} github long-term update: https://github.com/fanux/gopatterns

Tags: Windows living room dormitory classroom process pattern complexity code example function instance actual high school one processing update design pattern design Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno vpn Shulou Information Microsoft