这是答案
为什么在这种特定情况下使用代理类?
如果您仔细阅读下面为类“ SetConversionValueObserver”编写的代码,如果Google adwards未激活,则“返回”,如果没有订单,则“返回”。意味着,仅当订单ID存在且Google adwords处于活动状态时,才会创建订单集合对象。如果我们注入实际的Order集合类,则对象管理器会使用其父类对象创建集合对象,而不会知道Google adwords未处于活动状态,并且会降低订单成功页面的速度。因此,更好地按需创建使用代理的对象。/vendor/magento/module-google-adwords/Observer/SetConversionValueObserver.php
/**
* Set base grand total of order to registry
*
* @param \Magento\Framework\Event\Observer $observer
* @return \Magento\GoogleAdwords\Observer\SetConversionValueObserver
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
if (!($this->_helper->isGoogleAdwordsActive() && $this->_helper->isDynamicConversionValue())) {
return $this;
}
$orderIds = $observer->getEvent()->getOrderIds();
if (!$orderIds || !is_array($orderIds)) {
return $this;
}
$this->_collection->addFieldToFilter('entity_id', ['in' => $orderIds]);
$conversionValue = 0;
/** @var $order \Magento\Sales\Model\Order */
foreach ($this->_collection as $order) {
$conversionValue += $order->getBaseGrandTotal();
}
$this->_registry->register(
\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_REGISTRY_NAME,
$conversionValue
);
return $this;
}
通常,什么时候应该使用代理类?
-当您觉得创建对象会很昂贵并且类的构造函数特别耗费资源时,请注入Proxy类。-当您不希望由于对象创建而对性能产生不必要的影响时。-当您感觉并非总是在特定条件下调用特定方法时,就应该创建对象。例如,布局构造器是资源密集型的。
实际布局构造函数与布局/代理
public function __construct(
Layout\ProcessorFactory $processorFactory,
ManagerInterface $eventManager,
Layout\Data\Structure $structure,
MessageManagerInterface $messageManager,
Design\Theme\ResolverInterface $themeResolver,
Layout\ReaderPool $readerPool,
Layout\GeneratorPool $generatorPool,
FrontendInterface $cache,
Layout\Reader\ContextFactory $readerContextFactory,
Layout\Generator\ContextFactory $generatorContextFactory,
AppState $appState,
Logger $logger,
$cacheable = true,
SerializerInterface $serializer = null
) {
$this->_elementClass = \Magento\Framework\View\Layout\Element::class;
$this->_renderingOutput = new \Magento\Framework\DataObject();
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
$this->_processorFactory = $processorFactory;
$this->_eventManager = $eventManager;
$this->structure = $structure;
$this->messageManager = $messageManager;
$this->themeResolver = $themeResolver;
$this->readerPool = $readerPool;
$this->generatorPool = $generatorPool;
$this->cacheable = $cacheable;
$this->cache = $cache;
$this->readerContextFactory = $readerContextFactory;
$this->generatorContextFactory = $generatorContextFactory;
$this->appState = $appState;
$this->logger = $logger;
}
代理构造函数,看看,没有父构造函数调用,也没有传递布局类名称,因此在调用方法时实际的对象创建发生了。
/**
* Proxy constructor
*
* @param \Magento\Framework\ObjectManagerInterface $objectManager
* @param string $instanceName
* @param bool $shared
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager,
$instanceName = \Magento\Framework\View\Layout::class,
$shared = true
) {
$this->_objectManager = $objectManager;
$this->_instanceName = $instanceName;
$this->_isShared = $shared;
}
代理类具有按需创建对象的方法,_subject是传递的类的对象。
/**
* Get proxied instance
*
* @return \Magento\Framework\View\Layout
*/
protected function _getSubject()
{
if (!$this->_subject) {
$this->_subject = true === $this->_isShared
? $this->_objectManager->get($this->_instanceName)
: $this->_objectManager->create($this->_instanceName);
}
return $this->_subject;
}
和使用_subject调用的方法。
/**
* {@inheritdoc}
*/
public function setGeneratorPool(\Magento\Framework\View\Layout\GeneratorPool $generatorPool)
{
return $this->_getSubject()->setGeneratorPool($generatorPool);
}