欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > golang设计模式-装饰器模式

golang设计模式-装饰器模式

2025/5/11 16:55:26 来源:https://blog.csdn.net/qq_43000128/article/details/146128876  浏览:    关键词:golang设计模式-装饰器模式

装饰器模式

装饰器模式是一种结构型设计模式,它允许在运行时动态地添加对象的新行为。这种模式通过将对象包装在装饰器类的对象中来实现。

装饰器模式通常用于以下几种情况:

  • 当你需要在不修改现有对象代码的情况下扩展对象的行为时。装饰器模式提供了一种灵活的方法来混合和匹配新行为,而不需要创建大量的子类。
  • 当你需要在运行时动态地添加或删除对象的行为时。由于装饰器模式允许你在运行时包装和解包装对象,所以你可以在运行时改变对象的行为。
  • 当你需要将一些行为应用于多个独立的对象时。使用装饰器模式,你可以定义一个通用的装饰器类,并将其应用于多个不同的对象,而不需要为每个对象都定义一个新的子类。

代码示例:

package mainimport "net/http"func main() {mux := http.NewServeMux()mux.HandleFunc("GET /hello", HelloWorld)mux.HandleFunc("GET /how", HowAreYou)srv := http.Server{Addr:    ":8080",Handler: mux,}srv.ListenAndServe()
}func HowAreYou(w http.ResponseWriter, r *http.Request) {w.WriteHeader(http.StatusOK)w.Write([]byte("I am fine"))
}func HelloWorld(w http.ResponseWriter, r *http.Request) {w.WriteHeader(http.StatusOK)w.Write([]byte("hello world!"))
}

如果我要记录请求的时间和请求日志,可能会这么写:

func HowAreYou(w http.ResponseWriter, r *http.Request) {now := time.Now()w.WriteHeader(http.StatusOK)w.Write([]byte("I am fine"))log.Printf("url: %s, elase: %v", r.URL, time.Since(now))
}func HelloWorld(w http.ResponseWriter, r *http.Request) {now := time.Now()w.WriteHeader(http.StatusOK)w.Write([]byte("hello world!"))log.Printf("url: %s, elapsed: %v", r.URL, time.Since(now))
}

但是如果方法很多的话这么写太浪费时间了

这时候就可以使用装饰器模式

package mainimport ("log""net/http""time"
)func main() {mux := http.NewServeMux()mux.HandleFunc("GET /hello", Logger(HelloWorld))mux.HandleFunc("GET /how", Logger(HowAreYou))srv := http.Server{Addr:    ":8080",Handler: mux,}srv.ListenAndServe()
}type Handler func(w http.ResponseWriter, r *http.Request)func Logger(handler Handler) Handler {return func(w http.ResponseWriter, r *http.Request) {now := time.Now()handler(w, r)log.Printf("url: %s, elase: %v", r.URL, time.Since(now))}
}
func HowAreYou(w http.ResponseWriter, r *http.Request) {w.WriteHeader(http.StatusOK)w.Write([]byte("I am fine"))
}func HelloWorld(w http.ResponseWriter, r *http.Request) {w.WriteHeader(http.StatusOK)w.Write([]byte("hello world!"))
}

或者

package mainimport ("log""net/http""time"
)func main() {mux := http.NewServeMux()mux.HandleFunc("GET /hello", HelloWorld)mux.HandleFunc("GET /how", HowAreYou)srv := http.Server{Addr:    ":8080",Handler: Logger(mux),}srv.ListenAndServe()
}func Logger(next http.Handler) http.Handler {fn := func(w http.ResponseWriter, r *http.Request) {now := time.Now()next.ServeHTTP(w, r)log.Printf("url: %s, elase: %v", r.URL, time.Since(now))}return http.HandlerFunc(fn)
}
func HowAreYou(w http.ResponseWriter, r *http.Request) {w.WriteHeader(http.StatusOK)w.Write([]byte("I am fine"))
}func HelloWorld(w http.ResponseWriter, r *http.Request) {w.WriteHeader(http.StatusOK)w.Write([]byte("hello world!"))
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词