This is an example of a Proxy Pattern.
A proxy is mainly used to control access to a particular object. This is usually done by performing an action before or after the request has reached the object.
Both the Proxy and the original object must implement the same interface. The client can be left agnostic of which it receives and is only concerned about the actions it wants to execute.
A proxy can be used to hold off calling a resource hungry service until absolutely necessary. It can also help prevent unnecessary calls in some situations.
See the example below. When our services RealService has to be prepared, the proxy ProxyService will actually check its status.
On the first time we will need to call the resource hungry method. The second time around ProxyService will notice it's unnecessary and handle accordingly.
Prepared. Took me 3 seconds.
Prepared. Took me 0 seconds.
A proxy can be used to avoid heavy executions by caching results.
See the example below. When our services RealService has to execute the first time, the proxy ProxyService will cache the results.
The second time around ProxyService will notice it's unnecessary to execute and will return the cached results. This process is also known as Memoization.
Executing...
Executing...
Executing...
Executing...
Executing...
Executing...
Executing...
Executing...
Executing...
Executing...
FROM PROXY:
Executing...
Executing...
Executing...
Executing...
Executing...
Executing...
Executing...
Executing...
Executing...
Executing...