Proxy Pattern
Manjeet Singh
Senior Developer with 5 years of experience in full-stack development using React.js, Node.js, Python, and Django. Skilled in building scalable web applications and leading teams.
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:-
Components in Proxy Pattern & How to Implement It
Components:- Proxy patterns consist of the following components.
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:-
Disadvantages
Overall Proxy pattern is a great tool to add to your knowledge for a variety of reasons.
#python #system-design #proxy