-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver_patern2.rb
71 lines (60 loc) · 1.71 KB
/
observer_patern2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
=begin
Наблюдатель. Определяет зависимость типа «один ко многим» между объектами
таким образом, что при изменении состояния одного объекта все зависящие
от него оповещаются об этом событии.
У нас есть класс Ticker, который будет менять цену и оповещать об изменении
наблюдателей WarnLow и WarnHigh, которые будут выводить соответствующие
сообщения в случае с выходом цена за заданные пределы.
=end
require 'observer'
class Ticker
include Observable
def initialize(symbol)
@symbol = symbol
end
def run
last_price = nil
10.times do
price = Price.fetch(@symbol)
print "Current price: #{price}\n"
if price != last_price
changed
last_price = price
notify_observers(Time.now, price)
end
sleep 1
end
end
end
class Price
def Price.fetch(symbol)
60 + rand(80)
end
end
class Warner
def initialize(ticker, limit)
@limit = limit
ticker.add_observer(self)
end
def update(time, price)
raise 'Method "update" must be implemented!'
end
end
class WarnLow < Warner
def update(time, price)
if price < @limit
print "---#{time.to_s}: Price below #{@limit}: #{price}\n"
end
end
end
class WarnHigh < Warner
def update(time, price)
if price > @limit
print "+++#{time.to_s}: Price above #{@limit}: #{price}\n"
end
end
end
ticker = Ticker.new('MSFT')
WarnLow.new(ticker, 80)
WarnHigh.new(ticker, 120)
ticker.run