为什么Singleton被认为是反格局? [英] Why is Singleton considered an anti-pattern?

查看:241
本文介绍了为什么Singleton被认为是反格局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

对单身人士来说如此糟糕?

Singleton设计模式:陷阱

Singleton反模式

我听说过最近Singleton是一个反模式。我知道这与一个类单例的事实有关,就是使这个唯一实例成为一个全局变量,但它也做了很多事情(限制该对象的实例数量,管理实例化等)。

I've heard recently that Singleton is an anti-pattern. I know it has to do with the fact making a class singleton is like making that unique instance a global variable, but it's also doing a lot more than that (limiting the number of instances of that object, managing instantiation, etc..).

为什么单身人士被认为是反模式?有什么选择?

Why exactly is Singleton considered an anti-pattern? And what are the alternatives?

推荐答案

为了帮助回答,这里有更多关于反模式评论:

To help with answering, here is more about the anti-pattern comment:


它被过度使用,在
的情况下引入了不必要的限制,其中一个类的唯一实例实际上不是必需的,而
引入了全局状态转化为应用程序

it is overused, introduces unnecessary restrictions in situations where a sole instance of a class is not actually required, and introduces global state into an application

From: http://en.wikipedia.org/wiki/Singleton_pattern

有关更多信息,您可以查看: https://www.michaelsafyan.com/tech/design/patterns/singleton

For more on this you can look at: https://www.michaelsafyan.com/tech/design/patterns/singleton

这是上面博客的一个很好的结局:

Here is a great ending to the blog above:


简而言之,单例模式使代码更复杂,不太有用,
和真正的痛苦重新使用或测试。消除单身人士可能是
棘手,但这是值得的努力。

In short, the singleton pattern makes code more complex, less useful, and a real pain to re-use or test. Eliminating singletons can be tricky, but it’s a worthwhile endeavour.

好的,在本段中描述的模式很好,正如作者所表达的那样,它将您的代码与单身密切联系。

OK, so, the reason it is an anti-pattern is described well in this paragraph, and, as the author expresses, it tightly couples your code to the singleton.

如果您发现要使用单例,你可能想考虑你的设计,但有时候它是有用的。

If you find that you want to use a singleton, you may want to consider your design, but there are times where it is useful.

例如,一旦我必须编写最多可以拥有一个数据库连接的应用程序,来处理数千个请求。所以,一个单例是有道理的,因为我被资源限制只有一个实例。

For example, once I had to write an application that could have at most one database connection, to process thousands of requests. So, a singleton makes sense since I am resource constrained to having only one instance.

但是,一般来说,这是用来简化代码,而不考虑将是

But, generally this is used to simplify code, without thinking of the difficulties that will be introduced.

例如,这也适用于静态类,如果单元测试或并发性,则一个请求的状态将改变状态,引起问题,因为调用实例的类可能会假设状态是按预期的。

For example, and this applies to static classes also, if you unit test, or have concurrency, then the state of one request will change the state and that may cause problems, as the class calling the instance may be assuming the state is as it expected.

我认为挑战使用的最好方法是想想如何处理如果你的程序是多线程的,并且一个简单的方法是单元测试,如果你有几个测试一次运行。

I think the best way to challenge the use is to think of how to handle it if your program is multi-threaded, and a simple way to do that is to unit test it, if you have several tests that run at one time.

如果你发现你仍然需要它,然后使用它,但意识到稍后会遇到的问题。

If you find that you still need it, then use it, but realize the problems that will be encountered later.

这篇关于为什么Singleton被认为是反格局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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