-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.7.go
51 lines (44 loc) · 1.25 KB
/
6.7.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import "fmt"
// 实例化一个通过字符串映射函数切片的map
var eventByName = make(map[string][]func(interface{}))
// 注册事件,提供事件名和回调函数
func RegisterEvent(name string, callback func(interface{})) {
// 通过名字查找事件列表
list := eventByName[name]
// 在列表切片中添加函数
list = append(list, callback)
// 将修改的事件列表切片保存回去
eventByName[name] = list
}
// 调用事件
func CallEvent(name string, param interface{}) {
// 通过名字找到事件列表
list := eventByName[name]
// 遍历这个事件的所有回调
for _, callback := range list {
// 传入参数调用回调
callback(param)
}
}
// 声明角色的结构体
type Actor struct {
}
// 为角色添加一个事件处理函数
func (a *Actor) OnEvent(param interface{}) {
fmt.Println("actor event:", param)
}
// 全局事件
func GlobalEvent(param interface{}) {
fmt.Println("global event:", param)
}
func main() {
// 实例化一个角色
a := new(Actor)
// 注册名为OnSkill的回调
RegisterEvent("OnSkill", a.OnEvent)
// 再次在OnSkill上注册全局事件
RegisterEvent("OnSkill", GlobalEvent)
// 调用事件,所有注册的同名函数都会被调用
CallEvent("OnSkill", 100)
}