Created
February 5, 2015 20:50
-
-
Save asterite/2add7a706cc4f8fb4642 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Observer(T) | |
abstract def notify(obj : T) | |
end | |
module Observable(T) | |
def add_observer(observer : Observer(T)) | |
observers = @observers ||= [] of Observer(T) | |
observers << observer | |
end | |
def notify_observers(obj : T) | |
@observers.try &.each &.notify(obj) | |
end | |
end | |
class Foo | |
include Observable(Int32) | |
end | |
class Bar < Observer(Int32) | |
def notify(num) | |
puts self | |
puts num | |
end | |
end | |
foo = Foo.new | |
foo.add_observer(Bar.new) | |
foo.add_observer(Bar.new) | |
foo.notify_observers 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment