简体   繁体   English

用工厂定义的Zope实用程序与组件定义的Zope实用程序有什么区别?

[英]What is the difference between a Zope utility defined with a factory versus a component?

It's a little confusing that ZCML registrations for Zope utilities can accept a component or a factory. Zope实用程序的ZCML注册可以接受组件或工厂,这有点令人困惑。

<utility component=".some.Class" />

versus

<utility factory=".some.Factory" />

What is the difference? 有什么区别?

I think @lennart-regebro's answer might be a little unintentionally deceptive. 我认为@ lennart-regebro的回答可能是无意的欺骗。 It's understandable as the zope docs are a little vague. 这是可以理解的,因为zope文档有点模糊。 I think below is the correct interpretation but I had to test it myself to be sure. 我认为以下是正确的解释,但我必须自己进行测试才能确定。

In both the component and the factory registration each subsequent call to getUtility, after an initial registration, will return the same instance . 组件和工厂注册中,在初始注册之后, 每次后续对getUtility的调用都将返回相同的instance

The difference is that the component method will register the referenced object as the utility instance to be returned, where as the factory method will call the referenced object and store the result as the utility instance to be returned. 区别在于,component方法会将引用的对象注册为要返回的实用程序实例,而factory方法将调用引用的对象并将结果存储为要返回的实用程序实例。

Factory Example 工厂实例

So for example if you have a utility class defined as thus; 因此,例如,如果您有一个这样定义的实用程序类;

class MyUtility(object):
    implements(IMyUtility)
    ...

registering it with; 向其注册; <utility factory=".my_module.MyUtility"/>

will at the point the zcml is executed create an instance of MyUtility and store it for any future calls to getUtility; 将在执行zcml时创建MyUtility的实例并将其存储以供将来对getUtility的任何调用;

> a = getUtility(IMyUtility)
> b = getUtility(IMyUtility)
> c = getUtility(IMyUtility)
> a is b is c
True

Note that we could have registered the factory to be a function instead of a class, it just has to be something that when called with no arguments returns the instance of your utility you want to use. 请注意,我们可以将工厂注册为一个函数而不是一个类,它只需要在不带参数的情况下调用即可返回您要使用的实用程序的实例。

Component Example 组件示例

An equivalent example with component is; 组件的等效示例是;

class MyUtility(object):
    implements(IMyUtility)
    ...

my_module_instance_of_utility = MyUtility()

registering it with; 向其注册; <utility component=".my_module.my_module_instance_of_utility"\\>

This is more or less equivalent to the factory example. 这或多或少与工厂示例等效。 The only difference is that the instance returned in the component example is instantiated when my_module is imported by anything (including the registration mechanism), where as when using a factory the instance is instantiated when the zcml config is executed, just before it's registered. 唯一的不同是,组件实例示例中返回的实例是在my_module被任何东西(包括注册机制)导入时实例化的,就像使用工厂时,实例在zcml config被执行时实例化时一样(在注册之前)。 In both cases subsequent calls to getUtility will return the same instance. 在这两种情况下,对getUtility的后续调用都将返回相同的实例。

Not to be confused with IFactory 不要与IFactory混淆

None of this is to be confused with IFactory which does provide something which can provide you a new instance each time it's asked for but with a slightly different API. 这一切都不能与IFactory混淆,后者确实提供了可以在每次请求时为您提供新实例的东西,但API稍有不同。 See this document for more information about those . 有关更多信息,请参阅本文档

A factory creates utilities, while a utility registered as a component is an instance. 工厂创建实用程序,而注册为组件的实用程序是实例。 Hence, if you look up a utility registered as a component, you will get the same object back every time. 因此,如果您查找注册为组件的实用程序,则每次都会获得相同的对象。 But if it's registered as a factory, you'll get a new instance every time. 但是,如果它已注册为工厂,则每次都会获得一个新实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Factory和ServerFactory / ClientFactory有什么区别? - What's the difference between Factory and ServerFactory/ClientFactory? 使用Python的线程与异步/等待之间有什么区别 - What is the difference between using Python's threading versus async/await pop() 与从 dict 获取密钥有什么区别? - What's the difference between pop() versus getting key from dict? “导入包”与“从 package 导入……”有什么区别? - What is the difference between “import package” versus “from package import …”? RFECV 与 GridSearchCV 的评分有什么区别? - What's the difference between scoring in RFECV versus GridSearchCV? Interface(obj)和getAdapter(obj,Interface)之间的Zope组件差异 - Zope component discrepancy between Interface(obj) and getAdapter(obj, Interface) networkx2 adjlist_inner_dict_factory和adjlist_outter_dict_factory有什么区别 - what is the difference between networkx2 adjlist_inner_dict_factory and adjlist_outter_dict_factory 在discord py上有一个主要的bot类与没有类有什么区别? - What is the difference between having a main bot class versus no class on discord py? tf.keras.layers 与 tf.layers 有什么区别? - What is the difference between tf.keras.layers versus tf.layers? 在Pyramid Framework中,默认的未加密会话工厂和手动设置cookie有什么区别? - In Pyramid Framework what is the difference between default Unencrypted Session Factory and setting cookies manually?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM