其他分享
首页 > 其他分享> > 工厂方法模式lua实现

工厂方法模式lua实现

作者:互联网

简单工厂就是生产整个计算器,而工厂方法只生产计算器的一部分;

原有的简单工厂可以生'+' '-' '*' '/' ;但是如果添加新的部件'%',厂房就

需要扩充、修改很可以会影响原来部件的正常生产,这就违背了

开放封闭原则;而工厂方法则不存在这个问题;我新开一个工厂专门

生产'%'就可以了.

  1. Factory = {}
  2. Operation = {}
  3. function Factory:new(o)
  4. o = o or {}
  5. setmetatable(o,self)
  6. self.__index = self
  7. return o;
  8. end
  9.  
  10. function Operation:new(o)
  11. o = o or {}
  12. setmetatable(o,self)
  13. self.__index = self
  14. return o;
  15. end
  16.  
  17.  
  18.  
  19. OperationAdd = Operation:new()
  20.  
  21. function OperationAdd:GetResult()
  22. if self.NumberA and self.NumberB then
  23. return self.NumberA + self.NumberB;
  24. else
  25. return "error"
  26. end
  27. end
  28.  
  29.  
  30. FactoryAdd = Factory:new()
  31.  
  32. function FactoryAdd:CreateOperation()
  33. return OperationAdd:new()
  34. end
  35.  
  36. OperationSub = Operation:new()
  37.  
  38. function OperationSub:GetResult()
  39. if self.NumberA and self.NumberB then
  40. return self.NumberA - self.NumberB;
  41. else
  42. return "error"
  43. end
  44. end
  45.  
  46.  
  47. FactorySub = Factory:new()
  48.  
  49. function FactorySub:CreateOperation()
  50. return OperationSub:new()
  51. end
  52.  
  53. OperationMul = Operation:new()
  54.  
  55. function OperationMul:GetResult()
  56. if self.NumberA and self.NumberB then
  57. return self.NumberA * self.NumberB;
  58. else
  59. return "error"
  60. end
  61. end
  62.  
  63.  
  64. FactoryMul = Factory:new()
  65.  
  66. function FactoryMul:CreateOperation()
  67. return OperationMul:new()
  68. end
  69.  
  70. OperationDiv = Operation:new()
  71.  
  72. function OperationDiv:GetResult()
  73. if self.NumberA and self.NumberB then
  74. return self.NumberA / self.NumberB;
  75. else
  76. return "error"
  77. end
  78. end
  79.  
  80.  
  81. FactoryDiv = Factory:new()
  82.  
  83. function FactoryDiv:CreateOperation()
  84. return OperationDiv:new()
  85. end
  86.  
  87.  
  88.  
  89. operAddFactory = FactoryAdd:new() --定义一个工厂对象
  90.  
  91. operAdd = operAddFactory:CreateOperation()
  92.  
  93. operAdd.NumberA = 10
  94.  
  95. operAdd.NumberB = 5
  96.  
  97. print("Add:"..operAdd:GetResult())
  98.  
  99. operSubFactory = FactorySub:new() --定义一个工厂对象
  100.  
  101. operSub = operSubFactory:CreateOperation()
  102.  
  103. operSub.NumberA = 10
  104.  
  105. operSub.NumberB = 5
  106.  
  107. print("Sub:"..operSub:GetResult())
  108.  
  109. operMulFactory = FactoryMul:new() --定义一个工厂对象
  110.  
  111. operMul = operMulFactory:CreateOperation()
  112.  
  113. operMul.NumberA = 10
  114.  
  115. operMul.NumberB = 5
  116.  
  117. print("Mul:"..operMul:GetResult())
  118.  
  119. operDivFactory = FactoryDiv:new() --定义一个工厂对象
  120.  
  121. operDiv = operDivFactory:CreateOperation()
  122.  
  123. operDiv.NumberA = 10
  124.  
  125. operDiv.NumberB = 5
  126.  
  127. print("Div:"..operDiv:GetResult())
  128.  
  129. --[[ **************新添加的'%'操作*************** ]]--
  130.  
  131. OperationMod = Operation:new()
  132.  
  133. function OperationMod:GetResult()
  134. if self.NumberA and self.NumberB then
  135. return self.NumberA % self.NumberB;
  136. else
  137. return "error"
  138. end
  139. end
  140.  
  141.  
  142. FactoryMod = Factory:new()
  143.  
  144. function FactoryMod:CreateOperation()
  145. return OperationMod:new()
  146. end
  147.  
  148. operModFactory = FactoryMod:new() --定义一个工厂对象
  149.  
  150. operMod = operModFactory:CreateOperation()
  151.  
  152. operMod.NumberA = 10
  153.  
  154. operMod.NumberB = 5
  155.  
  156. print("Mod:"..operMod:GetResult())

输出结果:

Add:15
Sub:5
Mul:50
Div:2
Mod:0


这里有我的一篇简单工厂模式的博文。

简单工厂模式lua实现

如果用这篇博文里的简单工厂来添加'%',需要在

function OperationFactory : CreateOperation(oper)

中修改,添加一个'%'操作。这不符合开放封闭原则;

而用工厂方法,我们只需要扩展我们的程序,不需要做修改。

抽象工厂lua实现

标签:return,NumberA,NumberB,self,end,模式,工厂,lua,new
来源: https://www.cnblogs.com/goodgongdstudy/p/13903453.html