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!"))
}
相关推荐
workflower19 小时前
例二:某汽车制造企业深度融合AI智能体,构建园区网络的智能运维体系
人工智能·机器学习·设计模式·机器人·云计算·汽车·制造
圣殿骑士-Khtangc20 小时前
Go错误处理最佳实践进阶从error到panic的完整指南
golang
ly768920 小时前
Java 设计模式详解:从原则到 23 种经典模式
java·开发语言·设计模式
运维开发笔记21 小时前
3.6 Go defer 语句学习笔记
golang
FfHUCisI21 小时前
Golang HTTP 路由设计与请求处理
开发语言·http·golang
Mr. zhihao1 天前
深度解析:为什么Java序列化需要搭配ByteArrayOutputStream?IO装饰器模式的精妙设计
java·开发语言·装饰器模式
存在morning1 天前
【Python 开发实践 一】Python vs Go vs Java 三门语言对比
java·python·golang
AI人工智能+电脑小能手1 天前
大白话说Java设计模式-14-适配器模式(业务实战篇)
java·设计模式·适配器模式·系统兼容·多渠道对接
略略略咯咯1 天前
(总结)设计模式
设计模式
圣殿骑士-Khtangc1 天前
Go字符串高效拼接性能对比与底层原理分析
服务器·前端·golang