《大话设计模式》代理模式

代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问。[DP]
代理模式其实就是在访问对象时引入一定程度的间接性,因为这种间接性,可以附加多种用途(远程代理、虚拟代理、安全代理、智能指引等)。
代理模式结构图

// 需要通过代理来调用的方法
type SubjectInterface interface {
    
    
	Show()
}

// 代理"类"
type Proxy struct {
    
    
	real realSubject
}

func (p *Proxy) Show() {
    
    
	// 代理操作
	fmt.Println("Here is proxy.")
	fmt.Println("Calling real subject's methods...")
	// 调用真实对象的方法
	p.real.Show()
}

// 真实对象"类"
type realSubject struct {
    
    }

func (s* realSubject) Show() {
    
    
	fmt.Println("Here is real subject.")
}

func main() {
    
    
	p := Proxy{
    
    }
	p.Show()
}

猜你喜欢

转载自blog.csdn.net/unirrrrr/article/details/104225804
今日推荐