You may get some of the good information about Design patterns with Software design pattern and .NET Design Patterns.
Here just I want to say few tricks about single ton and implementation at runtime.
Familiar and Traditionally,
- NormalProgram objProgram = NormalProgram.Singleton();
- NormalProgram objProgram2 = NormalProgram.Singleton();
- if(objProgram == objProgram2)
- {
- Console.WriteLine("Instances matched");
- }

Simple we can call and implement and change patterns modeling but intention is to be making ‘Instance must be same’.
We can change Pattern style not behavior
The following code snippets talk about properties in the class. I will demonstrate by using Lazy Keyword.

- public class DemoProgram
- {
- public static readonly Lazy < DemoProgram > Demo = new Lazy < DemoProgram > (() => new DemoProgram());
- public static DemoProgram _instance
- {
- get
- {
- return Demo.Value;
- }
- }
- public DemoProgram()
- {
- Console.WriteLine("Lazy Singleton acheived");
- }
- public static void TestMethod()
- {
- Console.WriteLine("Sample Method call from Singleton Class");
- }
- public static DemoProgram SngltonMethod()
- {
- return _instance;
- }
- }

In new approach no bother about conditions and checking’s. But it is little lazy than traditional.

Join the conversation! Your thoughts help the community grow.