Created
December 3, 2011 07:44
-
-
Save rubydubee/1426455 to your computer and use it in GitHub Desktop.
Observer and Duck type
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
$: << File.join(File.dirname(__FILE__), "/") | |
require "Observable.rb" | |
require "Observer1.rb" | |
require "Observer2.rb" | |
observable = Observable.new | |
o1 = Observer1.new(observable) | |
o2 = Observer2.new(observable) | |
observable.called |
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 Observable | |
def initialize | |
@observers = [] | |
end | |
def observe(o) | |
@observers << o | |
end | |
def called | |
puts "Observable is being called" | |
@observers.each { |o| o.update } | |
end | |
end |
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 Observer1 | |
def initialize(obs) | |
obs.observe(self) | |
end | |
def update | |
puts "Okay! Observing in Observer1" | |
end | |
end |
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 Observer2 | |
def initialize(obs) | |
obs.observe(self) | |
end | |
def update | |
puts "Okay! Observing in Observer2" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment