-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_patern3.rb
89 lines (66 loc) · 1.19 KB
/
command_patern3.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class BaseCommand
attr_reader :hero
def initialize(hero)
@hero = hero
end
def execute
raise 'Method execute must be implemented!'
end
end
class GetMoneyCommand < BaseCommand
MONEY_VALUE = 100
def execute
@hero.money += MONEY_VALUE
end
def unexecute
@hero.money -= MONEY_VALUE
end
end
class HealCharacter < BaseCommand
HEALTH_VALUE = 5
def execute
@hero.health += HEALTH_VALUE
end
def unexecute
@hero.health -= HEALTH_VALUE
end
end
class Hero
attr_reader :name
attr_accessor :health, :money
def initialize(name, health = 100, money = 800)
@name = name
@health = health
@money = money
end
def info
"Name: #{@name}, health: #{@health}, money: #{@money}"
end
def print_info
p info
end
end
class Turn
def initialize
@moves = []
end
def make_move(move)
move.execute
@moves << move
end
def undo_move
@moves.pop.unexecute
end
end
hero = Hero.new('Spartak')
turn = Turn.new
money = GetMoneyCommand.new(hero)
heal = HealCharacter.new(hero)
hero.print_info
turn.make_move(money)
turn.make_move(heal)
hero.print_info
turn.undo_move
hero.print_info
turn.undo_move
hero.print_info