为什么使用singlton模式?而如果我们可以通过静态类做同样的事情? [英] Why singlton pattern use ? while if we can do the same by Static class?

查看:113
本文介绍了为什么使用singlton模式?而如果我们可以通过静态类做同样的事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们必须在mvc框架中使用Singlton模式?如果我们可以通过静态类和静态方法做同样的事情。



那么SINGLTON和STATIC CLASS有什么区别?



在哪种情况下我们可以使用singlton模式和什么好处。

解决方案

Google是你的朋友:好好经常拜访他。他可以比在这里发布问题更快地回答问题...



使用部分问题快速搜索结果超过100,000: Google:SINGLTON& STATIC CLASS 之间的差异[ ^ ] - 其中大部分都是解释。



将来,请尝试自己做至少基础研究并且不要浪费你的时间或我们的时间。


让我们看看。也许你认为单身人士模式是好的。我不同意这一点 - 它最多只能容忍可以,不应该过度使用。单例对象为您提供严苛的麻烦,特别是在多线程中。是的,我自己使用这种模式,但仅限于特殊的,独特的情况。



现在,人们可以用不同的方式实现单身,好的和坏的。静态类是禁止好方法的类,只允许坏类。为什么?非静态类允许在类中拥有任何成员,静态和非静态(实例)一次。静态类仅限于静态成员。您将无法使用任何实例成员。但为什么你需要使用实例成员?好吧,为了使单例实现更加令人厌恶。怎么样?以下是良好实施的示例: http://csharpindepth.com/Articles/General/Singleton.aspx [ ^ ]。 (参考 http://en.wikipedia.org/wiki/Singleton_pattern [ ^ ]。)



请同时阅读文章。阅读之后,你应该明白为什么你使用静态类作为单身的想法是如此糟糕,与更好的实现相比。



- SA

Why we have to use Singlton pattern in mvc framework ? while if we can do the same by Static class & static method.

So what is the difference between SINGLTON & STATIC CLASS ?

In which scenario we can use the singlton pattern & whats benefits.

解决方案

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search using part of your question gave over 100,000 results: Google: "difference between SINGLTON & STATIC CLASS[^] - most of which are explanations.

In future, please try to do at least basic research yourself, and not waste your time or ours.


Let's see. Probably you think that singleton pattern is something good. I would not agree with that — it is at best only tolerable, and should not be overused. The singleton objects gives you draconian hassles, notably in multithreading. Yes, I use this pattern myself, but only in special, exclusive situations.

Now, one can implement singletons in different ways, good and bad. Static class is the class which would prohibit the good ways, will allow only bad ones. Why? Non-static class allows to have any member in class, static and non-static (instance) once. A static class limits you to static members only. You would not be able to use any instance members. But why would you need to use an instance member? Well, to make the singleton implementation a bit less disgusting that it could be. How? Here is the example of good implementation: http://csharpindepth.com/Articles/General/Singleton.aspx[^]. (Referenced from http://en.wikipedia.org/wiki/Singleton_pattern[^].)

Please read the both articles. After reading then, you should come to understanding why your idea of using of the static class as a singleton was so bad, compared to better implementation.

—SA


这篇关于为什么使用singlton模式?而如果我们可以通过静态类做同样的事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆