开发者

If I am not allowed to use the Singleton pattern, what are the options? [duplicate]

This question already has answers here: Closed 12 years ago.

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?


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


"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.


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 : 
{
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜