Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2e5f90b

Browse files
committedMar 25, 2016
add proxy mode
1 parent 16c3d90 commit 2e5f90b

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
 

‎09_proxy/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 代理模式
2+
3+
代理模式用于延迟处理操作或者在进行实际操作前后进行其它处理。
4+
5+
## 代理模式的常见用法有
6+
7+
* 虚代理
8+
* COW代理
9+
* 远程代理
10+
* 保护代理
11+
* Cache 代理
12+
* 防火墙代理
13+
* 同步代理
14+
* 智能指引
15+
16+
等。。。

‎09_proxy/proxy.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package proxy
2+
3+
type Subject interface {
4+
Do() string
5+
}
6+
7+
type RealSubject struct{}
8+
9+
func (RealSubject) Do() string {
10+
return "real"
11+
}
12+
13+
type Proxy struct {
14+
real RealSubject
15+
}
16+
17+
func (p Proxy) Do() string {
18+
var res string
19+
20+
// 在调用真实对象之前的工作,检查缓存,判断权限,实例化真实对象等。。
21+
res += "pre:"
22+
23+
// 调用真实对象
24+
res += p.real.Do()
25+
26+
// 调用之后的操作,如缓存结果,对结果进行处理等。。
27+
res += ":after"
28+
29+
return res
30+
}

‎09_proxy/proxy_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package proxy
2+
3+
import "testing"
4+
5+
func TestProxy(t *testing.T) {
6+
var sub Subject
7+
sub = &Proxy{}
8+
9+
res := sub.Do()
10+
11+
if res != "pre:real:after" {
12+
t.Fail()
13+
}
14+
}

0 commit comments

Comments
 (0)
Failed to load comments.