-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_obs.rb
49 lines (42 loc) · 999 Bytes
/
test_obs.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
# coding: utf-8
require 'dxruby'
require 'singleton'
require 'observer'
class BulletObserver
def initialize
@bullet_container = BulletContainer.instance
end
def update bullet
@bullet_container.bullets.push bullet
end
end
class BulletContainer
include Singleton
attr_accessor :bullets
def initialize
@bullets = []
end
end
class Bullet
attr_accessor :position, :velocity
def initialize position, velocity
position = Complex position[0], position[1] if position.kind_of? Array
velocity = Complex velocity[0], velocity[1] if position.kind_of? Array
@position, @velocity = position, velocity
end
end
class BulletBuilder
include Observable
def initialize position, velocity
@obj = Bullet.new position, velocity
add_observer BulletObserver.new
end
def build
changed true
notify_observers @obj
@obj
end
end
BulletBuilder.new([1, 2], [3, 4]).build
BulletBuilder.new([1, 5], [3, 4]).build
puts BulletContainer.instance.bullets