From 9c063170984f1b026753b12cc238615ba5fd52bd Mon Sep 17 00:00:00 2001 From: ismayilmalik Date: Tue, 23 Jan 2018 23:12:14 +0300 Subject: [PATCH] implemented decorator pattern --- concurrency/barrier/barrier.go | 2 ++ structural/decorator/decorator.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 concurrency/barrier/barrier.go create mode 100644 structural/decorator/decorator.go diff --git a/concurrency/barrier/barrier.go b/concurrency/barrier/barrier.go new file mode 100644 index 0000000..dd1f6a5 --- /dev/null +++ b/concurrency/barrier/barrier.go @@ -0,0 +1,2 @@ +package barrier + diff --git a/structural/decorator/decorator.go b/structural/decorator/decorator.go new file mode 100644 index 0000000..d85b3ac --- /dev/null +++ b/structural/decorator/decorator.go @@ -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) +}