工厂方法模式lua实现
作者:互联网
简单工厂就是生产整个计算器,而工厂方法只生产计算器的一部分;
原有的简单工厂可以生'+' '-' '*' '/' ;但是如果添加新的部件'%',厂房就
需要扩充、修改很可以会影响原来部件的正常生产,这就违背了
开放封闭原则;而工厂方法则不存在这个问题;我新开一个工厂专门
生产'%'就可以了.
- 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("Add:"..operAdd:GetResult())
- operSubFactory = FactorySub:new() --定义一个工厂对象
- operSub = operSubFactory:CreateOperation()
- operSub.NumberA = 10
- operSub.NumberB = 5
- print("Sub:"..operSub:GetResult())
- operMulFactory = FactoryMul:new() --定义一个工厂对象
- operMul = operMulFactory:CreateOperation()
- operMul.NumberA = 10
- operMul.NumberB = 5
- print("Mul:"..operMul:GetResult())
- operDivFactory = FactoryDiv:new() --定义一个工厂对象
- operDiv = operDivFactory:CreateOperation()
- operDiv.NumberA = 10
- operDiv.NumberB = 5
- print("Div:"..operDiv:GetResult())
- --[[ **************新添加的'%'操作*************** ]]--
- OperationMod = Operation:new()
- function OperationMod:GetResult()
- if self.NumberA and self.NumberB then
- return self.NumberA % self.NumberB;
- else
- return "error"
- end
- end
- FactoryMod = Factory:new()
- function FactoryMod:CreateOperation()
- return OperationMod:new()
- end
- operModFactory = FactoryMod:new() --定义一个工厂对象
- operMod = operModFactory:CreateOperation()
- operMod.NumberA = 10
- operMod.NumberB = 5
- print("Mod:"..operMod:GetResult())
输出结果:
Add:15
Sub:5
Mul:50
Div:2
Mod:0
这里有我的一篇简单工厂模式的博文。
如果用这篇博文里的简单工厂来添加'%',需要在
function OperationFactory : CreateOperation(oper)
中修改,添加一个'%'操作。这不符合开放封闭原则;
而用工厂方法,我们只需要扩展我们的程序,不需要做修改。
标签:return,NumberA,NumberB,self,end,模式,工厂,lua,new 来源: https://www.cnblogs.com/goodgongdstudy/p/13903453.html