Zend_Registry:现实生活中的例子 [英] Zend_Registry: real life examples

查看:80
本文介绍了Zend_Registry:现实生活中的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否发现 Zend_Registry 有用?



应该使用哪些任务?为什么没有?



变量的全局状态不是一个好习惯。
主对象可能通过 $ front-> setParam('paramName',$ object)
注入全局状态,Zend_Registry的目的是什么 noreferrer>注册表模式中的PoEAA :


当您想要找到一个对象时,您通常会从另一个对象开始,与其关联,并使用关联导航到它。因此,如果您想要查找客户的所有订单,则首先使用客户对象并使用其上的方法来获取订单。但是,在某些情况下,您将不会有适当的对象开始。您可能知道客户的身份证号码,但没有参考。在这种情况下,您需要某种查找方法 - 查找器 - 但问题仍然存在:您如何到达查找器?


我使用注册表(当我使用它)的主要原因是因为它创建了一个易于访问的应用程序范围。通过注册表,我不必在全球范围内乱抛垃圾;只有注册表本身是全球性的。查找我从任何地方进入注册表的内容都很方便,包括模型:


  • Zend_Cache,Zend_Translate,重要的应用程序路径等然而,就像单身人士一样,注册管理机构往往不被看好。这里是布兰登萨维奇的一篇文章,想了解为什么不使用注册表。反对注册机构的主要观点是:


    • 它让单元测试变得更加困难

    • 没有经验的编码人员可能会抛出太多了,不关心正确的设计



    那些反对注册表的人通常会主张使用 Dependency Injection ,但应该注意的是,您也可以注册一次你有它的一个实例。然而,您没有控制反转,因为使用对象将从注册。尽管使用注册表作为服务定位器是一种有效的方法。

    请参阅Martin Fowler关于ServiceLocator的文章vs.依赖注入



    Like在您的问题的评论中指出, Zend_Registry 不是一个严格的单身人士。除了使用 Zend_Registry :: getInstance()获得的全局实例外,您还可以在需要的地方实例化多个实例。所以对象可以有自己的注册表。但是,以这种方式使用注册表时,它基本上只是一个荣耀的ArrayObject。



    最后一点:就像所有设计模式一样,如果适用于您的问题,请使用它。 >

    Do you find Zend_Registry useful?

    For which tasks it should be used? For which not?

    Global state for variables is not a good practice. Main objects may have global state injected via $front->setParam('paramName', $object), so what's the purpose of Zend_Registry?.

    解决方案

    Quoting PoEAA on Registry Pattern:

    When you want to find an object you usually start with another object that has an association to it, and use the association to navigate to it. Thus, if you want to find all the orders for a customer, you start with the customer object and use a method on it to get the orders. However, in some cases you won't have an appropriate object to start with. You may know the customer's ID number but not have a reference. In this case you need some kind of lookup method - a finder - but the question remains: How do you get to the finder?

    The main reason why I use the registry (when I use it) is because it creates an easily accessible application scope. With the registry, I don't have to litter objects all over the global scope; only the Registry itself is global. It's convenient to lookup whatever I've thrown into the Registry from everywhere, including the model:

    • Zend_Cache, Zend_Translate, important applications paths, etc

    However, just like with Singletons, Registry is often frowned upon. Here is an article by Brandon Savage with some thought about why not to use the Registry. The main arguments against the Registry are

    • it makes unit-testing harder
    • inexperienced coders might throw too much into it and don't care about proper design

    Those who vote against the Registry usually advocate the usage of Dependency Injection, although it should be noted that you can inject the Registry as well once you got an instance of it. You do not have Inversion of Control then though, because the using object will pull what it needs from the Registry. Using the Registry as a Service Locator is a valid approach though.

    See this article by Martin Fowler about ServiceLocator vs. Dependency Injection.

    Like pointed out in the comments to your question, Zend_Registry is not a strict Singleton. You can instantiate multiple instances where needed in addition to using the global instance you get with Zend_Registry::getInstance(). So objects can have their own registry. But when using Registry this way, it's basically just a glorified ArrayObject.

    Final note: just like with all Design Patterns, use it if applicable to your problem.

    这篇关于Zend_Registry:现实生活中的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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