Singleton-Design patterns- step by step explanation with simple examples for beginners to intermediate
Singleton-Design patterns - Software Design patterns - Design patterns in C#.NET - easy simple example
what is Singleton design pattern
In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the system. The term comes from the mathematical concept of a singleton.
using System;
namespace SingleTonnDesignPatternExample
{
class Logger
{
private static Logger logger;
private Logger()
{
}
public static Logger GetInstance()
{
if (logger == null)
{
logger = new Logger();
}
return logger;
}
}
class Program
{
static void Main(string[] args)
{
Logger logg = Logger.GetInstance();
}
}
}
what is Singleton design pattern
In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the system. The term comes from the mathematical concept of a singleton.
using System;
namespace SingleTonnDesignPatternExample
{
class Logger
{
private static Logger logger;
private Logger()
{
}
public static Logger GetInstance()
{
if (logger == null)
{
logger = new Logger();
}
return logger;
}
}
class Program
{
static void Main(string[] args)
{
Logger logg = Logger.GetInstance();
}
}
}
Comments
Post a Comment