C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Singleton design pattern code example


Posted Date: 24 Jan 2008    Resource Type: Code Snippets    Category: Design Patterns

Posted By: Krishna Prasad RVS       Member Level: Gold
Rating:     Points: 10



Ensure a class has only one instance and provide a global point of access to it.





Example 1:

using System;

namespace DoFactory.GangOfFour.Singleton.Structural
{

// MainApp test application

class MainApp
{

static void Main()
{
// Constructor is protected -- cannot use new
Singleton s1 = Singleton.Instance();
Singleton s2 = Singleton.Instance();

if (s1 == s2)
{
Console.WriteLine("Objects are the same instance");
}

// Wait for user
Console.Read();
}
}

// "Singleton"

class Singleton
{
private static Singleton instance;

// Note: Constructor is 'protected'
protected Singleton()
{
}

public static Singleton Instance()
{
// Use 'Lazy initialization'
if (instance == null)
{
instance = new Singleton();
}

return instance;
}
}
}



Example2:

///
/// Class implements singleton pattern.
///

public class Singleton
{
// Private constructor to avoid other instantiation
// This must be present otherwise the compiler provide
// a default public constructor
private Singleton()
{
}

///
/// Return an instance of
///

public static Singleton Instance
{
get
{
/// An instance of Singleton wont be created until the very first
/// call to the sealed class. This a CLR optimization that ensure that
/// we have properly lazy-loading singleton.
return SingletonCreator.CreatorInstance;
}
}

///
/// Sealed class to avoid any heritage from this helper class
///

private sealed class SingletonCreator
{
// Retrieve a single instance of a Singleton
private static readonly Singleton _instance = new Singleton();

///
/// Return an instance of the class
///

public static Singleton CreatorInstance
{
get { return _instance; }
}
}
}





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: To set the maximum length to Text Area in ASP
Previous Resource: Singleton Pattern in C#
Return to Discussion Resource Index
Post New Resource
Category: Design Patterns


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

efax

Contact Us    Privacy Policy    Terms Of Use