Singleton: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "{{Useful Class}} {{Needs_Example}} <lowercasetitle/> __NOTOC__ This class allows you to restricts the instantiation of a specific class to one object. ==Requirements== sbx3...")
 
mNo edit summary
Line 4: Line 4:
__NOTOC__
__NOTOC__


This class allows you to restricts the instantiation of a specific class to one object.
This class allows you to restrict the instantiation of a specific class to one object.


==Requirements==
==Requirements==

Revision as of 20:58, 14 February 2015


Accessories-text-editor.png Script Example Missing Function Singleton needs a script example, help out by writing one.

Before submitting check out Editing Guidelines Script Examples.


This class allows you to restrict the instantiation of a specific class to one object.

Requirements

sbx320's classLib, can be found Here[1]

Code

Singleton = {}

function Singleton:getSingleton(...)
	if not self.ms_Instance then
		self.ms_Instance = self:new(...)
	end
	return self.ms_Instance
end

function Singleton:new(...)
	self.new = function() end
	local inst = new(self, ...)
	self.ms_Instance = inst
	return inst
end

function Singleton:isInstantiated()
	return self.ms_Instance ~= nil
end

function Singleton:virtual_destructor()
	for k, v in pairs(super(self)) do
		v.ms_Instance = nil
		v.new = Singleton.new
	end
end

Original Author: Jusonex

Example

Click to expand [+]
Example

See Also

  • Singleton » This class allows to restrict the instantiation of a specific class to one object.
  • CThread » This class represents a simple coroutine manager which can be used to limit method calls / loop.
  • Importer » This class make easy to use exported functions.
  • Observable » Observable variables. Call function on variable value change.
  • MatrixPOP » This class allows to use simple matrix without using MTA's OOP functions