Permalink
Browse files

simple observer pattern example

  • Loading branch information...
1 parent a646ebd commit ca8c749b02df91ff2694d400d21dc9f1f6c8ebd9 @lljk committed Jun 27, 2011
Showing with 33 additions and 0 deletions.
  1. +33 −0 observer_example.rb
View
@@ -0,0 +1,33 @@
+require "observer"
+
+class Publisher
+ include Observable
+ attr_reader :shot
+ def initialize
+ @hits = [1, 3, 5, 7, 9]
+ end
+
+ def shoot
+ 50.times{|n|
+ @shot = rand(10)
+ if @hits.include?(@shot)
+ changed
+ notify_observers(n, @shot)
+ end
+ }
+ end
+
+end
+
+Shoes.app do
+
+ pub = Publisher.new
+
+ def update(n, shot)
+ para "shot ##{n} => #{shot}\n"
+ end
+
+ pub.add_observer(self)
+ pub.shoot
+
+end

0 comments on commit ca8c749

Please sign in to comment.