单例

存在这么一类class, 无论class怎么初始化, 产生的instance都是同一个对象。

Code

string.toHTMLCode = function(self)

return encodeHTML(self)

end

-- Instantiates a class

local function _instantiate(class, ...)

-- 单例模式,如果实例已经生成,则直接返回

if rawget(class, "__singleton") then

-- _G[class]值为本class的实例

if _G[class] then

return _G[class]

end

end

local inst = setmetatable({__class=class}, {__index = class})

if inst.__init__ then

inst:__init__(...)

end

--单例模式,如果实例未生成,则将实例记录到类中

if rawget(class, "__singleton") then

if not _G[class] then

_G[class] = inst

end

end

return inst

end

-- LUA类构造函数

local function class(base)

local metatable = {

__call = _instantiate,

__index = base

}

-- __parent 属性缓存父类,便于子类索引父类方法

local _class = {__parent = base}

-- 在class对象中记录 metatable ,以便重载 metatable.__index

_class.__metatable = metatable

return setmetatable(_class, metatable)

end

---- code lua format ------

-- Instantiates a class
local function _instantiate(class, ...)
-- 抽象类不能实例化
if rawget(class, "__abstract") then
error("asbtract class cannot be instantiated.")
end -- 单例模式,如果实例已经生成,则直接返回
if rawget(class, "__singleton") then
-- _G[class]值为本class的实例
if _G[class] then
return _G[class]
end
end local inst = setmetatable({__class=class}, {__index = class})
if inst.__init__ then
inst:__init__(...)
end --单例模式,如果实例未生成,则将实例记录到类中
if rawget(class, "__singleton") then
if not _G[class] then
_G[class] = inst -- 对类对象增加实例获取接口
class.getInstance = function ( self )
return _G[class]
end
            -- 对类对象增加实例销毁
class.destroyInstance = function ( self )
return _G[class]
end
end
end return inst
end -- LUA类构造函数
local function class(base)
local metatable = {
__call = _instantiate,
__index = base
} -- __parent 属性缓存父类,便于子类索引父类方法
local _class = {__parent = base} -- 在class对象中记录 metatable ,以便重载 metatable.__index
_class.__metatable = metatable return setmetatable(_class, metatable)
end --- Test whether the given object is an instance of the given class.
-- @param object Object instance
-- @param class Class object to test against
-- @return Boolean indicating whether the object is an instance
-- @see class
-- @see clone
function instanceof(object, class)
local meta = getmetatable(object)
while meta and meta.__index do
if meta.__index == class then
return true
end
meta = getmetatable(meta.__index)
end return false
end

使用说明:

使用方法, class继承方式不变

如果给定义的class设置一个 _singleton 为 true, 开启单利模式。

Dmenu = class() -- 菜单类
Dmenu.__singleton = true -- 开启单例模式

使用类创建实例:

local menu = Dmenu()
if Dmenu:getInstance() == menu then
print("true")
end Dmenu:destroyInstance()
menu:destroyInstance()

最新文章

  1. 如何给frame标签的src属性以及a标签的href属性自动设值
  2. 解决首次访问jenkins,输入默认密码之后,一直卡住问题
  3. 2016 Al-Baath University Training Camp Contest-1 F
  4. POJ 1845 求a^b的约数和
  5. java 读取TXT文件的方法
  6. python staticmethod classmethod
  7. Ajax调用WebService(一)
  8. String类概述
  9. weblogic启动问题
  10. JavaScript 中的日期和时间
  11. [Android] Volley源代码分析(五岁以下儿童)Q \\ u0026一个
  12. wex5 设置文本居中或图片居中
  13. javaScript中关于字符串的操作函数和方法
  14. HDU [P2819] swap
  15. ●POJ 2794 Double Patience
  16. Python基础2(2017-07-18)
  17. Curator实现分布式锁
  18. golang基础学习及web框架
  19. hashMap 和linkedHashMap
  20. kbmmw 5.06.20 发布

热门文章

  1. 51Nod 1079 中国剩余定理 Label:数论
  2. POJ-A Simple Problem with Integers
  3. JS:操作样式表2 :用JS实现添加和删除一个类名的功能(addClass()和removeClass())
  4. 【CodeVS】 p1225 八数码难题
  5. IOS 蓝牙相关-app作为外设被连接的实现(3)
  6. KindEditor用法介绍
  7. rdp爆破工具 Fast RDP Brute
  8. 使用dbms_crypto包加密关键列数据
  9. zju(3)内核编译与运行
  10. DIV与CSS布局需知