Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello frends,

can anybody provide me any kind of small project which will teach me how to implement Singleton Design pattern

[Addition]
OP Also wants a thread safety in that project. :-D
[/addition]
Posted
Updated 22-Dec-10 20:29pm
v2

Hi,

Refer this article. THIS[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-Dec-10 1:47am    
Sorry, the sample in this article is not thread-safe, but thread-safety is much more important for singletons than anywhere else.
what about  WikiPedia[^] ?
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-Dec-10 2:28am    
Good point. Authors of too many questions are too lazy to read even Wikepedia (most articles are thoroughly accurate), so one would wonder why they think pretty fast answer in CodeProject will be easier. Unfortunately, I can see many could not even use the help. Why asking?
nilesh.d.mankar 23-Dec-10 3:17am    
I agree with you hiren,But I m new to DESIGN patterns, i Dont know why people dont use help they asking for,I always use,I think all d peoples on codeproject having lots of experience r ready to help others, so I m using their knowledge
 
Share this answer
 
Comments
nilesh.d.mankar 23-Dec-10 4:39am    
thanks thatraja
Unfortunately, the sample in this article (I refer to advice of vivekse) is not thread-safe, but thread-safety is much more important for singletons than anywhere else.

Look at this article: http://www.dofactory.com/Patterns/PatternSingleton.aspx#_self1[^].

Locate the class Singleton and look at the "not thread safe" comment:

C#
class Singleton {
   private static Singleton _instance;
   protected Singleton() { }
   public static Singleton Instance() {
         // Uses lazy initialization.
         // Note: this is not thread safe.
         if (_instance == null)
            _instance = new Singleton();
         return _instance;
   } //Instance
} //Singleton


It looks like this comment and its placement suggests that thread safety could be provided by locking of the method returning its instance (I will also improve usability by converting it to a property:

C#
class Singleton { //the class better be non-static
   private static Singleton _instance;
   private static object SignletonLock = new object();
   protected Singleton() { }
   public static Singleton Instance() {
      get {   // Uses lazy initialization.
         // Note: this is not thread safe.
         if (_instance == null)
            lock(SignletonLock)
               _instance = new Singleton();
         return _instance;
      } //get Instance
   } //Instance
} //Singleton


Will it resolve the issue? Of course not!
In real life class Singleton is not such an empty class, it has to have members which might be accessed from different threads.
As this is a singleton, all threads will work with the same object, accessing same very members of the same instance in competition.

So, to make it thread safe, one need to make sure every shared resource in the singleton instance is interlocked. It will require one or more extra lock objects like SingletonLock.

Better yet, you may need to use System.Threading.ReaderWriterLockSlim instead of simple lock. This is important when threads often need read access to some singleton and much less need write (modification) access. This situation is quite typical, and ReaderWriterLockSlim deals with it with much better thoughput, because multiple threads can really allowed to read at the same time. Please see Microsoft help on System.Threading.ReaderWriterLockSlim complete with very clear code sample.
 
Share this answer
 
Comments
vivekse 23-Dec-10 2:26am    
Great explanation :) thanks.
Sergey Alexandrovich Kryukov 25-Dec-10 3:30am    
Thank you. I think we complemented our answer, so it gives quite enough matter to learn and use the pattern. I voted 5 for your answer despite the lack of threading consideration (which was honestly commented in the code you reference), will you consider your vote as well.

By the way, why our inquirer is not accepting the answers? any further questions? anything unclear?
Sergey Alexandrovich Kryukov 11-Apr-11 16:04pm    
Thank you very much.
--SA
Espen Harlinn 26-Feb-11 11:07am    
Good reply - a 5
Sergey Alexandrovich Kryukov 26-Feb-11 19:26pm    
Interesting you've found quite a number of my very detailed Answers, like this one, which were apparently ignored.
Thank you very much.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900