5

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.

radu florescu
  • 171
  • 1
  • 1
  • 6
  • 7
    Personally I would use an IoC container and let that manage the objects lifetime. – MattDavey Mar 23 '12 at 08:59
  • See also http://programmers.stackexchange.com/questions/56674/could-a-singleton-type-replace-static-methods-and-classes – JBRWilkinson Mar 23 '12 at 09:13
  • 2
    Why your choice is between two bad patterns only? http://misko.hevery.com/2008/08/25/root-cause-of-singletons/ – OZ_ Mar 23 '12 at 09:35

3 Answers3

8

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.

  • How does inheritance make sense on a singleton? – tdammers Mar 23 '12 at 09:44
  • 2
    @tdammers a abstract superclass with templated methods and the concrete subclass dynamically chosen depending on the environment/settings – ratchet freak Mar 23 '12 at 09:53
  • @ratchetfreak: I'd prefer an interface for that... – tdammers Mar 23 '12 at 09:57
  • 1
    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: https://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 09 '13 at 06:56
5

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.

DXM
  • 19,932
  • 4
  • 55
  • 85
  • 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
  • 1
    @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 09 '13 at 07:00
1

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.

RPK
  • 4,378
  • 11
  • 41
  • 65