单例

存在这么一类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. 【NOI2016】区间 题解

    题目大意: 有n个区间,当有m个区间有公共部分时,求m个区间长度的最大值与最小值之差的最小值. 思路: 按区间的长度从小到大排序,可知连续的几个区间最优,则用两个指针指其头尾,线性扫描,再用线段树区间 ...

  2. AJAX 跨域请求 - JSONP获取JSON数据

    Asynchronous JavaScript and XML (Ajax ) 是驱动新一代 Web 站点(流行术语为 Web 2.0 站点)的关键技术.Ajax 允许在不干扰 Web 应用程序的显示 ...

  3. UVA 11039 - Building designing(DP)

    题目链接 本质上是DP,但是俩变量就搞定了. #include <cstdio> #include <cstring> #include <algorithm> u ...

  4. 透过现象看现象(SQL501N错误处理)

    某日一直运行比较正常的报表系统,突然报存储过程执行失败,查看DB2 错误返回码为sql501n,查看此错误原因如下: [db2inst1@limt ~]$ db2 ? sql501n SQL0501N ...

  5. NOI模拟赛Day3

    终于A题啦鼓掌~开心~ 开考看完题后,觉得第二题很好捏(傻叉上线 搞到十一点准备弃疗了然后突然发现我会做第一题 于是瞎码了码,就去准备饭票了... 好了,停止扯淡(就我一个我妹子每天不说话好难受QAQ ...

  6. 【bzoj1455】罗马游戏 可并堆

    2016-05-31  10:04:41 可并堆的裸题. 左偏树(小根堆为例 性质 1.满足堆的性质,每个节点权值小于左右儿子权值 2.每个节点有dis值,表示子树最浅的叶子深度加1 3.左子树dis ...

  7. C语言(4)

    C语言(4)--数据类型 C语言在用“/”是,注意左右两边都是整数时,商也是只有整数部分. 下面介绍一下C语言常用的数据类型:  注意: 1.char类型数据范围:256中字符. 2.float和do ...

  8. 前端自动化工具 -- fis 使用简介

    https://github.com/fex-team/fis FIS入门: http://fis.baidu.com/docs/beginning/getting-started.html FIS ...

  9. 在VS2010中创建并引用dll(C#)

    一般情况下,如果在新建或添加时选择“windows应用程序”或“控制台应用程序”时,‎结果都会被编译成exe,而选择“类库”时就会被编译成dll.也可以在项目属性中更改其输出类型,如下图:       ...

  10. bootstrap日期插件

    <!DOCTYPE HTML> <html> <head> <link href="http://netdna.bootstrapcdn.com/t ...