Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Possible Duplicate:
What is the difference between all-static-methods and applying a singleton pattern?

I need to make a decision for a project I'm working of whether to use static or singleton.

After reading an article like this I am inclined to use singleton.

What is better to use static class or singleton?

Edit 1 : Client Server Desktop Application.

Please provide code oriented solutions.

share|improve this question
7  
Personally I would use an IoC container and let that manage the objects lifetime. –  MattDavey Mar 23 '12 at 8:59
    
2  
Why your choice is between two bad patterns only? misko.hevery.com/2008/08/25/root-cause-of-singletons –  OZ_ Mar 23 '12 at 9:35
add comment

marked as duplicate by ChrisF Mar 23 '12 at 10:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 7 down vote accepted

Your code will be more flexible if you use a singleton.

The advantage is that the code that uses the singleton doesn't need to know if it is a singleton or a transient object. Using a static class means you have to call static methods explicitly. Think about dependency injection, for example.

Moreover, singleton, as a non-static class, can still implement an interface, supports inheritance, etc. For example, you can't inherit a static class and override a virtual method in it.

share|improve this answer
    
How does inheritance make sense on a singleton? –  tdammers Mar 23 '12 at 9:44
1  
@tdammers a abstract superclass with templated methods and the concrete subclass dynamically chosen depending on the environment/settings –  ratchet freak Mar 23 '12 at 9:53
    
@ratchetfreak: I'd prefer an interface for that... –  tdammers Mar 23 '12 at 9:57
    
tdammers, Singleton can be developed just as a normal object. Any class can be made singleton. When using an IoC Container, a class can be made singleton wihtout changing a single line of code. (more info: github.com/ninject/ninject/wiki/Object-Scopes ) Ofcourse you do not need an IoC Container to implement singleton. All I am saying is that a singleton object does support inheritance, since it is just a normal class, which is decorated with a static getter (and a static private field) and a private/protected constructor (protected for inheritance ;-) ) –  Frederik P. Jul 9 '13 at 6:56
add comment

You have not mentioned whether it is a Web application or a standalone desktop application.

If it is a Web application, Static Classes can create some major problems in a concurrent environment (because of shared data), specially when you are also using static variables and static methods.

share|improve this answer
5  
No more or less than a singleton. –  pdr Mar 23 '12 at 8:53
add comment

I agree with everything Martin said, +1 to that. To add a little, also consider that today your application has a global shared object and tomorrow you decide that you want to have more than one instance of it.

If you use a singleton, all you need to do is make your constructor public. But if you decide to use a static class, the change would have to be much more intrusive.

Also if you ever decide to add unit tests to your code, replacing a singleton instance with a fake one is much easier (change in one place) compared to having to deal with a whole bunch of static functions that all share global data.

share|improve this answer
    
I like your point about the possibility of a singleton possibly becoming a non-singleton in the future - but after thinking about it, seems like YAGNI to me. I can't think of a time that has ever happened to me nor can I imagine a scenario where it would. –  Dan Ling Dec 20 '12 at 13:54
    
@DanLing: This wasn't exactly my point but a point from several design patterns books and blogs. I think YAGNI is more when you put in a whole bunch of extra work for something that will never be useful. Whereas here, if you follow this pattern and structure the code differently, it gives you more options for the future with virtually no extra effort. Having options is always good and much better when you don't have to pay for them. –  DXM Dec 20 '12 at 14:08
    
YAGNI does not always mean "You need more work to get it done". Sometimes you write things, which result it YAGNI but never the less it costed the exaxt amount of time to develop it. But by doing it this way, you give the opportunity to change it quickly if needed. I've had situations where i.e. you want to switch from a singleton EF Context implementation to a new instantation per file... When using an IoC Container this is even easier then DXM describes! –  Frederik P. Jul 9 '13 at 7:00
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.