gpt4 book ai didi

asp.net - 单例模式与 Web 应用程序,不是一个好主意!

转载 作者:行者123 更新时间:2023-12-02 20:52:21 24 4
gpt4 key购买 nike

我发现了一些有趣的东西,我在调试其他东西时幸运地注意到了它。我正在应用 MVP 模式,并制作了一个单例 Controller 以在所有演示文稿之间共享。

突然我发现某个事件在第一次回发时被调用一次,如果有两次回发则调用两次,如果有100次回发则调用100次。

因为 Singleton 基于保存实例的静态变量,并且静态变量跨回发,所以我连接该事件,假设它将连接一次,并为每次回发重新连接。

我认为在 Web 应用程序中应用单例之前我们应该三思而后行,否则我会错过一些东西?

谢谢

最佳答案

在任何地方使用 Singleton 时我都会三思而后行。

许多人认为 Singleton 反模式

Some consider it an anti-pattern, judging that it is overused, introduces unnecessary limitations in situations where a sole instance of a class is not actually required, and introduces global state into an application.

维基百科上有很多引用文献对此进行了讨论。

很少需要单例,我个人认为它们与全局变量相同。

关于asp.net - 单例模式与 Web 应用程序,不是一个好主意!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2709071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com