Factory Class: What is Factory Design Pattern in Magento 2

The factory class is a design pattern so that factories are known as service classes that provide the functionality to instantiate non-injectable classes, that is, models that represent a database entity. They create a layer of abstraction between the ObjectManager and business code.

Factory Class pattern in Magento

 

Factory Class Used for?

Create objects for classes instead of using the new keywords.

Here I am giving you to explain in a better way so you can easily understand how factory method used to create objects without using the new keywords.

 

<?php
/**
* Category: Factory Design Pattern
* Developer: Hemant Singh Magento 2X Developer
* Websites: http://www.wishusucess.com/
*/

namespace Magento\Framework\App\Config;

use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Simplexml\Element;
use Magento\Framework\App\Config\Base;

class BaseFactory
{
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;

/**
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManagerInterface $objectManager)
{
$this->_objectManager = $objectManager;
}

/**
* Create config model
*
* @param string|Element $sourceData
* @return Base
*/
public function create($sourceData = null): Base
{
return $this->_objectManager->create(Base::class, ['sourceData' => $sourceData]);
}
}

 

What is Object Manager?

Magento has one class that is

Magento\Framework\ObjectManager

and this object manager class is responsible for instantiating objects in your Magento app.

So this whole concept needs to avoid the direct use of object manager in Magento source code.

But this factory's methods are an exception in case of avoiding the object manager rule because they require the ObjectManager to instantiate specific models.

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

 

Why Writing Factory Class?

Magento core functionality provides you to use factory classes without explicitly defining unless and until if you do not require some specific changes in the behavior.

Magento’s object manager automatically generates the factory methods if it does not exist.

Naming Convention Example:

Magento\Cms\Model\BlockFactory class is a factory that instantiates the class Magento\Cms\Model\Block.

 

Instance Through the Constructor Example

function __construct ( \Magento\Cms\Model\BlockFactory $blockFactory) {
$this->blockFactory = $blockFactory;
}

Using factories

We can get the singleton instance of a factory for a specific model using di.

We can use create() method to call on factory methods so that will give an instance of its specific class.

Example:

$block = $this->blockFactory->create();

 

How To Use Interfaces in Factory Class

By using factories we can resolve dependencies in Magento that provide the functionality to get the correct instance of an interface from the module’s di.xml.

<preference for="Magento\CatalogInventory\Api\Data\StockItemInterface" type="Magento\CatalogInventory\Model\Stock\Item" />

 

 

Recommended Post:

Magento 2 REST API: How To Call REST API in Magento 2

Category ID: How To Get Current Category ID in Magento 2

Leave a Reply