-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_patern2.rb
75 lines (62 loc) · 1.42 KB
/
command_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
72
73
74
75
class BaseCommand
def execute
raise 'Method execute must be implemented'
end
end
class AmplifyShieldCommand < BaseCommand
def execute
'Amplified Plutonium-Gamma Shield'
end
end
class CalibrateDriverCommand < BaseCommand
def execute
'Calibrate Urbanium-Rod Driver'
end
end
class TestCompilerCommand < BaseCommand
def execute
'Tested Jupiter Wave Compiler'
end
end
class InstallRegulatorCommand < BaseCommand
def execute
'Installed Hydroelecric Magnetosphere Regulator'
end
end
class Computer
attr_reader :queue
def initialize
@queue = []
end
def add(command)
@queue << command
end
def execute
@queue.inject([]) { |result, command| result << command.execute }.join(' ~ ')
end
end
class Reactor
def initialize
@functional = false
@right_command = [
'Amplified Plutonium-Gamma Shield',
'Calibrate Urbanium-Rod Driver',
'Tested Jupiter Wave Compiler',
'Installed Hydroelecric Magnetosphere Regulator'
].join(' ~ ')
end
def fix(result)
@functional = result == @right_command
end
def functional?
@functional
end
end
comp = Computer.new
comp.add(AmplifyShieldCommand.new)
comp.add(CalibrateDriverCommand.new)
comp.add(TestCompilerCommand.new)
comp.add(InstallRegulatorCommand.new)
reactor = Reactor.new
reactor.fix(comp.execute)
p reactor.functional?