Pattern: Singleton
Singleton pattern restricts the instantiation of a class to one object.
It is used when you want to have one instance of a class that is shared across the application.
Purpose of .NET implementation
You have a factory to create objects.
You want to have a single point of creation for all the objects.
You can use the Singleton pattern to ensure that only one instance of the factory is created and that all requests for object creation are handled by that single instance.
Example in .NET :
Singleton
Singleton example for Pattern Singleton
using System.Data.OleDb;
namespace Singleton;
/// <summary>
///
///sealed class Singleton
///{
/// private Singleton() { }
/// public static readonly Singleton Instance = new Singleton();
///}
///
/// </summary>
internal class SingletonDemo
{
public static void GetFactory()
{
//cannot do new
//OleDbFactory factory=new OleDbFactory();
//get singleton instance
OleDbFactory factory = OleDbFactory.Instance;
}
}
See Source Code for Microsoft implementation of Singleton
SourceCode OleDbFactory.InstanceLearn More
C2Wikidofactory
DPH
Wikipedia
Homework
Implement a singleton that will allow you to create a single instance of a logger that logs to a file and to a console.