1

Possible Duplicate:
What's Alternative to Singleton

If I am not allowed to use the Singleton pattern because it is not good OOP, what are my options? I often have a lot of stuff that needs quick access. If I use a Singleton I am told it is not good OOP, and if I reference the stuff I get a lot of classes with to much references.

Any idéas?

Community
  • 1
  • 1
Mattias
  • 3,907
  • 4
  • 28
  • 50
  • 1
    Who said Singleton isn't good OOP? It's just not recommended in some cases. – Michael Todd Oct 12 '10 at 21:22
  • 2
    The singleton pattern is just another pattern - by and of itself its not 'bad OOP'. What you need to think about is the specifics of what you are doing, and find the pattern that fits that - what type of application are you developing? – Assaf Oct 12 '10 at 21:22
  • 1
    I personally believe the singleton patter is a great OOP in cases where the object you are modeling can never have more than one instance. – Brad The App Guy Oct 12 '10 at 21:25
  • Many people smarter than me avoid it, calling it a [liar](http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-join-new-project.html) and [stupid](http://sites.google.com/site/steveyegge2/singleton-considered-stupid). – jball Oct 12 '10 at 21:26
  • @jball: Hmm, I just read the "liar" article. I don't see how the problems he describes have anything to do with singletons: they're all about code that hides the relationships between objects. At best he's describing problems with using global data, of which singletons are just one example. But you could get exactly the same issues he describes without using singletons or global data. Suppose his credit card object creates a new connection every time you make a charge? It could connect, authorize, charge, close, dispose of object. – Jay Oct 12 '10 at 21:44
  • What's wrong with a lot of references? It's better OOP than using a mess of Singletons with all their downsides. – Amardeep AC9MF Oct 12 '10 at 21:46
  • @Jay, of course there are many ways to write bad code. Some ways of writing code just lead to problems more easily than others. – jball Oct 12 '10 at 21:59

3 Answers3

5

Consider using Inversion of Control via Dependency Injection. This can often be used in many places where people reach for Singletons.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

"It isn't good OOP" is not a good reason to reject a coding pattern. OOP is good for certain things, but most definitely not for all things.

I'm not a big fan of Singleton myself, tbh, but not because it's "not good OOP", but rather because it has many of the same drawbacks as global variables, plus extra object gunk. I'd have to know more about the structure of your application to give advice on what to use instead, though.

zwol
  • 135,547
  • 38
  • 252
  • 361
0

You could always go to a more service oriented approach and host a service that could defined as a Singleton to process various work items for multiple callers...

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class MySingleton : 
{
}
fdfrye
  • 1,099
  • 7
  • 14