-
Notifications
You must be signed in to change notification settings - Fork 33
/
factoryMethod.lua
105 lines (82 loc) · 1.85 KB
/
factoryMethod.lua
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Factory = {}
Operation = {}
function Factory:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Operation:new(o)
o = o or {}
setmetatable(o,self)
self.__index = self
return o
end
OperationAdd = Operation:new()
function OperationAdd:GetResult()
if self.numberA and self.numberB then
return self.numberA + self.numberB
else
return "error"
end
end
FactoryAdd = Factory:new()
function FactoryAdd:CreateOperation()
return OperationAdd:new()
end
OperationSub = Operation:new()
function OperationSub:GetResult()
if self.numberA and self.numberB then
return self.numberA - self.numberB
else
return "error"
end
end
FactorySub = Factory:new()
function FactorySub:CreateOperation()
return OperationSub:new()
end
OperationMul = Operation:new()
function OperationMul:GetResult()
if self.numberA and self.numberB then
return self.numberA * self.numberB
else
return "error"
end
end
FactoryMul = Factory:new()
function FactoryMul:CreateOperation()
return OperationMul:new()
end
OperationDiv = Operation:new()
function OperationDiv:GetResult()
if self.numberA and self.numberB then
return self.numberA / self.numberB
else
return "error"
end
end
FactoryDiv = Factory:new()
function FactoryDiv:CreateOperation()
return OperationDiv:new()
end
operAddFactory = FactoryAdd:new()
operAdd = operAddFactory:CreateOperation()
operAdd.numberA = 10
operAdd.numberB = 5
print(operAdd:GetResult())
operSubFactory = FactorySub:new()
operSub = operSubFactory:CreateOperation()
operSub.numberA = 10
operSub.numberB = 5
print(operSub:GetResult())
operMulFactory = FactoryMul:new()
operMul = operMulFactory:CreateOperation()
operMul.numberA = 10
operMul.numberB = 5
print(operMul:GetResult())
operDivFactory = FactoryDiv:new()
operDiv = operDivFactory:CreateOperation()
operDiv.numberA = 10
operDiv.numberB = 5
print(operDiv:GetResult())