require 'observer'
class Game
include Observable
def initialize(name)
@name = name
end
def start
changed
notify_observers(@name)
end
end
class Statistic
attr_reader :startsCount, :lastGame
def initialize
@startsCount = 0
@lastGame = ""
end
# callback for observer
def update(lastGame)
@startsCount += 1
@lastGame = lastGame
end
end
statistic = Statistic.new
heroes = Game.new("Heroes")
doom = Game.new("Doom")
heroes.add_observer(statistic)
doom.add_observer(statistic)
heroes.start()
doom.start()
puts "lastGame is '#{statistic.lastGame}'"
puts "startsCount is #{statistic.startsCount}"