mirror of
https://github.com/tmrts/go-patterns.git
synced 2025-04-08 19:42:01 +00:00
Added comments in example proxy also added link to go play sandbox
This commit is contained in:
parent
b5702f7953
commit
b4d87800a2
1 changed files with 8 additions and 8 deletions
|
@ -8,37 +8,37 @@ The proxy could interface to anything: a network connection, a large object in m
|
|||
|
||||
Short idea of implementation:
|
||||
```go
|
||||
// To use proxy and to object they must implement same methods
|
||||
type IObject interface {
|
||||
ObjDo(action string)
|
||||
}
|
||||
|
||||
// Object represents real objects which proxy will delegate data
|
||||
type Object struct {
|
||||
action string
|
||||
}
|
||||
|
||||
// ObjDo implements IObject interface and handels all logic
|
||||
func (obj *Object) ObjDo(action string) {
|
||||
// Action handler
|
||||
// Action behaiver
|
||||
fmt.Printf("I can, %s", action)
|
||||
}
|
||||
|
||||
// ProxyObject represents proxy object with intercepts actions
|
||||
type ProxyObject struct {
|
||||
object *Object
|
||||
}
|
||||
|
||||
// ObjDo are implemented IObject and intercept action before send in real Object
|
||||
func (p *ProxyObject) ObjDo(action string) {
|
||||
if p.object == nil {
|
||||
p.object = new(Object)
|
||||
}
|
||||
if action == "Run" {
|
||||
p.object.ObjDo(action)
|
||||
p.object.ObjDo(action) // Prints: I can, Run
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
newObj := new(ProxyObject)
|
||||
newObj.ObjDo("Run") // Prints: I can, Run
|
||||
}
|
||||
```
|
||||
|
||||
## Usage
|
||||
For usage, see [proxy/main.go](proxy/main.go) or [view in the Playground]().
|
||||
For more complex usage, see [proxy/main.go](proxy/main.go) or [view in the Playground](https://play.golang.org/p/VswGMpz-gp).
|
||||
|
|
Loading…
Add table
Reference in a new issue