-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsingleton.lua
More file actions
36 lines (28 loc) · 743 Bytes
/
singleton.lua
File metadata and controls
36 lines (28 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
local function Class(super)
local obj = {}
obj.__index = obj
setmetatable(obj, super)
function obj.new(...)
if obj._instance then
return obj._instance
end
local instance = setmetatable({}, obj)
if instance.ctor then
instance:ctor(...)
end
obj._instance = instance
return obj._instance
end
return obj
end
local class = Class()
local a = class.new()
local b = class.new()
a.name = "Singleton Pattern"
print(a.name)
print(b.name)
---------------------------------------------------------------------------
-- OUTPUT
---------------------------------------------------------------------------
-- Singleton Pattern
-- Singleton Pattern