Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Go Builder pattern, [go Design pattern]

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report