-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-abstract-factory.rb
73 lines (60 loc) · 1.01 KB
/
01-abstract-factory.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
# 01. ABSTRACT FACTORY
# Abstract factory
class FruitsFactory
def make_apple
raise NotImplementedError
end
def make_orange
raise NotImplementedError
end
end
# Concrete factory
class BlueFruitsFactory < FruitsFactory
def make_apple
return new BlueApple
end
def make_orange
return new BlueOrange
end
end
# Concrete factory
class GreenFruitsFactory < FruitsFactory
def make_apple
return new GreenApple
end
def make_orange
return new GreenOrange
end
end
# Abstract product
class Apple
end
# Concrete product
class GreenApple < Apple
end
# Concrete product
class BlueApple < Apple
end
# Abstract product
class Orange
end
# Concrete product
class GreenOrange < Orange
end
# Concrete product
class BlueOrange < Orange
end
# Usage
#
# factory = if color == 'green'
# GreenFruitsFactory
# elsif color == 'blue'
# BlueFruitsFactory
# else
# raise 'No factory for that color'
# end
#
# apple = factory.create_apple
# orange = factory.create_orange
# fruits = [apple, orange]
#