Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 24b63d6

Browse files
author
chenlumin
committedJan 6, 2023
feat: init
0 parents  commit 24b63d6

File tree

7 files changed

+157
-0
lines changed

7 files changed

+157
-0
lines changed
 

‎go.work

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go 1.18
2+
3+
use ./go

‎go/creational/prototype/prototype.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package prototype
2+
3+

‎go/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/Miltian/design-pattern
2+
3+
go 1.18

‎go/structural/REAME.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### 发布订阅者
2+
3+
1. 一对多
4+
2. 当一个对象改变, 其他依赖的对象会被同步通知

‎go/structural/observer/event_bus.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package observer
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"sync"
7+
)
8+
9+
type IEventBus interface {
10+
// on(eventName string, handler interface{})
11+
// off(evnetName string, handler interface{})
12+
// 订阅事件
13+
Subscribe(eventName string, handler interface{}) error
14+
// 取消订阅事件
15+
UnSubscribe(eventName string, handler interface{}) error
16+
// 发布事件
17+
Publish(eventName string, args ...interface{})
18+
}
19+
20+
type AsyncEventBus struct {
21+
handlers map[string][]reflect.Value
22+
lock sync.Mutex
23+
}
24+
25+
func NewAsyncEventBus() *AsyncEventBus {
26+
return &AsyncEventBus{
27+
handlers: make(map[string][]reflect.Value),
28+
lock: sync.Mutex{},
29+
}
30+
}
31+
32+
func (b *AsyncEventBus) Subscribe(eventName string, handler interface{}) error {
33+
34+
b.lock.Lock()
35+
defer b.lock.Unlock()
36+
37+
v := reflect.ValueOf(handler)
38+
if v.Type().Kind() != reflect.Func {
39+
return fmt.Errorf("handler must be Func but is %s", v.Type().Kind())
40+
}
41+
42+
// 往事件注册函数, 不存在初始化数组
43+
handlers, ok := b.handlers[eventName]
44+
if !ok {
45+
handlers = []reflect.Value{}
46+
}
47+
handlers = append(handlers, v)
48+
b.handlers[eventName] = handlers
49+
return nil
50+
}
51+
52+
func (b *AsyncEventBus) UnSubscribe(eventName string, handler interface{}) error {
53+
return nil
54+
}
55+
56+
// 发布事件
57+
func (b *AsyncEventBus) Publish(eventName string, args ...interface{}) {
58+
handlers, ok := b.handlers[eventName]
59+
if !ok {
60+
return
61+
}
62+
63+
params := make([]reflect.Value, len(args))
64+
for _, v := range args {
65+
arg := reflect.ValueOf(v)
66+
params = append(params, arg)
67+
}
68+
69+
// // handler := reflect.ValueOf(v)
70+
// go v.Call(params)
71+
// }
72+
73+
for i := range handlers {
74+
go handlers[i].Call(params)
75+
}
76+
}

‎go/structural/observer/observer.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package observer
2+
3+
import "fmt"
4+
5+
type ISubject interface {
6+
Register(observer IObserver)
7+
Remove(observer IObserver)
8+
Notify(msg string)
9+
}
10+
11+
type IObserver interface {
12+
Update(msg string)
13+
}
14+
15+
//发布者
16+
type Subject struct {
17+
observers []IObserver
18+
}
19+
20+
// 新增订阅者
21+
func (s *Subject) Register(observer IObserver) {
22+
s.observers = append(s.observers, observer)
23+
}
24+
25+
// 移除订阅者
26+
func (s *Subject) Remove(observer IObserver) {
27+
for i, v := range s.observers {
28+
if v == observer {
29+
s.observers = append(s.observers[:i], s.observers[i:]...)
30+
}
31+
}
32+
}
33+
34+
func (s *Subject) Notify(msg string) {
35+
for _, v := range s.observers {
36+
v.Update(msg)
37+
}
38+
}
39+
40+
// 订阅者
41+
type Observer1 struct{}
42+
43+
func (o *Observer1) Update(msg string) {
44+
fmt.Printf("%s observer1 update! \n", msg)
45+
}
46+
47+
type Observer2 struct{}
48+
49+
func (o *Observer2) Update(msg string) {
50+
fmt.Printf("%s observer2 update! \n", msg)
51+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package observer
2+
3+
import "testing"
4+
5+
func TestSubjectAndObserver(t *testing.T) {
6+
s := Subject{}
7+
8+
o1 := Observer1{}
9+
o2 := Observer2{}
10+
11+
12+
s.Register(&o1)
13+
s.Register(&o2)
14+
15+
s.Notify("hello")
16+
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.