implemented decorator pattern
This commit is contained in:
parent
f118b649ac
commit
9c06317098
2
concurrency/barrier/barrier.go
Normal file
2
concurrency/barrier/barrier.go
Normal file
@ -0,0 +1,2 @@
|
||||
package barrier
|
||||
|
31
structural/decorator/decorator.go
Normal file
31
structural/decorator/decorator.go
Normal file
@ -0,0 +1,31 @@
|
||||
package decorator
|
||||
|
||||
import (
|
||||
"os"
|
||||
"io"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// simple http server
|
||||
type SimpleServer struct{
|
||||
msg string
|
||||
}
|
||||
func (s *SimpleServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, s.msg)
|
||||
}
|
||||
|
||||
// log decorator for simple server
|
||||
type ServerWithLog struct {
|
||||
handler http.Handler
|
||||
logger io.Writer
|
||||
}
|
||||
func (s *ServerWithLog) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(s.logger, "Got request with uri: %s \n", r.RequestURI)
|
||||
s.handler.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func Demo(){
|
||||
http.Handle("/", &ServerWithLog{&SimpleServer{"Simple server"}, os.Stdout})
|
||||
http.ListenAndServe(":3000", nil)
|
||||
}
|
Reference in New Issue
Block a user