单例

存在这么一类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()

LUA OOP 单例模式实现的 一个 方案的更多相关文章

  1. lua OOP实现对象的链式调用

    数学中的链式法则 http://sx.zxxk.com/ArticleInfo.aspx?InfoID=164649 链式微分法则:实数运算的链式法则:对数运算的链式法则:平行公理的链式法则:向量运算 ...

  2. lua判断字符串包含另一个字符串

    lua判断字符串包含另一个字符串 --string.find("元字符串","模式字符串") 如下: print(string.find("CCBWe ...

  3. LUA OOP编程实现方法

    lua原生不支持OOP特性 确实如此, 同时可以采用其它lua代码的方式实现OOP的特性. OOP四大特性 抽象 封装 继承 多态 http://www.cnblogs.com/xiaosongluf ...

  4. C++笔记1: 单例模式。(一个简单的设计模式在C++中复杂出翔。。)

    C++ 如果用指针new一个单例,内存不容易释放,所以Java和C#等语言中的单例模式在C++不适用... C++中,new申请的内存必须由delete释放,例如: Point p1; Point * ...

  5. Headless Chrome:服务端渲染JS站点的一个方案【上篇】【翻译】

    原文链接:https://developers.google.com/web/tools/puppeteer/articles/ssr 注:由于英文水平有限,没有逐字翻译,可以选择直接阅读原文 tip ...

  6. Headless Chrome:服务端渲染JS站点的一个方案【中篇】【翻译】

    接上篇 防止重新渲染 其实说不对客户端代码做任何修改是忽悠人的.在我们的Express 应用中,通过Puppteer加载页面,提供给客户端响应,但是这个过程是有一些问题的. js脚本在服务端的Head ...

  7. Lua:Nginx Lua环境配置,第一个Nginx Lua代码

    一.编译安装LuaJIT Lua:编译安装LuaJIT,第一个Lua程序 http://blog.csdn.net/guowenyan001/article/details/48250427 二.下载 ...

  8. 设置模式之单例模式(附上一个Objective-C编写的播放音乐的单例类)

    在查阅Cocoa Touch开发文档时,会发现框架中随处可见的大量单例类,比如说,UIApplication.NSFileManager 等. UIApplication 框架中极为常用的一个单例类, ...

  9. javascript OOP编辑思想的一个实践参考

    <html> <style type="text/css"> .current { background-color: red; } .dv { backg ...

随机推荐

  1. Yahoo!网站性能最佳体验的34条黄金守则

    Yahoo!的Exceptional Performance团队为改善Web性能带来最佳实践.他们为此进行了一系列的实验.开发了各种工具.写了大量的文章和博客并在各种会议上参与探讨.最佳实践的核心就是 ...

  2. 【POJ】1160 Post Office

    http://poj.org/problem?id=1160 题意:直线上有n个城市,其中有p个城市有邮局,问如何建p个邮局使得每个城市到最近的邮局和最小.(n<=300, p<=30&a ...

  3. 【BZOJ3673】&&【BZOJ3674】: 可持久化并查集 by zky 可持久化线段树

    没什么好说的. 可持久化线段树,叶子节点存放父亲信息,注意可以规定编号小的为父亲. Q:不是很清楚空间开多大,每次询问父亲操作后修改的节点个数是不确定的.. #include<bits/stdc ...

  4. SQL SERVER各种用法总结

    sql创建临时表http://www.cnblogs.com/jeffwongishandsome/archive/2009/08/05/1526466.html sqlserver更改表名 EXEC ...

  5. DataContract

    DataContractAttribute Class is in the System.Runtime.Serialization namespace. But you should add ref ...

  6. java编程eclipse常用快捷键方式

    Eclipse 常用快捷键 Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] 此快捷键为用户 ...

  7. [LintCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  8. jQuery -> 获取元素的各种过滤器

    转自http://blog.csdn.net/feelang/article/details/26613023

  9. ASM文件系统

    1.确认数据库版本 2.个人理解的存储解决方案的发展趋势 2.1图示说明 2.2图示描述 如上图我们描述了在不同时期的IT行业(数据库)出现的存储文件系统,下面我们将分别说明: ü  裸设备:所谓裸设 ...

  10. 织梦DEDECMS网站后台安全检测提示 加一个开关

    1.进入后台后,点击 系统->系统基本参数->添加变量: 变量名称:cfg_safecheck_switch 变量值:N 变量类型:布尔(Y/N) 参数说明:启用安全监测系统: 2.找到系 ...