-
Notifications
You must be signed in to change notification settings - Fork 0
/
Abstractfactory.rb
88 lines (71 loc) · 1.04 KB
/
Abstractfactory.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
class Abstractfactory
def initialize(n1,n2)
@animal = []
@feed = []
n1.times do |i|
@animal << animal_new("#{i+1}")
end
n2.times do |i|
@feed << feed_new("#{i+1}")
end
end
def getNames
@animal.each do |v|
puts "Animal #{v.getName}"
end
@feed.each do |v|
puts "Feed #{v.getName}"
end
end
end
class Factory1 < Abstractfactory
def initialize(n1,n2)
super(n1,n2)
end
def animal_new(name)
Lion.new(name)
end
def feed_new(name)
Sheep.new(name)
end
end
class Factory2 < Abstractfactory
def initialize(n1,n2)
super(n1,n2)
end
def animal_new(name)
Lion.new(name)
end
def feed_new(name)
Horse.new(name)
end
end
class Lion
def initialize(name)
@name = name
end
def getName
"Lion:#{@name}"
end
end
class Sheep
def initialize(name)
@name = name
end
def getName
"Sheep:#{@name}"
end
end
class Horse
def initialize(name)
@name = name
end
def getName
"Horse:#{@name}"
end
end
factory = Factory1.new(2,4)
factory.getNames
puts ""
factory = Factory2.new(3,4)
factory.getNames