December 2012 - Posts

30 December 2012
Singleton Pattern Implementation in C#: One Global Object Instance, One Entry Point
This post is going to explain how to implement the Singleton Pattern in .Net/C#. In most other programming languages/frameworks, global variables are allowed. In modern day programming this is extremely frowned upon, hence where this design pattern comes in handy: It provides a global object with only one point of reference. Once it is instantiated, all references to it are guaranteed to be to a single object instance which is available throughout the lifetime of the application. So, where would this be useful? Keeping an application-wide cache of objects to avoid expensive calls to non-local resources. .Net makes this fairly simple. Here’s the basic UML diagram for a singleton object: We create a static constructor (thus making sure only one object is created in the AppDomain) so the object can’t be created directly, a private backing store called instance of type of the singleton class, and a public accessor property (or method) that returns the private instance type. Your... Read More...