golang设计模式-装饰器模式

装饰器模式

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

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

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

代码示例:

go 复制代码
package main

import "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!"))
}

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

go 复制代码
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))
}

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

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

go 复制代码
package main

import (
	"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!"))
}

或者

go 复制代码
package main

import (
	"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!"))
}
相关推荐
Starwow10 分钟前
微服务之gRPC
后端·微服务·golang
Chandler242 小时前
Go:低级编程
开发语言·后端·golang
gospace4 小时前
Golang 事务消息队列:基于 SQLite 的轻量级消息队列解决方案
golang·sqlite
程序员JerrySUN5 小时前
设计模式每日硬核训练 Day 14:组合模式(Composite Pattern)完整讲解与实战应用
设计模式·组合模式
碎梦归途5 小时前
23种设计模式-创建型模式之工厂方法模式(Java版本)
java·设计模式·工厂方法模式
XU磊2605 小时前
Java 工厂设计模式详解:用统一入口打造灵活可扩展的登录系统----掌握 Spring 源码的基础第一步
java·设计模式
匹马夕阳6 小时前
java开发中的设计模式之工厂模式
java·设计模式
Pasregret6 小时前
设计模式入门:从 GoF 分类到 SOLID 原则实战
java·设计模式
Light608 小时前
Python依赖注入完全指南:高效解耦、技术深析与实践落地
python·设计模式·单元测试·fastapi·依赖注入·解耦
都叫我大帅哥9 小时前
代码界的「俄罗斯套娃」:组合模式的嵌套艺术
java·后端·设计模式