Go语言通过高阶函数和接口实现装饰器模式,如日志、HTTP中间件和缓存扩展,动态增强函数或结构体方法功能,符合开闭原则。

在 Go 语言中,虽然没有像 Python 那样原生支持装饰器语法(如 @decorator),但通过函数式编程和高阶函数的特性,我们可以实现类似 Decorator 模式 的功能扩展。这种方式既可用于结构体方法的增强,也可用于 HTTP 中间件、日志记录、权限校验等场景。
装饰器模式是一种结构设计模式,允许在不修改原始对象代码的前提下,动态地为对象添加新功能。它通过“包装”原有对象来实现功能叠加,符合开闭原则(对扩展开放,对修改封闭)。
在 Go 中,我们通常使用函数包装的方式来模拟装饰器行为,尤其是在处理函数类型时非常自然。
假设我们有一个处理字符串的函数,希望在执行前后打印日志:
立即学习“go语言免费学习笔记(深入)”;
func processMessage(msg string) {
fmt.Println("Processing:", msg)
}
// 日志装饰器
func withLogging(fn func(string)) func(string) {
return func(msg string) {
fmt.Printf("Before: Calling function with %s\n", msg)
fn(msg)
fmt.Printf("After: Finished processing %s\n", msg)
}
}
// 使用方式
func main() {
decorated := withLogging(processMessage)
decorated("Hello, World!")
}
输出结果:
Before: Calling function with Hello, World! Processing: Hello, World! After: Finished processing Hello, World!
这里 withLogging 就是一个装饰器函数,它接收一个函数并返回一个增强了日志能力的新函数。
Go 的 net/http 包广泛使用装饰器思想来构建中间件。比如身份验证、日志、CORS 等都可以用装饰器链实现。
func loggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("[%s] %s\n", r.Method, r.URL.Path)
next(w, r)
}
}
func authMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next(w, r)
}
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, authenticated user!")
}
func main() {
mux := http.NewServeMux()
// 应用多个装饰器(中间件)
handler := loggingMiddleware(authMiddleware(helloHandler))
mux.HandleFunc("/hello", handler)
http.ListenAndServe(":8080", mux)
}
请求经过 loggingMiddleware 和 authMiddleware 层层包装,形成责任链。这种写法清晰且可复用。
对于结构体方法,也可以通过接口和包装结构体实现装饰效果。
type Service interface {
FetchData(id int) string
}
type RealService struct{}
func (s *RealService) FetchData(id int) string {
return fmt.Sprintf("Data for ID: %d", id)
}
// 缓存装饰器
type CachedService struct {
service Service
cache map[int]string
}
func NewCachedService(s Service) *CachedService {
return &CachedService{
service: s,
cache: make(map[int]string),
}
}
func (c *CachedService) FetchData(id int) string {
if data, ok := c.cache[id]; ok {
fmt.Println("Cache hit!")
return data
}
fmt.Println("Cache miss, fetching from real service...")
data := c.service.FetchData(id)
c.cache[id] = data
return data
}
// 使用示例
func main() {
real := &RealService{}
cached := NewCachedService(real)
fmt.Println(cached.FetchData(1)) // Cache miss
fmt.Println(cached.FetchData(1)) // Cache hit
}
这个例子展示了如何通过组合模式包装原始服务,在不改变其接口的情况下增加缓存功能。
基本上就这些。Go 虽然没有装饰器语法糖,但凭借其简洁的函数类型和接口机制,完全可以实现强大而灵活的装饰模式。关键在于理解“包装”与“委托”的思想,并合理运用高阶函数或结构体嵌套。
以上就是Golang Decorator功能扩展与装饰模式示例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号