关于golang:手撸golang-行为型设计模式-委派模式

2次阅读

共计 4434 个字符,预计需要花费 12 分钟才能阅读完成。

手撸 golang 行为型设计模式 委派模式

缘起

最近温习设计模式
拜读谭勇德的 << 设计模式就该这样学 >>
本系列笔记拟采纳 golang 练习之

委派模式

委派模式(Delegate Pattern)又叫作委托模式,根本作用就是负责工作的调用和调配,是一种非凡的动态代理模式,能够了解为全权代理模式,然而代理模式重视过程,而委派模式重视后果。
委派模式有 3 个参加角色。
(1)形象工作角色(ITask):定义一个形象接口,它有若干实现类。
(2)委派者角色(Delegate):负责在各个具体角色实例之间做出决策,判断并调用具体实现的办法。
(3)具体任务角色(Concrete):真正执行工作的角色。
_

场景

  • 某音讯解决零碎, 须要解决客户端申请的各种音讯
  • 为不便后续扩大对立的消息日志 / 审计 / 权限 / 平安等性能, 依据 委派模式, 所有音讯由全局调度器对立调度
  • 调度器依据音讯的类型, 委派给具体的音讯处理器

设计

  • IMsg: 定义音讯接口
  • BaseMsg: 音讯的基类, 实现 IMsg 接口
  • EchoMsg: 示意原样返回的音讯, 用于 PING/PONG 心跳. 继承自 BaseMsg
  • TimeMsg: 示意获取服务器工夫的音讯. 继承自 BaseMsg
  • IMsgHandler: 音讯处理器接口. 调度器和具体音讯处理器, 均须要实现此接口.
  • tMsgDispatchDelegate: 全局音讯调度器, 是所有客户端音讯的对立入口. 用于注册音讯处理器, 按类型散发音讯.
  • tEchoMsgHandler: 专门解决 EchoMsg 音讯的处理器. 实现 IMsgHandler 接口.
  • tTimeMsgHandler: 专门解决 TimeMsg 音讯的处理器, 实现 IMsgHandler 接口.

单元测试

delegate_pattern_test.go

package behavioral_patterns_test

import (
    "fmt"
    "learning/gooop/behavioral_patterns/delegate"
    "testing"
)

func Test_DelegatePattern(t *testing.T) {
    dispatcher := delegate.GlobalMsgDispatcher
    vEchoMsg := delegate.NewEchoMsg("msg-1", "this is an echo msg")
    response := dispatcher.Handle(vEchoMsg)
    fmt.Printf("echo response: id=%v, cls=%v, content=%v\n", response.ID(), response.Class(), response.Content())

    vTimeMsg := delegate.NewTimeMsg("msg-2")
    response = dispatcher.Handle(vTimeMsg)
    fmt.Printf("time response: id=%v, cls=%v, content=%v\n", response.ID(), response.Class(), response.Content())
}

测试输入

$ go test -v delegate_pattern_test.go 
=== RUN   Test_DelegatePattern
tMsgDispatchDelegate.Handle, handler=*delegate.tEchoMsgHandler, id=msg-1, cls=EchoMsg
  tEchoMsgHandler.Handle, id=msg-1, cls=EchoMsg
  echo response: id=msg-1, cls=EchoMsg, content=this is an echo msg
tMsgDispatchDelegate.Handle, handler=*delegate.tTimeMsgHandler, id=msg-2, cls=TimeMsg
  tTimeMsgHandler.Handle, id=msg-2, cls=TimeMsg
  time response: id=msg-2, cls=TimeMsg, content=2021-02-05T09:18:45
--- PASS: Test_DelegatePattern (0.00s)
PASS
ok      command-line-arguments  0.002s

IMsg.go

定义音讯接口

package delegate

type IMsg interface {ID() string
    Class() string
    Content() string}

BaseMsg.go

音讯的基类, 实现 IMsg 接口

package delegate

type BaseMsg struct {
    sID string
    sClass string
    sContent string
}

func NewBaseMsg(id string, cls string, content string) *BaseMsg {
    return &BaseMsg{id, cls, content,}
}

func (me *BaseMsg) ID() string {return me.sID}

func (me *BaseMsg) Class() string {return me.sClass}

func (me *BaseMsg) Content() string {return me.sContent}

EchoMsg.go

示意原样返回的音讯, 用于 PING/PONG 心跳. 继承自 BaseMsg

package delegate

type EchoMsg struct {BaseMsg}

func NewEchoMsg(id string, content string) *EchoMsg {
    return &EchoMsg{*NewBaseMsg(id, "EchoMsg", content),
    }
}

TimeMsg.go

示意获取服务器工夫的音讯. 继承自 BaseMsg

package delegate

import "time"

type TimeMsg struct {BaseMsg}

func NewTimeMsg(id string) *TimeMsg {
    return &TimeMsg{*NewBaseMsg(id, "TimeMsg", time.Now().Format("2006-01-02T15:04:05")),
    }
}

IMsgHandler.go

音讯处理器接口. 调度器和具体音讯处理器, 均须要实现此接口.

package delegate

type IMsgHandler interface {Handle(request IMsg) IMsg
}

tMsgDispatchDelegate.go

全局音讯调度器, 是所有客户端音讯的对立入口. 用于注册音讯处理器, 按类型散发音讯. 实现 IMsgHandler 接口.

package delegate

import (
    "fmt"
    "reflect"
)

type tMsgDispatchDelegate struct {mSubHandlers map[string]IMsgHandler
}

func (me *tMsgDispatchDelegate) Register(cls string, handler IMsgHandler) {me.mSubHandlers[cls] = handler
}

func newMsgDispatchDelegate() IMsgHandler {
    it := &tMsgDispatchDelegate{mSubHandlers: make(map[string]IMsgHandler, 16),
    }

    it.Register("EchoMsg", newEchoMsgHandler())
    it.Register("TimeMsg", newTimeMsgHandler())

    return it
}


func (me *tMsgDispatchDelegate) Handle(request IMsg) IMsg {
    if request == nil {return nil}

    handler, ok := me.mSubHandlers[request.Class()]
    if !ok {fmt.Printf("tMsgDispatchDelegate.Handle, handler not found: id=%v, cls=%v\n", request.ID(), request.Class())
        return nil
    }

    fmt.Printf("tMsgDispatchDelegate.Handle, handler=%v, id=%v, cls=%v\n", reflect.TypeOf(handler).String(), request.ID(), request.Class())
    return handler.Handle(request)
}

var GlobalMsgDispatcher = newMsgDispatchDelegate()

tEchoMsgHandler.go

专门解决 EchoMsg 音讯的处理器. 实现 IMsgHandler 接口.

package delegate

import "fmt"

type tEchoMsgHandler struct {
}

func newEchoMsgHandler() IMsgHandler {return &tEchoMsgHandler{}
}

func (me *tEchoMsgHandler) Handle(request IMsg) IMsg {fmt.Printf("tEchoMsgHandler.Handle, id=%v, cls=%v\n", request.ID(), request.Class())
    return request
}

tTimeMsgHandler.go

专门解决 TimeMsg 音讯的处理器, 实现 IMsgHandler 接口.

package delegate

import (
    "fmt"
    "time"
)

type tTimeMsgHandler struct {
}

func newTimeMsgHandler() IMsgHandler {return &tTimeMsgHandler{}
}

func (me *tTimeMsgHandler) Handle(request IMsg) IMsg {fmt.Printf("tTimeMsgHandler.Handle, id=%v, cls=%v\n", request.ID(), request.Class())

    timeMsg := request.(*TimeMsg)
    timeMsg.sContent = time.Now().Format("2006-01-02T15:04:05")
    return timeMsg
}

委派模式小结

委派模式的长处
通过工作委派可能将一个大型工作细化,而后通过对立治理这些子工作的实现状况实现工作的跟进,放慢工作执行的效率。
委派模式的毛病
工作委派形式须要依据工作的复杂程度进行不同的扭转,在工作比较复杂的状况下,可能须要进行多重委派,容易造成错乱。
_
(end)

正文完
 0