Skip to content

Instantly share code, notes, and snippets.

@JulesWang
Created June 6, 2011 06:59
Show Gist options
  • Save JulesWang/1009841 to your computer and use it in GitHub Desktop.
Save JulesWang/1009841 to your computer and use it in GitHub Desktop.
observer pattern by ruby
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