Hello Friends, In this topic we will understand about magento 2 design patterns.
Magento 2 comes with interesting feature to optimize code and make the development easy which is called design patterrns. Design patterns in Magento 2 make your life easy if you know the right way to use it while you developing any module or doing any customization.
There are various types of magento 2 design pattern.
- Object Manager
- Dependency Injection
- Service Contract Design pattern
- Argument Replacment
- Events and Observers
- Injectable/Non-Injectable Object
Object Manager Plays a big role in Magento2 , Though Magento 2 prohibited the direct use of object manager. It can be use with factory, proxy etc design patterns. It automatically initiate the parameters in class constructor. It can be used either as GET or Create Method.
GET : This method works as singleton object which can be share between multiple components. So if you call get method from 2 places it will provide you same response.
Create : This create fresh instance every time. So if you call this create method from 2 places then every time you will receive different object.
Dependency Injection is a design pattern in Magento 2 , When you have one class and and this class is dependent on another class/object to perform some functions, so it’s called Dependency and when you inject that class into dependent class then it’s called Injection.
You can read our Dedicated blog on Dependency Injection
Service Contract Design pattern
Magento 2 is a module system , which allows 3rd party developer to customize the application in their own way. So to avoid any issues with code in terms of duplication or inconsitency of code magento 2 provides service contract design pattern.
Service contract is a set of interface that act as an layer between end user and business layer. Mostly Service contract is used while developing API.
We will understand more about service contract in our dedicated blog on service contract design pattern.
Factory is a non-injectable object design pattern in Magento 2 , This class automatically generates in 2 ways , If you are running magento in developer mode then it generates on run time , If you are running magento on production mode then you can generate it using setup:di:compile command.
For example if you are creating a class Xyz then it will create a factory class XyzFactory
This factory then can be use in any class constructor and you can use either get or create method of it.
We will understand more about Factory Design pattern in our dedicated blog
Proxy is one of the important design pattern and feature of magento 2 which can be use to improve your performance and reduce resource consumption if you will use it at right place.
Proxy is used to solve redundant problem in project.
You can read more about Proxy in My dedicated blog Magento 2 Proxies
Preference Design Pattern is used when you want to override any existing class. It is also a good pattern when you want to expand your business logic, but it should be used in right way as there are many other design patterns also available which you can use instead of preference. This design pattern you should use where other design patterns like plugin , observer etc. cannot be use.
We will read more about preference in our dedicated blog.
Argument replacement design patterns is very useful if you want to change your constructor argument without overwrite existing class and affecting other class.
You can read in detail about argument replacement from our dedicated blog Argument Replacement in Magento2
Events and Observers
If you are looking to modify or run your custom code then this design pattern can be helpful for you.
You can create events and dispatch it according to your needs
You can read more about it by reading our dedicated blog on How to create Magento 2 observer
A plugin, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface
You can read in detail by reading our blog How to create plugin in magento 2
Repositories are part of Service Contract – interfaces so it can be use for APIs.
In Magento 2 There are 2 types of object
Injectable Object : which you can inject into constructor and use as singleton or sharable object
Non-Injectable Object which cannot directly initiated, to initiate it you need to create a new instance. In this case you can use factory design pattern and to create instance you need to use create method.
Hope you like my this blog , please share with your friends and visit us again.