策略模式Lua达成
发布时间:2021-11-20 18:48:35 所属栏目:教程 来源:互联网
导读:策略模式Lua实现 Strategy = {} ConcreteStrategyA = {} ConcreteStrategyB = {} ConcreteStrategyC = {} Context = {strategy = nil} function Strategy:new(o) o = o or {} setmetatable(o,self) self.__index = self return o; end function Strategy:Algo
策略模式Lua实现 Strategy = {} ConcreteStrategyA = {} ConcreteStrategyB = {} ConcreteStrategyC = {} Context = {strategy = nil} function Strategy:new(o) o = o or {} setmetatable(o,self) self.__index = self return o; end function Strategy:AlgorithmInterface() print("逻辑接口") end ConcreteStrategyA = Strategy:new() function ConcreteStrategyA:AlgorithmInterface() print("具体策略A") end function Context:new(o,s) o = o or {} setmetatable(o,self) self.__index = self if s ~= nil then o.strategy = s end return o; end function Context:ContextInterface() self.strategy:AlgorithmInterface() end context = Context:new({},ConcreteStrategyA:new()) context:ContextInterface() 上面的代码的输出结果是:具体的策略A 如果你把 function ConcreteStrategyA:AlgorithmInterface() 注释掉,输出的结果应该就会是:逻辑接口。 (编辑:常州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |