https://github.com/changsongl/gevent
Observer pattern package in Golang.
https://github.com/changsongl/gevent
design-patterns go observer observer-pattern
Last synced: 2 months ago
JSON representation
Observer pattern package in Golang.
- Host: GitHub
- URL: https://github.com/changsongl/gevent
- Owner: changsongl
- License: mit
- Created: 2022-03-17T02:02:43.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-09T02:25:24.000Z (about 2 years ago)
- Last Synced: 2024-12-18T08:39:31.847Z (5 months ago)
- Topics: design-patterns, go, observer, observer-pattern
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gevent
GEvent is a golang package for observer pattern.
The Observer pattern addresses the following problems:
- A one-to-many dependency between objects should be defined without making the objects tightly coupled.
- It should be ensured that when one object changes state, an open-ended number of dependent objects are updated automatically.
- It should be possible that one object can notify an open-ended number of other objects.### Example:
```` go
// init.go
type test struct {
data int
}type testLog struct {
}func (t testLog) Error(msg string) {
myProgramLog.Error(msg)
}var ge GEvent
func init() {
ge = NewGEvent(NewLogOption(&testLog{}))
}// account.go
func Register() {// some logic for register
a := test{1}
b := &test{2}
ge.TriggerEvent("account-create", a, b)
}// point.go
func init() {
ge.AddObserver("account-create", "point", func(a test, b *test) {
// do something for points
}, true)
}// user.go
func init() {
ge.AddObserver("account-create", "user", func(a test, b *test) {
// do something for user
}, true)
}````