-
Notifications
You must be signed in to change notification settings - Fork 33
/
simpleFactory.lua
86 lines (70 loc) · 1.54 KB
/
simpleFactory.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
OperationFactory = {}
Operation = {}
function Operation:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
o.numberA = 0
o.numberB = 1
return o
end
OperationAdd = Operation:new()
OperationSub = Operation:new()
OperationMul = Operation:new()
OperationDiv = Operation:new()
function OperationAdd:GetResult()
if self.numberA and self.numberB then
return self.numberA + self.numberB;
else
return "error"
end
end
function OperationSub:GetResult()
if self.numberA and self.numberB then
return self.numberA - self.numberB;
else
return "error"
end
end
function OperationMul:GetResult()
if self.numberA and self.numberB then
return self.numberA * self.numberB;
else
return "error"
end
end
function OperationDiv:GetResult()
if self.numberA and self.numberB then
return self.numberA / self.numberB;
else
return "error"
end
end
function OperationFactory:CreateOperation(oper)
if oper == "+" then
return OperationAdd:new()
elseif oper == "-" then
return OperationSub:new()
elseif oper == "*" then
return OperationMul:new()
elseif oper == "/" then
return OperationDiv:new()
else
end
end
Oper1 = OperationFactory:CreateOperation("+")
Oper1.numberA = 10
Oper1.numberB = 5
print(Oper1:GetResult())
Oper2 = OperationFactory:CreateOperation("-")
Oper2.numberA = 10
Oper2.numberB = 5
print(Oper2:GetResult())
Oper3 = OperationFactory:CreateOperation("*")
Oper3.numberA = 10
Oper3.numberB = 5
print(Oper3:GetResult())
Oper4 = OperationFactory:CreateOperation("/")
Oper4.numberA = 10
Oper4.numberB = 5
print(Oper4:GetResult())