Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
Hai All


I am new to design patterns could any one help with the sample of "singleton". I am i need sample which will use singleton method. I need a SQL Access handler using singleton method.
Posted

The MSDN documentation should be of great help if you're trying to implement the singleton pattern in .net:

http://msdn.microsoft.com/en-us/library/ff650316.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 14:17pm    
Trivial enough, useful enough. My 5, but this time I like the Answer by Bob more, please see.
--SA
Here is an excellent article regarding the Singleton Pattern.
The author has given various aspects of implementing the Singleton pattern and provides you the best way to use it!
http://csharpindepth.com/Articles/General/Singleton.aspx[^]

Here is another sample from MSDN :
http://msdn.microsoft.com/en-us/library/ee817670.aspx[^]

Good luck!
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 14:17pm    
Same thing: simple and useful enough. My 5, but this time I like the Answer by Bob more, please see. --SA
Tarun.K.S 12-Apr-11 14:54pm    
Thanks SA, I had a look. He is right but don't know what's going on in OP's mind.
Sergey Alexandrovich Kryukov 12-Apr-11 14:58pm    
"We're not the mind readers" should be a part of many Answers!
--SA
Tarun.K.S 12-Apr-11 15:02pm    
Yeah!
You might find this[^] discussion useful.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 14:18pm    
Same thing: simple and useful enough. My 5, but this time I like the Answer by Bob more, please see. --SA
See my answer to this question here: how to open only one instance of class not more than that ?[^]. It follows the singleton pattern.

Best Regards,

-MRB
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 14:19pm    
That's good, too, my 5, but this time I like the Answer by Bob more, please see.
--SA
Some good answers here, but I would answer your question with a question. If you're new to design patterns, how are you so sure you want a singleton to solve this problem? An application could easily want to read from multiple databases at the same time, for instance. It is quite a common beginner mistake to see singletons everywhere where they aren't really necessary, and this might be one of those.

You can make sure you only have one instance by the simple mechanism of only creating one instance. Singletons should be used where it makes no sense to have more than one instance of an object, ever, which is fairly unusual.

Your question is a bit vague and I'm not entirely sure what you mean by a 'SQL Access handler' so perhaps you do need a singleton, and if so, refer to one of the articles linked in the other solutions. But before deciding to use a pattern you should always make sure that it is actually what you need for the problem you are trying to solve.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 14:16pm    
Good point. My 5+++++++ for sure. One of my favorite ideas (but not my idea) is "Don't be a problem solver!". I think you understand it.
Respect,
--SA
Tarun.K.S 12-Apr-11 15:01pm    
Some important points are pointed out here.

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