单例

存在这么一类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. POJ 1127 Jack Straws(计算几何)

    题目链接 抄的模版,居然1Y了.就是简单的线段相交+并查集. #include <iostream> #include <cstring> #include <cstdi ...

  2. Find和FirstOrDefault()有什么区别?

    Find方法和FirstOrDefault方法效果相同,都是返回满足条件的第一个元素,如果没有该元素,则返回null. 那么这两个扩展方法有什么不同? 1)Find方法是.netFramework2. ...

  3. PL/SQL无法连接,提示:pl/sql initialization error sql*net not properly installed

    各种找资料都不行,最后还是在公司的线上环境中去找不同点配置好的.不多说了,就做了如下配置: 服务器环境是:windows server 2008 64位操作系统 1.电脑系统环境变量PATH增加:x: ...

  4. java工程展示问题

    当java工程这样展示时,需要选择window---->Open Perspective----->java改变java工程的展示方式

  5. C# Closure

    JavaScript Closure function f1(){ var n=999; return function(){ alert(n); // 999 return n; } } var a ...

  6. inconfont 字体库应用

    先去注册个号码,好像只可以用新浪微博登录哈,搞一个微博去. 第一就是点上面图标库,选择官方和所有都行. 恩接着点一个图标,他就自己跑到 第二个按钮哪里去了,在点第二个按钮,会出来一个创建项目,随便创建 ...

  7. [CareerCup] 17.9 Word Frequency in a Book 书中单词频率

    17.9 Design a method to find the frequency of occurrences of any given word in a book. 这道题让我们找书中单词出现 ...

  8. WinForm timer控件

    timer 控件:按用户定义的时间间隔引发的事件 属性: Enabled   是否启用:  Interval    事件发生的事件间隔,单位是毫秒 事件只有一个:Tick    事件经过指定的时间间隔 ...

  9. firefox如何禁用JavaScript

    1.在Firefox地址栏里输入“about:config”.2.在搜索栏输入“javascript.enabled”查找到首选项.3.点击鼠标右键选择“切换”,把“javascript.enable ...

  10. Thinkphp框架感悟(二)

    这次主要分析一下I方法 /** * 获取输入参数 支持过滤和默认值 * 使用方法: * <code> * I('id',0); 获取id参数 自动判断get或者post * I('post ...