概念
代理(Proxy),为其他对象提供一种代理以控制对这个对象的访问。
需求
帮助卓贾易送礼物给娇娇
UML图
代码
送礼物接口
type IGiveGift interface {
GiveDolls()
GiveFlowers()
GiveChocolate()
}
追求者类
type Pursuit struct {
Girl string
}
func (p *Pursuit) GiveDolls() {
fmt.Printf("%v 送你洋娃娃", p.Girl)
fmt.Println()
}
func (p *Pursuit) GiveFlowers() {
fmt.Printf("%v 送你鲜花", p.Girl)
fmt.Println()
}
func (p *Pursuit) GiveChocolate() {
fmt.Printf("%v 送你巧克力", p.Girl)
fmt.Println()
}
代理类
type Proxy struct {
Pursuit Pursuit
}
func (p *Proxy) GiveDolls() {
p.Pursuit.GiveDolls()
}
func (p *Proxy) GiveFlowers() {
p.Pursuit.GiveFlowers()
}
func (p *Proxy) GiveChocolate() {
p.Pursuit.GiveChocolate()
}
测试
zhuojiayi := proxyPattern.Pursuit{Girl: "娇娇"}
daili := proxyPattern.Proxy{Pursuit: zhuojiayi}
daili.GiveDolls()
daili.GiveFlowers()
daili.GiveChocolate()