为什么单例被认为是一种不好的实践?

30 浏览
0 Comments

为什么单例被认为是一种不好的实践?

复制:\n引用:\n单例模式有什么问题?\n

\n我在阅读这个问题时,惊讶地发现他认为单例模式是一种“不好的实践”,并且认为这是常识。\n我在使用iBatis从XML中加载查询语句的任何项目中都经常使用单例模式。这在这些情况下大大提高了速度。我不确定为什么在这种情况下不使用它们。\n那么...它们为什么被认为是不好的呢?

0
0 Comments

在软件开发中,单例模式并不被认为是一个坏的实践。事实上,它在许多情况下非常有用。但是它确实存在两个主要的滥用和/或失败的领域:

  • 单元测试性
  • 多线程

这两个问题都是可以解决的,但是初学者往往会忽视这些问题(通常是因为无知),结果导致比他们所能处理的麻烦更多。

在单元测试方面,单例模式可能会导致问题,因为它们在整个应用程序中只有一个实例。这意味着在进行单元测试时,我们无法轻松地模拟和隔离单例对象。为了解决这个问题,我们可以使用依赖注入来替代单例模式。通过将依赖关系作为参数传递给对象,我们可以更容易地进行单元测试,并且不会受到单例对象的限制。

另一个问题是多线程环境下的单例模式。当多个线程同时访问单例对象时,可能会导致竞态条件和不一致的状态。为了避免这种情况,我们可以使用双重检查锁定(double-checked locking)来确保只有一个线程可以创建单例对象。双重检查锁定是一种常见的解决方案,它通过在创建对象之前和之后进行额外的同步来确保线程安全。

总之,单例模式本身并不是一个坏的实践,但在单元测试和多线程环境下容易被滥用或导致问题。通过使用依赖注入和双重检查锁定等解决方法,我们可以克服这些问题,并正确地使用单例模式。

0
0 Comments

单例模式是一种工具,像任何工具一样,有时候你应该使用它们,有时候你应该使用其他东西。在这种情况下,很常见的情况是,在一开始看起来适合使用单例模式的情况下,使用其他东西(工厂模式、静态类)可能会更好。

当《设计模式》出现时,似乎每个人都加入了单例模式的行列——它们无处不在,甚至出现在不应该出现的地方。现在我们看到的是对单例模式的反弹(也许是理所当然的反弹)。并不是说你不应该使用它们,但是退一步,考虑一下所有可用的选项可能是一个好主意。

为什么单例模式被认为是一种不良实践?有几个原因。首先,它们引入了全局状态,并且可以在任何地方访问。这使得代码更难以理解和维护,因为你无法准确地知道在何处修改了该状态。

其次,单例模式违反了单一职责原则。一个类应该只有一个原因去改变。而单例模式将创建对象和管理对象生命周期的责任合并到一个类中,这可能导致一个类有太多的职责。

另外,单例模式使得代码更难以测试。由于它们的全局可访问性,单例对象在测试时可能会引入不确定性和依赖性,使得测试变得困难。

解决这个问题的方法是使用其他替代方案,如工厂模式或静态类。工厂模式可以通过创建对象的工厂方法来提供对对象的访问,并允许在需要时创建多个实例。静态类则提供了一种无需实例化即可访问的方式,但需要注意避免过度使用静态类,以免引入与单例模式相似的问题。

总之,单例模式并不总是适合所有情况。在使用单例模式之前,我们应该考虑其他替代方案,并根据具体情况选择最合适的设计模式。

0
0 Comments

单例模式并不一定是坏的,只是被误用和过度使用。人们似乎无法理解地被这种模式吸引,并试图将其硬塞到他们的应用程序中,无论它是否真的适用。

The main reason why singletons are considered to be a bad practice is because they introduce a global state into an application. This global state can be accessed and modified from anywhere within the application, leading to potential issues with concurrency and thread safety. It also makes the codebase more difficult to test and maintain, as the dependencies of a singleton are hidden and cannot be easily mocked or replaced.

Another reason is that singletons often violate the Single Responsibility Principle. They are responsible for both creating and managing their instance, as well as providing the functionality they are designed for. This can lead to a class that is tightly coupled with other parts of the application and difficult to extend or modify.

To address these issues, it is recommended to use dependency injection instead of singletons. Dependency injection allows for better control and management of dependencies, making the codebase more modular and testable. Instead of relying on a global state, dependencies are explicitly provided to the classes that need them.

In addition, using dependency injection frameworks or containers can simplify the process of managing dependencies and reduce the boilerplate code required for instantiation and wiring of objects.

Overall, singletons should be used sparingly and only when truly necessary. It is important to carefully consider the design and architecture of an application, and choose the appropriate patterns and practices that promote maintainability, testability, and extensibility.

0