Proxy Design Pattern

Proxy Design Pattern

Design Pattern Serie

Intent

A proxy controls access to the original object as It provides a substitute or a placeholder for another object to save time or resources when it costs a lot to access it. The proxy could interface with anything.

Problem

Imagine that we have an object consuming a huge amount of system resources, and we need to use it sometimes.
There is a problem that it is stored on a computer in another place and we can access it using the internet, but we have a problem that our internet speed isn't fast so we want :

  • To make sure that this object is available before making some effort to try to access it.
  • To use some temporary data (if there were a high necessity) if this object isn't available for any reason.

So how can we do that?

Solution

Here comes the role of the proxy design pattern that works as a bodyguard to allow allowed people to access the resources while others do not, and it represents that important person that he protects when it is not there.

Pros and Cons

Pros

  • The proxy works even if the service object isn’t available.
  • Encapsulation of access to microservices
  • We can control the service object without having clients need to know about it.

Cons

  • The response from services may get delayed (Bottleneck).
  • The code becomes more complicated as we introduce many new classes.

Thank you, and goodbye.