Created
June 6, 2011 06:59
-
-
Save JulesWang/1009841 to your computer and use it in GitHub Desktop.
observer pattern by ruby
This file contains 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
class Newspaper | |
include Observable | |
def deliver(news) | |
changed | |
@news = news | |
notify_observers @news | |
end | |
end | |
class Subscriber | |
def update(news) | |
p news | |
end | |
end | |
newspaper = Newspaper.new | |
# add two subscribers | |
a = Subscriber.new | |
b = Subscriber.new | |
newspaper.add_observer(a) | |
newspaper.add_observer(b) | |
newspaper.deliver("some news") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment