{"id":8126,"date":"2011-08-12T05:40:55","date_gmt":"2011-08-12T05:40:55","guid":{"rendered":"https:\/\/www.techopedia.com\/definition\/singleton\/"},"modified":"2011-08-18T17:21:32","modified_gmt":"2011-08-18T17:21:32","slug":"singleton","status":"publish","type":"definition","link":"https:\/\/www.techopedia.com\/definition\/15830\/singleton","title":{"rendered":"Singleton"},"content":{"rendered":"
A singleton is a class that allows only a single instance of itself to be created and gives access to that created instance. It contains static variables that can accommodate unique and private instances of itself. It is used in scenarios when a user wants to restrict instantiation of a class to only one object. This is helpful usually when a single object is required to coordinate actions across a system.<\/p>\n
The singleton pattern is used in programming languages such as Java and .NET to define a global variable. A single object used across systems remains constant and needs to be defined only once rather than many times.<\/p>\n
A singleton is intended to provide only one instance of itself while facilitating a global point of access. Implementing a singleton pattern involves creating a class with a method that creates a new instance of the class. In order to implement a singleton pattern, principles of single instance and global access must be satisfied. The singleton class is like a global repository for an instance of itself, making the constructor private. Therefore, an instance outside the class cannot be created at all, and a singleton can contain only one instance. A singleton class instantiates itself and maintains that instance across systems. <\/p>\n
Abstract factory, builder and prototype patterns can use singletons. Fa\u00e7ade objects and static objects are often singletons. Singleton implementation requires a mechanism through which a class member can be accessed without having to create a class object and hold on to the value of class members among class objects. The steps involved in creating a singleton are as follows:<\/p>\n
Example:
class testdata
{
Private testdata ()
{
\/\/\u2026 no-op for a singleton<\/p>\n
Example:<\/p>\n
\/\/Lazy creation of singleton internal instance
Public static testdata Instance
{
Get
{
Lock(type of (testdata) )
{
If (_instance==null)
_instance = testdata ();
}
Return _instance;
}
}<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"
What Does Singleton Mean? A singleton is a class that allows only a single instance of itself to be created and gives access to that created instance. It contains static variables that can accommodate unique and private instances of itself. It is used in scenarios when a user wants to restrict instantiation of a class […]<\/p>\n","protected":false},"author":7813,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"_acf_changed":false,"_lmt_disableupdate":"","_lmt_disable":"","om_disable_all_campaigns":false,"footnotes":""},"definitioncat":[273,244,216],"class_list":["post-8126","definition","type-definition","status-publish","format-standard","hentry","definitioncat-devops","definitioncat-programming-languages","definitioncat-software-development"],"acf":[],"yoast_head":"\n