Singleton Pattern Code Review
Define the Singleton Object
Code Download
- Download Description:singleton pattern download
- .NET Framework:3.5
- .NET Language:C#
- Date Published:2009-07-01
- Download Size:8 KB
Code Review
- Singleton Pattern Code Review: Define the Singleton Object
- Singleton Pattern Code Review: Use the Singleton Pattern
Code Walkthrough
Define the Singleton class with the required getInstance() method used to create a single instance of the class (object).
class Singleton
{
private static Singleton instance = null;
private static Object syncLock = new Object();
public String Item { get; set; }
private Singleton() {}
public static Singleton getInstance() { if (instance == null) { lock (syncLock) { if (instance == null) { Console.WriteLine("****** Creating Singleton instance ******"); instance = new Singleton(); } } } return instance; } }
public String Item { get; set; }
private Singleton() {}
public static Singleton getInstance() { if (instance == null) { lock (syncLock) { if (instance == null) { Console.WriteLine("****** Creating Singleton instance ******"); instance = new Singleton(); } } } return instance; } }
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!