Golang 设计模式-代理模式

代理模式,简单来说就是提供一个对象来控制其他对象的功能。在一些情况下,一个Object不适合直接引用目标对象,但可以通过代理对象调用目标对象,起到中介代理的作用。

Golang 设计模式-代理模式

一、被代理的公共函数(中介是干什么的)

type ProxyFuncs interface {

//准备代理卖房

SailHouse()

}

二、业主要卖房

type MasterBeijing struct {

Name string //北京业主姓名

Location string //业主所卖房屋的位置

}

func (this *MasterBeijing) SailHouse() {

fmt.Printf("%s sailing house at %s\n", this.Name, this.Location)

}

三、代理业主操作卖房

type Proxier struct {

Mofbj *MasterBeijing

}

func (this *Proxier) SailHouse() {

if this.Mofbj == nil {

this.Mofbj = &MasterBeijing{}

}

this.Mofbj.SailHouse()

}

示例:中介代理业主卖方

m := &MasterBeijing{

Name: "Lao wang",

Location: "Xi Cheng",

}

proxier := &Proxier{

Mofbj: m,

}

proxier.SailHouse()

更多内容请关注每日编程,每天进步一点。


分享到:


相關文章: