i want to know about sigleton design pattern in dot net

Posted by Subhadip under ASP.NET on 3/12/2011 | Points: 10 | Views : 1283 | Status : [Member] | Replies : 2
i want to know about sigleton design pattern in dot net used in presentation layer,busines layer,data access layer,cache layer




Responses

Posted by: Raja on: 3/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Subhadip,

There are few already articles written here on Singleton design pattern, read them and I am sure that will help you in understanding it.

http://www.dotnetfunda.com/articles/article69.aspx
http://www.dotnetfunda.com/articles/article889-design-pattern-implementation-using-csharp-.aspx

Thanks

Regards,
Raja, USA

Subhadip, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gsolvers on: 3/13/2011 [Member] Starter | Points: 25

Up
0
Down
Singleton pattern forces the single instance creation of a class. Even if you initialize many times you will end up in having only single instance. See the code below

using System;



namespace DoFactory.GangOfFour.Singleton.Structural

{

/// <summary>

/// MainApp startup class for Structural

/// Singleton Design Pattern.

/// </summary>

class MainApp

{

/// <summary>

/// Entry point into console application.

/// </summary>

static void Main()

{

// Constructor is protected -- cannot use new

Singleton s1 = Singleton.Instance();

Singleton s2 = Singleton.Instance();



// Test for same instance

if (s1 == s2)

{

Console.WriteLine("Objects are the same instance");

}



// Wait for user

Console.ReadKey();

}

}



/// <summary>

/// The 'Singleton' class

/// </summary>

class Singleton

{

private static Singleton _instance;



// Constructor is 'protected'

protected Singleton()

{

}



public static Singleton Instance()

{

// Uses lazy initialization.

// Note: this is not thread safe.

if (_instance == null)

{

_instance = new Singleton();

}



return _instance;

}

}

}


Best Regards,

VG
www.TeacherJi.com

Subhadip, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response