代理模式Golang

代理模式

代理模式(Proxy Pattern): 给某一个对象提供一个代理, 并有代理对象控制原来对象的引用.

优点和缺点

代理模式能够协调调用者和被调用者, 在一定程度上降低系统的耦合度.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

type IObject interface {
ObjDo(action string)
}

type Object struct {
action string
}

func (obj *Object) ObjDo(action string) {
fmt.Printf("I can %s", action)
}

type ProxyObject struct {
object *Object
}

func (p *ProxyObject) ObjDo(action string) {
if p.object == nil {
p.object = new(Object)
}
if action == "run" {
p.object.ObjDo(action)
}
}

Powered by Hexo and Hexo-theme-hiker

Copyright © 2018 - 2021 Noonde All Rights Reserved.

UV : | PV :