所有前端类都应该使用单例吗?
考虑 Martin Fowler 的企业应用程序架构模式和前端控制器模式:http://martinfowler.com/eaaCatalog /frontController.html 显然,它使用单例模式。好吧,我在 php 应用程序中有一个可以一起工作的类包(如 Zend 的控制器包),并且有一个类使它们全部可用,并且由于它与 Front Controller 的大部分概念相似,所以我将其命名为 PackageName_Front。但它不应该是一个单例类(与 Front Controller 相对),所以我仍然让它具有 Front 名称吗?如果不是,我该给它起什么名字呢? 由于它是一个相当大的包,我只需要它尽可能遵循约定(而不是以教条的方式!),以便其他开发人员可以阅读。
更多信息:这与控制器无关。它只是一个像 Zend_Form 一样工作的对象(它将 Zend_Form_Element_X 和 Zend_Validate 等所有其他对象的使用合并到一个对象中),但我不能只将其命名为 PackageName。它必须是 PackageName_Something,而我只是不知道 Something 应该是什么。也许是“Handler”?...我只是想确保当有人读到它的名字时,不会对它在整个包中的角色感到困惑:)
Consider Martin Fowler's Patterns Of Enterprise Application Architecture, and the pattern of Front Controller: http://martinfowler.com/eaaCatalog/frontController.html
Apparently, it uses the singleton pattern. Well, I have a package of classes in php application that work together (like Zend's Controller Package) and there is one class that makes them all usable and since it resembles much of Front Controller's concepts, I named it PackageName_Front. But it shouldn't be a singleton class (as opposed to Front Controller), so do I still let it have the name Front? If not, what do I name it?
Since it's a quite big package, I just need it to follow conventions as much as possible (not in a dogmatic way!) so it would be readable to other developers.
More info: It's not anything related to controllers. It's just an object that works like Zend_Form (which consolidates use of all the other objects like Zend_Form_Element_X and Zend_Validate into one object) But I can't just name it PackageName. It has to be PackageName_Something, and I'm just not sire what Something should be. Maybe "Handler"?... I just wanna make sure when someone reads it's name, doesn't get confused about it's role in the whole Package :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
FrontController 不必实现为 Singleton。这本书没有提出这样的建议。书中的示例使用 Servlet 作为处理程序。
仅仅因为一个类在应用程序中只需要一次,并不能证明它作为单例实现是合理的。它缺少单例的目的,即强制一个类只能有一个实例并提供对其的全局访问。如果您只需要一次特定实例,请考虑仅创建一个。
如今,许多人(包括 GoF 中的 Erich Gamma)将 Singleton 视为一种代码味道,并且不鼓励使用它。在无共享架构中,单例只能限制当前请求内的实例,因此在 PHP 中的使用受到限制。无需使用单例模式,可以通过(邪恶的)全局关键字或静态方法来实现对对象的全局访问。全局访问总是会产生不必要的耦合。更好的方法是使用依赖注入,它的额外好处是提供更少的耦合,从而提供更好的可维护性。
据我所知,没有这样的关于命名类 Front 类的约定。您所描述的可能是 Facade 或 网关。另外,您确定不能在 PackageName 之后命名该类吗?毕竟,
Zend_Form
包也有一个Zend_Form
类。FrontController does not have to be implemented as Singleton. The book does not suggest anything like this. The example in the book uses a Servlet for the Handler.
Just because a class will only be needed once in an application doesnt justify it's implementation as a Singleton. It's missing the purpose of the Singleton which is to enforce a class can only have one instance and provide global access to it. If you need a particular instance only once, consider Just Create One instead.
Many people nowadays (including Erich Gamma of GoF fame) view the Singleton as a code smell and discourage it's use. In a shared-nothing-architecture the Singleton can only restrict instances inside the current request anyway, so the use in PHP is limited. Global access to an object can be achieved without the Singleton pattern, either through the (evil) global keyword or static methods. Global access always creates unneeded coupling. The better way would be to use Dependency Injection, which has the added benefit of providing less coupling and thus better maintainability.
There is no such convention about naming classes Front classes to my knowledge. What you describe could be a Facade or a Gateway though. Also, are you sure you cannot name the class after the PackageName? After all, the
Zend_Form
package has aZend_Form
class, too.仅从纯粹的设计角度来看,当您说以下内容时,听起来您正在使用 PackageName_Front 作为外观:
福勒对该模式的实现说:
这暗示可以使用 Singleton 来实现 Front Controller 类,但它肯定不会限制它使用它。但他没有明确提及。
我认为是否是 Singleton 并不重要。只需确保它是请求的唯一渠道,您就可以成功使用该模式。 :)
Just from a purely design view, it sounds like you're using that PackageName_Front as a Facade when you say:
Fowler's implementation of the pattern says:
This insinuates that a Singleton might be used to implement the Front Controller class, but it certainly doesn't constrain it to use it. He doesn't explicitly mention it though.
I don't think it's important whether or not its a Singleton. Just makes sure its the sole channel for requests, and you'll have successfully used the pattern. :)
单例模式背后的想法是确保一个对象只有一个实例,该对象应该只存在于单个实例中。前端控制器非常适合这一类,因此让它遵循单例模式也许是明智的。
但是,如果您的代码始终确保它仅调用构造函数一次,那么就有空间容纳您的非单例模式对象。
我的 2 美分在这里,因为我不是任何书籍作者之类的。
The idea behind the singleton pattern is to make sure there is only one instance of an object that is supposed to only exist in a single instance. The front controller falls very well into this category, so it would, perhaps, be wise to make it follow a singleton pattern.
If, however, your code will always make sure it calls the constructor only once, then there is room for your non-singleton pattern object.
My 2 cents here, since I'm not any book author or something.