Skip to content

Instantly share code, notes, and snippets.

@clicube
Created October 26, 2013 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clicube/7169066 to your computer and use it in GitHub Desktop.
Save clicube/7169066 to your computer and use it in GitHub Desktop.
Observerパターンで複数種類の通知を使い分けたい時どうするのかなって考えた
class Subject
attr_reader :subject1, :subject2
def initialize
@subject1 = Object.new.extend(Observable)
@subject2 = Object.new.extend(Observable)
end
def notify1
@subject1.changed
@subject1.notify_observers
end
def notify2
@subject2.changed
@subject2.notify_observers
end
end
class Observer
def initialize(subject)
subject.subject1.add_observer(self, :update1)
subject.subject2.add_observer(self, :update2)
end
def update1
puts "updated 1"
end
def update2
puts "updated 2"
end
end
subject = Subject.new
observer = Observer.new(subject)
subject.notify1
subject.notify2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment