Go中“责任链+策略”组合模式的核心是Handler委托策略处理请求并按需传递:ChainHandler持HandlerStrategy实例,调用其Handle方法,根据返回结果决定是否终止或继续调用next。

用 Go 实现“责任链 + 策略”组合模式,核心是让每个处理器(Handler)既能按顺序传递请求(责任链),又能根据请求类型动态选择具体处理逻辑(策略)。这不是简单叠加两个模式,而是用策略解耦“怎么处理”,用责任链控制“谁来处理”。关键在于:处理器本身不硬编码业务逻辑,而是委托给可插拔的策略对象。
定义统一请求与策略接口
先抽象出请求数据结构和策略行为契约,这是组合的基础:
// 请求上下文,可携带任意字段
type Request struct {
Type string
Data map[string]interface{}
Error error
}
// 策略接口:具体处理逻辑由实现者提供
type HandlerStrategy interface {
Handle(req *Request) *Request
}
// 可选:带初始化能力的策略(如需依赖注入)
type InitializableStrategy interface {
HandlerStrategy
Init(config map[string]interface{}) error
}
构建可插拔的责任链节点
每个链节点(Handler)持有策略实例,并决定是否继续传递。它不关心策略内部怎么处理,只负责调用并判断是否中断链:
type ChainHandler struct {
strategy HandlerStrategy
next *ChainHandler
}
func NewChainHandler(s HandlerStrategy) *ChainHandler {
return &ChainHandler{strategy: s}
}
func (h *ChainHandler) SetNext(next *ChainHandler) {
h.next = next
}
func (h *ChainHandler) Handle(req *Request) *Request {
// 策略执行前可加通用逻辑(日志、校验等)
if req.Error != nil {
return req
}
// 委托给策略处理
result := h.strategy.Handle(req)
if result.Error != nil || h.shouldStop(result) {
return result
}
// 继续传递(若存在下一节点)
if h.next != nil {
return h.next.Handle(result)
}
return result
}
// 可扩展:按返回值或请求状态决定是否终止链
func (h *ChainHandler) shouldStop(req *Request) bool {
// 例如:策略显式返回 stop 标志,或 type 变更为 "done"
if flag, ok := req.Data["stop_chain"]; ok && flag == true {
return true
}
return false
}
实现具体策略并组装链
策略实现完全独立,便于单元测试和替换。链的组装发生在运行时,支持条件化插入:
立即学习“go语言免费学习笔记(深入)”;
- 写多个策略,比如
AuthStrategy、RateLimitStrategy、DBSaveStrategy - 每个策略只专注一件事,不耦合其他环节
- 用工厂或配置驱动创建策略实例,避免 new 硬编码
示例策略:
type AuthStrategy struct {
userDB *UserDB // 依赖可注入
}
func (a *AuthStrategy) Handle(req *Request) *Request {
token := req.Data["token"].(string)
if !a.userDB.Validate(token) {
req.Error = fmt.Errorf("unauthorized")
req.Data["stop_chain"] = true
}
return req
}
// 组装链(可读配置、DI 容器或代码硬编排)
auth := &AuthStrategy{userDB: newUserDB()}
rate := &RateLimitStrategy{limiter: newLimiter()}
save := &DBSaveStrategy{db: newDB()}
chain := NewChainHandler(auth)
chain.SetNext(NewChainHandler(rate)).SetNext(NewChainHandler(save))
// 使用
req := &Request{
Type: "order_create",
Data: map[string]interface{}{"token": "abc123", "order": "..."},
}
resp := chain.Handle(req)
增强灵活性:运行时策略路由
在责任链入口处加一层策略路由,根据请求类型分发到不同子链,进一步解耦:
type RouterChain struct {
chains map[string]*ChainHandler // type → 子链
}
func (r *RouterChain) Handle(req *Request) *Request {
handler, ok := r.chains[req.Type]
if !ok {
req.Error = fmt.Errorf("no handler for type: %s", req.Type)
return req
}
return handler.Handle(req)
}
// 使用:为不同业务类型挂不同责任链
router := &RouterChain{
chains: map[string]*ChainHandler{
"payment": NewChainHandler(&PayAuth{}).SetNext(NewChainHandler(&PayLog{})),
"notify": NewChainHandler(&NotifyRetry{}).SetNext(NewChainHandler(&NotifyPush{})),
},
}
这种组合让系统既保持流程可控(链式顺序),又具备高度可扩展性(策略即插即用),还利于测试和演进——改策略不影响链结构,增节点不改策略逻辑。










