Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello All

I become confused where to implement Singleton and where to use the Factory.
Please help me to Solve this issue .
Posted
Comments
Sergey Alexandrovich Kryukov 14-Mar-11 16:59pm    
This is nothing to be solved. It's just learning of the concepts.
--SA

Hi

Go through this design_patterns[^], You will good idea. Very good one for beginners.

[Update]
Similar discussion on stackoverflow.com,
When to use the singleton[^]
When to use factory methods?[^]

Some other articles,
Pattern Singleton[^]
Pattern Factory[^]
 
Share this answer
 
v2
Comments
Henry Minute 14-Mar-11 13:35pm    
That's a nice link and I've bookmarked it.
I don't think that it answers the OPs question though, bearing in mind that they (say they) are confused about when to use them not what they are.
TweakBird 14-Mar-11 13:42pm    
I thought on OP: He need some samples (available there, i.e source code with C#), by doing practice OP can learn about design patterns usage. with out practice design patterns always tough to understand & easily got confused. ok thanks, i will update my answer as per your suggestions.
Albin Abel 14-Mar-11 14:04pm    
My 5, the first link is good one
TweakBird 14-Mar-11 14:18pm    
Thank you AlbinAbel.
Sergey Alexandrovich Kryukov 14-Mar-11 17:03pm    
Good and useful references, my 5, but I prefer mine, please see :-)
--SA
E$w@r's link is a good one. In a nutshell Singletons are generally used prevent repeated load of a heavy resource in memory. Say class A has a big common dataset which is reusable. If many other objects access various instance of this object then it is a wastage of memory, because the same type of data repeated in memory. So singleton pattern prevents the multiple instance of it.

Singleton can be used to keep global data as well, if static members not satisfied.

Factory is by the name itself it explains creation of objects. Basically if you create an object by hardcode inside the class then your class need to be edited when a new type of objects need to create in a new condition. Instead of that you can separate the object creation to a factory and delegate the creation work to it. So any changes in the object creation will impact on the factory class coding, but no need to modify the classes which using the factory. Once the new condition is passed to the factory, the factory knows which object has to instantiate to do the job. The caller class doesn't need to worry about which object has to instantiate.

This is just a nutshell explanation


There are lot of good material available in internet. You can go through it.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Mar-11 17:02pm    
Good answer, my 5, but I prefer mine, please see :-)
--SA
Espen Harlinn 15-Mar-11 11:51am    
Nice points, 5!
Albin Abel 15-Mar-11 12:56pm    
Thanks Espen Harlinn
The Question in not completely incorrect, but can be greatly improved. You should have re-formulated it:

"How to use Wikipedia, Google, CodeProject and StackOverflow.com?"
If you asked and got good Answers, the Questions like your original one would not be needed!

—SA
 
Share this answer
 
Comments
Albin Abel 14-Mar-11 23:18pm    
For me Wikipedia is preferable. Google is helper, Stackoverflow sometimes. 5 for google, 5 for wikipedia and 3 for stack overflow. Give vote for my question about how i can give 13 for this?
Sergey Alexandrovich Kryukov 14-Mar-11 23:25pm    
For me, too. Ordered by my preference... but in fact the order depends on the question. On concrete stuff, some tricks better to start with Google.
Thank you.
--SA
Espen Harlinn 15-Mar-11 11:53am    
5 - a Lazy<5> :)
Sergey Alexandrovich Kryukov 15-Mar-11 12:59pm    
Laziness is a driving force of progress.
Thank you.
--SA
Here is a Lazy<Answer> :)
System.Lazy<T>[^]

Implements the singleton pattern in .Net ...

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Abhinav S 15-Mar-11 12:28pm    
Nice little link :). 5.
Espen Harlinn 15-Mar-11 13:50pm    
Thanks Abhinav!
Albin Abel 15-Mar-11 12:58pm    
Good Notification. Thanks and 5
Espen Harlinn 15-Mar-11 13:50pm    
Thanks AlbinAbel!
Sergey Alexandrovich Kryukov 15-Mar-11 16:01pm    
Lazy again... my 5. Good way of being lazy!
--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