Add observable design pattern implementation

This commit is contained in:
Glenn Y. Rolland 2021-01-03 21:30:58 +01:00
parent 9ff3997bac
commit ef4093c5ba

21
src/lib/observable.cr Normal file
View 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