-
Notifications
You must be signed in to change notification settings - Fork 12
/
factory_method.cr
89 lines (74 loc) · 1.71 KB
/
factory_method.cr
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
# Used to replace class constructors, abstracting the process of object generation
# so that the type of the object instantiated can be determined at run-time.
abstract class Fighter
getter name : String
def initialize(@name)
end
def fullname
"#{@name} (#{self.class})"
end
end
class Human < Fighter
end
class God < Fighter
end
class Kiborg < Fighter
end
class Wampire < Fighter
end
class Tournament
getter team1 = [] of Fighter
getter team2 = [] of Fighter
def initialize(number_of_fights = 0)
number_of_fights.times do |i|
@team1 << create_fighter(:team1, "Fighter ##{i + 1}")
@team2 << create_fighter(:team2, "Fighter ##{i + 1}")
end
end
end
class HumansVSWampires < Tournament
def create_fighter(team, name)
case team
when :team1
Human.new(name)
when :team2
Wampire.new(name)
else
raise Exception.new "unknown team #{team}"
end
end
end
class KiborgsVSGods < Tournament
def create_fighter(team, name)
case team
when :team1
Kiborg.new(name)
when :team2
God.new(name)
else
raise Exception.new "unknown team #{team}"
end
end
end
t1 = HumansVSWampires.new(2)
puts "Team1:\n #{t1.team1.map(&.fullname).join("\n ")}"
puts "Team2:\n #{t1.team2.map(&.fullname).join("\n ")}"
# Team1:
# Fighter #1 (Human)
# Fighter #2 (Human)
# Team2:
# Fighter #1 (Wampire)
# Fighter #2 (Wampire)
t2 = KiborgsVSGods.new(4)
puts "Team1:\n #{t2.team1.map(&.fullname).join("\n ")}"
puts "Team2:\n #{t2.team2.map(&.fullname).join("\n ")}"
# Team1:
# Fighter #1 (Kiborg)
# Fighter #2 (Kiborg)
# Fighter #3 (Kiborg)
# Fighter #4 (Kiborg)
# Team2:
# Fighter #1 (God)
# Fighter #2 (God)
# Fighter #3 (God)
# Fighter #4 (God)