Here are the answers
why a proxy class is used in that particular case ?
If you take a close look below code which is written for class "SetConversionValueObserver", if Google adwards is not active "return" and if there is no order "return". Means, Order Collection Object will be created only when order Ids exist and Google adwords active. if we inject actual Order collection class then object manager create collection object with its parent class objects without knowing Google adwords not active and that slow down order success page. so, better create object on demand that is the use of proxy.
/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;
}
when, in general, should one use a proxy class ?
- Inject Proxy class when you feel object creation will be expensive and
class’s constructor is particularly resource-intensive.
- when you don't want unnecessary performance impact due to object creation.
- when you feel object creation should happen when you call particular method in particular condition not always.
For example Layout constructor is a resource-intensive.
Actual Layout constructor vs layout/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, take a look, no parent constructor called as well as just passed layout class name so that actual object creation happen when method called.
/**
* 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;
}
Proxy class has method to create object on demand, _subject is the object of passed class.
/**
* 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;
}
And method called using _subject.
/**
* {@inheritdoc}
*/
public function setGeneratorPool(\Magento\Framework\View\Layout\GeneratorPool $generatorPool)
{
return $this->_getSubject()->setGeneratorPool($generatorPool);
}