为什么单例被认为是反模式? [英] Why is Singleton considered an anti-pattern?

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

问题描述

可能的重复:
单身人士有什么不好?

单例设计模式:陷阱

单例反模式

我最近听说单例模式是一种反模式.我知道这与创建类单例就像使唯一实例成为全局变量一样,但它的作用远不止于此(限制该对象的实例数量、管理实例化等).

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

来自: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

以上博客的精彩结尾:

简而言之,单例模式使代码更复杂,用处更小,以及重新使用或测试的真正痛苦.消除单身人士可以棘手,但这是一项值得的努力.

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.

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

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