Proxy Pattern

Proxy Pattern

Let us imagine you go to a service centre to get your vehicle repaired their you take your token number from the counter person and then when the time comes, a Machine expert sees and repairs your vehicle. So the middle-person over here which is the counter person is the proxy over here.

So in layman's terms, we can use a proxy pattern as a middleman between the User and the End Service which is being used in place of service.

Proxy Pattern acts as a middle-person between the client and the actual object, providing a simplified interface while hiding implementation details. It enables adding functionalities such as caching, logging, or access control to the object's operations. This pattern results in a more efficient and flexible system.

In the above example, the user is the Client object which thinks is giving work to the machine expert directly and is just able to access the Counter Person which is the proxy in that case and then it is up to the Proxy (Counter Person) whether the request of vehicle repair will be even processed or not.

Let's see the Proxy pattern's official definition of,

Proxy Pattern: The proxy pattern provides a surrogate or placeholder for another object to control access to it.



Advantages & Benifits

There are various advantages of using a Proxy pattern in your codebase which includes:-

  1. Adding Behaviors To requests:- Understand this as attaching several additional things to your request or processing the requests in a particular way. eg:- Attaching JSON Token with various requests.
  2. Adding Access Control:- Whether a user is authorized to use a particular service or not is a good use case for the proxy pattern. A common middleware of the same service type could be developed for this purpose using the proxy pattern.
  3. Lazy initialization or Virtual proxy:- In this the resources which are heavy to create and are used rarely only. A virtual proxy can be developed for it and to initialize only when it is required.
  4. Caching:- When we want to collect caches of the requests being processed we can efficiently implement in the proxy. Even if the caches which are no longer required can be cleared that use case can also be handled over in the proxy only.
  5. Logging:- A crazy and easy way to implement logging requests in the server can be done using the proxy pattern by logging the requests and responses in the server.


Components in Proxy Pattern & How to Implement It

Components:- Proxy patterns consist of the following components.

  1. Subject Interface:- This is the base interface which needs to be implemented by both the real object and the proxy object. To make them compatible and interchangeably useable among themselves.
  2. Proxy:- The proxy object which has an instance of the real object plus additional methods according to the requirements.

3. RealObject:- This is the service which needs sugarcoating or proxy.

How to Implement Proxy pattern:-

Step 1:- If there is no base interface which needs to be implemented by both real object and the proxy object make one. To make them compatible and interchangeably useable among themselves.

Step 2:- Create a proxy class and it must include:-

1. An instance of the real object service for which this proxy is being designed.

2. Usually a proxy manages the whole lifecycle of the service but sometimes, the creation phase is handled in the constructor initialization of the proxy.

Step 3:- Implement the proxy methods according to their purpose and in most of the cases after performing some operations control is passed on to the real Service.

Step 4:- Try lazy initialization in your code.


Coding Proxy pattern in Python:-

  • Define a subject interface or base interface.

  • Define the proxy class which takes the real object as an argument and implements its methods.

  • Now implement the base object class with its method and write the code of the Main function as shown.


Disadvantages

  1. It makes the code base a bit more complex to handle.
  2. Performance overhead is added.

Overall Proxy pattern is a great tool to add to your knowledge for a variety of reasons.

#python #system-design #proxy


要查看或添加评论,请登录

Manjeet Singh的更多文章

社区洞察

其他会员也浏览了