forked from senghoo/golang-design-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 代理模式 | ||
|
||
代理模式用于延迟处理操作或者在进行实际操作前后进行其它处理。 | ||
|
||
## 代理模式的常见用法有 | ||
|
||
* 虚代理 | ||
* COW代理 | ||
* 远程代理 | ||
* 保护代理 | ||
* Cache 代理 | ||
* 防火墙代理 | ||
* 同步代理 | ||
* 智能指引 | ||
|
||
等。。。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |