开发者

singleton patterns VS static types [duplicate]

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

Pos开发者_高级运维sible Duplicate:

Advantage of Static class over use of Singleton

Usually, everytime I needed a single systemwide object I used the singleton pattern. M question is, why shouldn't i just implement the object as static and get the single object behaviour naturally ? Are there any cons to use static types over singleton factored object?


A static type is a lot less (unit) testable. Singletons can be passed around (as parameters) and inherited.

For examples etc see this article:

http://dotnetperls.com/singleton-static

In general you should avoid systemwide objects, as these suggest you have global state. Singleton's are ofter used to manage access to a shared resource (rather than state).

I believe at lest one of the GoF is on record as saying that including singleton's in their book was a mistake and in many cases it's used as an anti-pattern.


depends on what you want to do with the object. if it's just calling methods on it then just use a static. if you want to pass the object around, and do object oriented stuff with it, i.e polymorphism then do it the object way.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜