mirror of
https://github.com/Estom/notes.git
synced 2026-02-07 04:23:55 +08:00
go知识重新整理
This commit is contained in:
40
Go/DesignPattern/proxy/proxy.go
Normal file
40
Go/DesignPattern/proxy/proxy.go
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* @Author:zhoutao
|
||||
* @Date:2020/12/11 下午2:03
|
||||
* @Desc:
|
||||
*/
|
||||
|
||||
package proxy
|
||||
|
||||
//代理模式:用于延迟处理操作或者进行实际操作前后进行其他处理
|
||||
//代理模式的常见用法:虚代理、COW代理、远程代理、保护代理、Cache代理、防火墙代理、同步代理、智能指引等
|
||||
|
||||
type Subject interface {
|
||||
Do() string
|
||||
}
|
||||
|
||||
type RealSubject struct {
|
||||
}
|
||||
|
||||
func (RealSubject) Do() string {
|
||||
return "going to do something"
|
||||
}
|
||||
|
||||
type Proxy struct {
|
||||
real RealSubject
|
||||
}
|
||||
|
||||
func (p Proxy) Do() string {
|
||||
var res string
|
||||
|
||||
//在调用想要做的工作之前,做一些事情,如检查缓存、判断权限、实例化真实对象等
|
||||
res += "pre to do :"
|
||||
|
||||
//实际要做的事情
|
||||
res += p.real.Do()
|
||||
|
||||
//调用之后执行的操作,如缓存结果,对结果进行处理等
|
||||
res += ": after to do"
|
||||
|
||||
return res
|
||||
}
|
||||
20
Go/DesignPattern/proxy/proxy_test.go
Normal file
20
Go/DesignPattern/proxy/proxy_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @Author:zhoutao
|
||||
* @Date:2020/12/11 下午2:13
|
||||
* @Desc:
|
||||
*/
|
||||
|
||||
package proxy
|
||||
|
||||
import "testing"
|
||||
|
||||
var expect = "pre to do :going to do something: after to do"
|
||||
|
||||
func TestProxy(t *testing.T) {
|
||||
var sub Subject
|
||||
sub = &Proxy{}
|
||||
res := sub.Do()
|
||||
if res != expect {
|
||||
t.Fatalf("expect %s ,return %s", expect, res)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user