- A singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance.
- The Singleton pattern ensures that a class only has one instance and provides a global point of access to it from a well-known access point.
- The class implemented using this pattern is responsible for keeping track of its sole instance rather than relying on global variables to single instances of objects.
- Most commonly, singletons don't allow any parameters to be specified when creating the instance.
C# Coding:
namespace DesignPatterns
{
public sealed class Singleton
{
private static readonly Singleton
instance = new Singleton();
private Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
}
// code to access the Singleton.
using System.Diagnostics;
using DesignPatterns;
Trace.WriteLine(Singleton.Instance.ToString());
No comments:
Post a Comment