Skip to content

Commit

Permalink
add proxy mode
Browse files Browse the repository at this point in the history
  • Loading branch information
senghoo committed Mar 25, 2016
1 parent 16c3d90 commit 2e5f90b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 09_proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 代理模式

代理模式用于延迟处理操作或者在进行实际操作前后进行其它处理。

## 代理模式的常见用法有

* 虚代理
* COW代理
* 远程代理
* 保护代理
* Cache 代理
* 防火墙代理
* 同步代理
* 智能指引

等。。。
30 changes: 30 additions & 0 deletions 09_proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package proxy

type Subject interface {
Do() string
}

type RealSubject struct{}

func (RealSubject) Do() string {
return "real"
}

type Proxy struct {
real RealSubject
}

func (p Proxy) Do() string {
var res string

// 在调用真实对象之前的工作,检查缓存,判断权限,实例化真实对象等。。
res += "pre:"

// 调用真实对象
res += p.real.Do()

// 调用之后的操作,如缓存结果,对结果进行处理等。。
res += ":after"

return res
}
14 changes: 14 additions & 0 deletions 09_proxy/proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package proxy

import "testing"

func TestProxy(t *testing.T) {
var sub Subject
sub = &Proxy{}

res := sub.Do()

if res != "pre:real:after" {
t.Fail()
}
}

0 comments on commit 2e5f90b

Please sign in to comment.