Add observable design pattern implementation
This commit is contained in:
parent
9ff3997bac
commit
ef4093c5ba
1 changed files with 21 additions and 0 deletions
21
src/lib/observable.cr
Normal file
21
src/lib/observable.cr
Normal file
|
@ -0,0 +1,21 @@
|
|||
|
||||
# Code from: https://github.com/crystal-community/crystal-patterns
|
||||
# Licence: MIT
|
||||
|
||||
module Observable(T)
|
||||
getter observers
|
||||
|
||||
def add_observer(observer)
|
||||
@observers ||= [] of T
|
||||
@observers.not_nil! << observer
|
||||
end
|
||||
|
||||
def delete_observer(observer)
|
||||
@observers.try &.delete(observer)
|
||||
end
|
||||
|
||||
def notify_observers
|
||||
@observers.try &.each &.update self
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in a new issue