Skip to content
Roland edited this page Sep 27, 2016 · 2 revisions

30log can be used to emulate the singleton pattern. A reference implementation is shipped with the library. Find it in the file 30log-singleton.lua.
This singleton class implementation is named "Singleton", by default.

local singletonClass = require '30log-singleton'
print(singletonClass.name) -- output "Singleton"

It cannot instantiate. Any attempt to do this raises an error.

local instance = singletonClass() -- raises an error!

Also, it cannot create subclasses. Any attempt to do this raises an error.

local subclass = singletonClass:extend() -- raises an error!

It implements a method named :getInstance(), which returns a private instance from this class.

local singleton = singletonClass:getInstance()
print(singleton.class) -- outputs "class 'Singleton' (table: 0x002bc520)"
Clone this wiki locally