【寒江雪】Go实现命令模式

Command Pattern

  命令模式将命令包裹在对象中,并传给调用对象。调用对象寻找处理该命令的合适对象,并把该命令传给合适的对象,该对象执行命令。

例子

  举一个例子,在go里边调用C语言的代码,来监听键盘的输入,并调用与输入绑定的函数。

  定义事件码和事件结构。

const (
	EVENT_CODE_KEY = iota
)

type EventCode rune
type EventData interface{}
type Event struct{
	Code EventCode
	Data EventData
}

  为了简单,只写一个按键的事件。

  定义一个抽象的接口。

type Command interface {
	Execute(EventData)
}

  这个接口定义了命令的形式。

  我们想让用户可以在外部传入回调函数,那么就需要定义一个接收事件的回调函数。

type EventFunc func(Event)

  有了回调函数形式,我们还需要一个对象,包含这种函数。这种函数不是Command接口对象。但我们可以把它包装成Command对象,这里就用到了适配器模式。

type EventFunder struct {
	f EventFunc
}

func (this EventFunder) Execute(data Event) {
	this.f(data)
}

func eventFunder(eventFunc EventFunc) EventFunder {
	return EventFunder{eventFunc}
}

  eventFunder就是把一个EventFunc类型的对象转换为EventFunder,EventFunder实现了Command接口,它属于Command。

  最后是EventSystem的实现。

type Hash map[EventCode]Command

type EventSystem struct {
	hash Hash
}

func (this *EventSystem) Init() *EventSystem {
	this.hash = make(Hash)
	return this
}

func (this *EventSystem) Map(code EventCode, eventFunc EventFunc) {
	this.hash[code] = eventFunder(eventFunc)
}

func (this *EventSystem) InspectKeyboard() {
	go func() {
		C.init_keyboard()
		for {
			if C.kbhit() > 0 {
				ch := EventCode(C.readch())
				// TODO::generate key event
				this.generateEvent(Event{EVENT_CODE_KEY, ch})
			}
		}
	}()
}

func (this *EventSystem) generateEvent(e Event) {
	v, ok := this.hash[e.Code]
	if ok {
		v.Execute(e)
	}
}

func NewEventSystem() *EventSystem {
	return (&EventSystem{}).Init()
}

  EventSystem实际上是一个调用者,它只接收Command对象。所以要把传入的函数做适配。

优点

  • 降低了系统耦合度。
  • 新的命令可以很容易添加到系统中去。

缺点

  • 使用命令模式可能会导致某些系统有过多的具体命令类。

注意事项

  系统需要支持命令的撤销(Undo)操作和恢复(Redo)操作,也可以考虑使用命令模式,见命令模式的扩展。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值