package main
import "fmt"
type FinishedHouse struct {
style int //0: Chinese; 1:American; 2:European
centralAirConditioning bool // true 或 false 是否安装中央空调
floorMaterial string // 地面材料 ground-tile 或 wood
wallMaterial string // 墙面材料 paper 或 diatom-mud
}
type Option func(*FinishedHouse)
func NewFinishedHouse(options ...Option) *FinishedHouse {
h := &FinishedHouse{
style: 0,
centralAirConditioning: true,
floorMaterial: "wood",
wallMaterial: "paper",
}
for _, option := range options {
option(h)
}
return h
}
func WithStyle(style int) Option {
return func(h *FinishedHouse) {
h.style = style
}
}
func WithCentralAirConditioning(centralAirConditioning bool) Option {
return func(h *FinishedHouse) {
h.centralAirConditioning = centralAirConditioning
}
}
func WithWallMaterial(wallMaterial string) Option {
return func(h *FinishedHouse) {
h.wallMaterial = wallMaterial
}
}
func WithfloorMaterial(floorMaterial string) Option {
return func(h *FinishedHouse) {
h.floorMaterial = floorMaterial
}
}
func main() {
fmt.Printf("%+v\n", NewFinishedHouse()) // 使用默认选项
fmt.Printf("%+v\n", NewFinishedHouse(WithStyle(1), WithWallMaterial("xiaoyu")))
}
go 优雅的配置参数
最新推荐文章于 2024-02-07 16:28:51 发布