LUA OOP 单例模式实现的 一个 方案
单例
存在这么一类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 单例模式实现的 一个 方案的更多相关文章
- lua OOP实现对象的链式调用
数学中的链式法则 http://sx.zxxk.com/ArticleInfo.aspx?InfoID=164649 链式微分法则:实数运算的链式法则:对数运算的链式法则:平行公理的链式法则:向量运算 ...
- lua判断字符串包含另一个字符串
lua判断字符串包含另一个字符串 --string.find("元字符串","模式字符串") 如下: print(string.find("CCBWe ...
- LUA OOP编程实现方法
lua原生不支持OOP特性 确实如此, 同时可以采用其它lua代码的方式实现OOP的特性. OOP四大特性 抽象 封装 继承 多态 http://www.cnblogs.com/xiaosongluf ...
- C++笔记1: 单例模式。(一个简单的设计模式在C++中复杂出翔。。)
C++ 如果用指针new一个单例,内存不容易释放,所以Java和C#等语言中的单例模式在C++不适用... C++中,new申请的内存必须由delete释放,例如: Point p1; Point * ...
- Headless Chrome:服务端渲染JS站点的一个方案【上篇】【翻译】
原文链接:https://developers.google.com/web/tools/puppeteer/articles/ssr 注:由于英文水平有限,没有逐字翻译,可以选择直接阅读原文 tip ...
- Headless Chrome:服务端渲染JS站点的一个方案【中篇】【翻译】
接上篇 防止重新渲染 其实说不对客户端代码做任何修改是忽悠人的.在我们的Express 应用中,通过Puppteer加载页面,提供给客户端响应,但是这个过程是有一些问题的. js脚本在服务端的Head ...
- Lua:Nginx Lua环境配置,第一个Nginx Lua代码
一.编译安装LuaJIT Lua:编译安装LuaJIT,第一个Lua程序 http://blog.csdn.net/guowenyan001/article/details/48250427 二.下载 ...
- 设置模式之单例模式(附上一个Objective-C编写的播放音乐的单例类)
在查阅Cocoa Touch开发文档时,会发现框架中随处可见的大量单例类,比如说,UIApplication.NSFileManager 等. UIApplication 框架中极为常用的一个单例类, ...
- javascript OOP编辑思想的一个实践参考
<html> <style type="text/css"> .current { background-color: red; } .dv { backg ...
随机推荐
- ACM 寻找最大数
寻找最大数 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=920813467185 ...
- ACM: meixiuxiu学图论-并查集-最小生成树-解题报告
/* 最小生成树,最小环的最大权值按照排序后去构建最小生成树就可以了,注意遇到的第一个根相同的点就记录权值,跳出,生成的环就是最小权值环. */ //AC代码: #include"iostr ...
- tornado 学习笔记4 异步以及非阻塞的I/O
Read-time(实时)的网站需要针对每个用户保持长时间的连接.在传统的同步网站服务中,通常针对每个用户开启来一个线程来实现,但是这样做非常昂贵. 为了使并发连接的成本最小化,Tornada使用单个 ...
- iOS 9之适配ATS(转载)
iOS 9系统已经出来了,而网络方面的ATS(App Transport Security)特性可以说每个人都要经历.而我这篇博客,就是结合我最近几天的经历,来谈谈从服务器到iOS客户端对ATS的适配 ...
- Spring_手动获取Bean
1.SpringContextHolder.java package com.lkb.util; import org.springframework.context.ApplicationConte ...
- ORACLE 查看锁
SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$session s W ...
- [LintCode] Parking Lot 停车场问题
Design a parking lot. see CC150 OO Design for details.1) n levels, each level has m rows of spots an ...
- HDU1892二维树状数组
See you~ Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Su ...
- JS(javascript) 将网站加入收藏夹
| 浏览:688 | 更新:2014-09-20 19:39 1 2 3 分步阅读 将网站网址加入收藏夹,方便下次访问! 工具/原料 网址: 电脑. 方法/步骤 //创建加入收藏夹JS函数 < ...
- Spring中的单例一二
Spring框架很好的帮助我们创建和管理dao.bean.service.action等对象, 但是它创建的对象是单例呢还是多例,又有哪些区别以及为什么 1.在Spring中默认创建的是单例模式,简单 ...