-
Notifications
You must be signed in to change notification settings - Fork 0
/
dec_pattern.go
60 lines (42 loc) · 957 Bytes
/
dec_pattern.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"net/http"
)
// DECORATOR Pattern
func makeHTTPFunc(db DB, fn httpFunc) http.HandlerFunc {
return func (w http.ResponseWriter, r *http.Request) {
if err := fn(db, w, r); err != nil {
// do something with the error
}
}
}
func Decmain() {
s := &store{}
http.HandleFunc("/", makeHTTPFunc(s, handler))
Execute(myExecuteFunc(s))
}
type DB interface {
Store(string) error
}
type store struct {}
func (s *store) Store(str string) error {
fmt.Println("storing into db", str)
return nil
}
func handler(db DB, w http.ResponseWriter, r *http.Request) error {
return nil
}
type httpFunc func(db DB, w http.ResponseWriter, r *http.Request) error
// this is coming from a third party library
type ExecuteFn func(string)
func myExecuteFunc (db DB) ExecuteFn{
// access to db
return func (s string) {
fmt.Println("my ex functoin")
db.Store(s)
}
}
func Execute(fn ExecuteFn) {
fn("fooBAAR")
}