What is Factory class in Magento 2

 

Click here for real example video

What is Factory
class in Magento 2

Overview

Factory classes are service class that instantiate
non-injectable classes, that is, models that represent a database entity.
They create a layer of abstraction between the 
ObjectManager and
business code.

Relationship to ObjectManager

The MagentoFrameworkObjectManager is the class responsible for instantiating objects in
the Magento application. Magento prohibits depending on and directly using
the 
ObjectManager in
your code.

Factories are an exception to this rule because
they require the 
ObjectManager to instantiate specific models.

The following example illustrates the relationship between a
simple factory and the 
ObjectManager:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

namespace MagentoFrameworkAppConfig;

 

class BaseFactory

{

  /**

   * @var
MagentoFrameworkObjectManagerInterface

   */

  protected $_objectManager;

 

  /**

   * @param
MagentoFrameworkObjectManagerInterface $objectManager

   */

  public function __construct(MagentoFrameworkObjectManagerInterface $objectManager)

  {

    $this->_objectManager = $objectManager;

  }

 

  /**

   * Create config model

   * @param
string|MagentoFrameworkSimplexmlElement $sourceData

   * @return
MagentoFrameworkAppConfigBase

   */

  public function create($sourceData = null)

  {

    return $this->_objectManager->create(MagentoFrameworkAppConfigBase::class, [‘sourceData’ => $sourceData]);

  }

}

Writing factories

Unless you require specific behavior for your factory
classes, you do not need to explicitly define them because they are an automatically generated class type. When you
reference a factory in a class constructor, Magento’s object manager generates the factory class
if it does not exist.

Factories follow the naming convention <class-type>Factory where <class-type> is
the name of the class the factory instantiates.

For example the automatically generated MagentoCmsModelBlockFactory class
is a factory that instantiates the class MagentoCmsModelBlock.

Using factories

You can get the singleton instance of a factory for a
specific model using dependency injection.

The following example shows a class getting the BlockFactory instance
through the constructor:

1

2

3

function __construct ( MagentoCmsModelBlockFactory $blockFactory)
{

    $this->blockFactory = $blockFactory;

}

Calling the create() method on a factory gives you
an instance of its specific class:

1

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

For classes that
require parameters, the automatically generated 
create() function accepts an array of
parameters that it passes on to the 
ObjectManager to create the target class.

The example below shows the construction of a MagentoFrameworkFlagFactory object
by passing in an array of parameters to a factory:

1

2

$flag
= $this->flagFactory->create([

 
‘data’ =>  [‘flag_code’ =>
‘something’]

The Flag class has a $data constructor parameter which
corresponds to the data key in the 
create array above.

Interfaces

Factories are smart enough to resolve dependencies and allow
you to get the correct instance of an interface as defined in your
module’s 
di.xml.

For example, in the CatalogInventory module,
the 
di.xml file
contains the following entry:

1

 <preference for=“MagentoCatalogInventoryApiDataStockItemInterface” type=“MagentoCatalogInventoryModelStockItem” />

It instructs Magento to
use the specific 
Item class wherever
the StockItemInterface is
used. When a class in that module includes the factory 
StockItemInterfaceFactory as
a dependency, Magento generates a factory that is capable of creating the
specific 
Item objects.

Puneet Kumar Magento Developer
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply