单例对于微博网站来说是一个好的设计模式吗?

发布于 2024-09-26 10:55:39 字数 508 浏览 3 评论 0原文

我过去在项目中没有使用过任何 OO,因为我让它更简单(实际上使用古老的 mysql_query 调用和我自己的过滤),所以我想开始一个新项目,学习在我的 OO 中使用设计模式。

我正在寻找建立一个微博网站的乐趣,并发现 单例设计模式 类似乎很完整,并与 PDO 一起使用,我看不出它有什么问题(除了不能够访问两个数据库,我不确定这个项目是否需要)。

对于像这样的项目,或者简单的 CMS 软件,单例会是一个好主意吗? “大佬”会使用什么样的设计模式/数据库类类型来完成这些事情,如果以后要升级(并发连接/性能),这会不会限制太多?

我还阅读了有关工厂单例的信息,以便稍后处理额外的连接,因为更改其中的代码更简单。这会解决任何负面问题并使其成为更合适的设计模式吗?

I have not used any OO in the past in projects, as I kept it simpler (in fact using archaic mysql_query calls and my own filtering), so I wanted to start a new project, learning to use design patterns with my OO along the way.

I was looking to build a microblogging site for kicks, and found the singleton design pattern class which seemed complete, and to use with PDO I could not see anything wrong with it (other than not being able to access two databases, which I am not sure I would need for this project).

On say a project like this, or a simple CMS software, would a singleton be a good idea? What sorts of design patterns/database class type would "the big guys" be using for these things, would this be too restrictive later on if it were to be upscaled (concurrent connections/performance)?

I had also read about a factory singleton to deal with dealing with extra connections later on as it is simpler to change the code in it. Would this fix any of the negative issues and make it a more suitable design pattern for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空气里的味道 2024-10-03 10:55:39

Singleton 的目的是将对象实例限制为一个并提供全局访问。
两者都是你不想要或不需要的东西。

在 PHP 中将实例限制为一个实例是毫无意义的,因为此限制仅适用于当前请求中的实例。如果两个请求同时到达您的微博网站,每个请求仍将有一个实例。如果您想确保只有一个实例,则只需不要实例化第二个实例即可。

全局访问也不是您想要的,因为它破坏了封装。如果您需要对象内的某个实例,请通过 依赖注入。这是干净且可维护的。它的另一个好处是允许您轻松地与其他实现交换依赖关系,例如 模拟类用于您的单元测试

甚至单例模式的发明者之一 Erich Gamma 如今也对这种模式提出了质疑:

"我赞成放弃 Singleton。它的用途是几乎总是有设计味”

你最好避免使用单例。

The Singleton's purpose is to limit object instances to one and to provide global access.
Both are things you don't want or need.

Limiting your instance to one instance is rather pointless in PHP where this restriction only applies to the instances in the current request. If two requests hit your microblogging site at the same time, there will still be one instance each per request. If you want to make sure there is only instance, simply do not instantiate a second instance.

Global access is nothing you want either, because it breaks encapsulation. If you need a certain instance inside your objects, pass it in via dependency injection. That's clean and maintainable. It has the added benefit of allowing you to easily exchange dependencies with other implementations, like for instance mock classes for your unit tests.

Even Erich Gamma, one of the Singleton pattern's inventors, questions this pattern nowadays:

"I'm in favor of dropping Singleton. Its use is almost always a design smell"

You are best off avoiding Singletons.

遇见了你 2024-10-03 10:55:39

如果我没有记错的话,单例是一个 反模式。但根据任务的不同,可以使用它。

If I am not mistaken singleton is an antipattern. But depending on the task it can be used.

一向肩并 2024-10-03 10:55:39

如前所述,单例对您的博客应用程序没有多大帮助。只需创建一个数据库实例并使用它。

Sidenode,你在 PHP 中看到的往往是“假单例”。如果作为普通类实现,通常涉及使用实现单例解决方法的 ::getInstance() 方法。然而,该类的存在允许实例化多个项目(new Singleton() && new Singleton())。因此,我建议使用过程单例,它不存在这个问题,而且看起来也更好:

 function db() {
     static $db;
     if (!isset($db)) {
          $db = new PDO("sqlite:memory");
     }
     return $db;
 }

这样你就可以使用 db()->query("SELECT * FROM blog")db()->query("SELECT * FROM blog") 并避免总是导入全局 $db var。

As said before, the singleton doesn't help you with your blog application much. Just create a single database instance and use it.

Sidenode, what you see in PHP are often "fake singletons". If implemented as plain class, it usually involves using a ::getInstance() method which implements the singleton workaround. The existence of the class however allows to instantiate multiple items (new Singleton() && new Singleton()). Therefore I'd recommend a procedural singleton, which doesn't have this problem and is also much nicer on the eyes:

 function db() {
     static $db;
     if (!isset($db)) {
          $db = new PDO("sqlite:memory");
     }
     return $db;
 }

This way you can use db()->query("SELECT * FROM blog") and avoid to always import a global $db var.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文