Proxy is a software design pattern which is generally used to implement additional functionality to control the access to the original subject, for example, adding caching for a heavy resource object, check access rights for sensitive data.
Magento 2 uses the proxy pattern to implement their own set of proxy classes. Proxy classes are automatically generated and contain a lazy loaded version of a class. The class contains only one dependency - object manager. This allows the class to be referenced anywhere and not all of the dependencies have to be loaded right away.
In development mode proxies are generated on the fly when application can't find the class during execution. In production mode the classes are generated using code compiler:
To understand better lets look at an example of a native class that is generated.
As you can see the Proxy class extends the original class and implements NoninterceptableInterface to make sure not interceptors are generated for this Proxy class.
There are for 4 protected variables - object manager, instance name, subject, and shareability flag. These are used to instantiate object manager and set properties for the object.
The only method that is relevant from the subject class is the resolve function which calls the following:
The logic that is happening here means that the function first gets the subject (original class instantiated through object manager) and then calls the original function. Take a look how the subject is fetched:
First we check if the subject class is already loaded and if not it creates the object using object manager.
This is an extremely powerful pattern that allows you to save performance where possible.
You may be wondering why not always use proxies if they are so efficient. The answer here is that generating the proxy code takes a bit of resources, and instantiating each object through proxy takes more time than doing it directly. So proxies should be used only when the constructor contains long running operations and does not need to be used right away.