单身人士将成为微博网站的良好设计模式吗? [英] Would singleton be a good design pattern for a microblogging site?

查看:129
本文介绍了单身人士将成为微博网站的良好设计模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我正在寻找一个微博网站踢,并发现单例设计模式类似乎完整,并与 PDO 我看不到任何错误(除了无法访问两个数据库,我不知道我需要这个项目)。



就像这样的项目,还是一个简单的CMS软件,单身汉会是个好主意?什么样的设计模式/数据库类类型将是大家伙用于这些事情,如果它被放大(并发连接/性能),这将是太限制?



我也读过一个工厂单例来处理额外的连接,因为更简单的更改代码。这是否会解决任何负面问题,并使其成为更合适的设计模式?

解决方案

Singleton的目的是限制对象实例到一个并提供全局访问。

两者都是您不想要或需要的东西。



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



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



即使 Erich Gamma ,Singleton模式的发明人之一,现在问这个模式:


我赞成放弃Singleton,它的使用几乎总是一种设计气味




你最好避免单身人士。


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?

解决方案

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.

这篇关于单身人士将成为微博网站的良好设计模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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