单例 存在这么一类class, 无论class怎么初始化, 产生的instance都是同一个对象. Code string.toHTMLCode = function(self) return encodeHTML(self) end -- Instantiates a class local function _instantiate(class, ...) -- 单例模式,如果实例已经生成,则直接返回 if rawget(class, "__singleton") then --
lua-单例 function newAccount(initlizedBanlance) local self = {balance = initlizedBanlance} local show = function (v) self.balance = self.balance - v end local getBanlance = function () return self.balance end return { show = show getBanlance = getBanla
function SingleTon:new() local store = nil return function(self) if store then return store end local o ={} setmetatable(o,self) self.__index = self store = o return o end end SingleTon.instance = SingleTon:new()