Design Pattern Snippets

## Singleton: A singleton stores common data in only one place.

public sealed class SiteStructure
{
object[] _data = new object[10];

static readonly SiteStructure _instance = new SiteStructure();

public static SiteStructure Instance
{
get { return _instance; }
}

private SiteStructure()
{
// Initialize members here.
}
}

===================

Best Practice: Use Lazy<T> to init a singleton as descibed in Wiki article.​

In .NET Framework 4.0, the Lazy<T> class was introduced, which internally uses double-checked locking by default​.

https://en.wikipedia.org/wiki/Double-checked_locking​​

Usage examples:

​Singleton – Logger, LoadBalancer

​​Creational: AbstractFactory, ​​​​Singleton, Builder

Structural: Adapter, Decorator, Facade, Proxy

​Behavioural: ​Observer, Strategy, ​​​Iterator

http://www.dofactory.com/net/design-patterns

Leave a Reply