GO设计模式06建造者模式
Posted XY丶YX
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GO设计模式06建造者模式相关的知识,希望对你有一定的参考价值。
package main
import (
"fmt"
)
//产品角色
type Car struct {
Brand string
Type string
Color string
}
func (this *Car) Drive() error {
fmt.Printf("A %s %s %s car is running on the road!\\n", this.Color, this.Type, this.Brand)
return nil
}
//建造者角色
type Builder interface {
PaintColor(color string) Builder
AddBrand(brand string) Builder
SetType(t string) Builder
Build() Car
}
//具体的建造者
type ConcreteBuilder struct {
ACar Car
}
func (this *ConcreteBuilder) PaintColor(cor string) Builder {
this.ACar.Color = cor
return this
}
func (this *ConcreteBuilder) AddBrand(bnd string) Builder {
this.ACar.Brand = bnd
return this
}
func (this *ConcreteBuilder) SetType(t string) Builder {
this.ACar.Type = t
return this
}
func (this *ConcreteBuilder) Build() Car {
return this.ACar
}
//导演者角色
type Director struct {
Builder Builder
}
func main() {
fmt.Println("hello")
dr := Director{&ConcreteBuilder{}}
adCar := dr.Builder.SetType("SUV").AddBrand("奥迪").PaintColor("white").Build()
adCar.Drive()
bwCar := dr.Builder.SetType("sporting").AddBrand("宝马").PaintColor("red").Build()
bwCar.Drive()
}
以上是关于GO设计模式06建造者模式的主要内容,如果未能解决你的问题,请参考以下文章