Open In App

Proxy Method – Python Design Patterns

The Proxy method is Structural design pattern that allows you to provide the replacement for an another object. Here, we use different classes to represent the functionalities of another class. The most important part is that here we create an object having original object functionality to provide to the outer world.
The meaning of word Proxy is “in place of” or “on behalf of” that directly explains the Proxy Method.



Proxies are also called surrogates, handles, and wrappers. They are closely related in structure, but not purpose, to Adapters and Decorators.

proxy-design-method

A real-world example can be a cheque or credit card is a proxy for what is in our bank account. It can be used in place of cash and provides a means of accessing that cash when required. And that’s exactly what the Proxy pattern does – “Controls and manage access to the object they are protecting“.



Problem Without Using Proxy method

Let’s understand the problem by considering the example of the College’s Database which takes care of all the student’s records. For e.g., we need to find the name of those students from Database whose balance fee if greater than 500. So, if we traverse the whole list of students and for each student object if we make a separate connection to the database then it will be proved as an expensive task.

Proxy-method-Problem

Solution using Proxy Method

Here comes the Proxy Method to solve the above-discussed problem. We will create a proxy server or maybe a proxy connection to the database and after that, we don’t have to create separate connections to the database for each student object.
We will simply get our needed data using the proxy without wasting a huge amount of memory for the creation of the object.

Proxy-method-Solution




class College:
    '''Resource-intensive object'''
  
    def studyingInCollege(self):
        print("Studying In College....")
  
  
class CollegeProxy:
    '''Relatively less resource-intensive proxy acting as middleman.
     Instantiates a College object only if there is no fee due.'''
  
    def __init__(self):
  
        self.feeBalance = 1000
        self.college = None
  
    def studyingInCollege(self):
  
        print("Proxy in action. Checking to see if the balance of student is clear or not...")
        if self.feeBalance <= 500:
            # If the balance is less than 500, let him study.
            self.college = College()
            self.college.studyingInCollege()
        else:
  
            # Otherwise, don't instantiate the college object.
            print("Your fee balance is greater than 500, first pay the fee")
  
"""main method"""
  
if __name__ == "__main__":
      
    # Instantiate the Proxy
    collegeProxy = CollegeProxy()
      
    # Client attempting to study in the college at the default balance of 1000.
    # Logically, since he / she cannot study with such balance,
    # there is no need to make the college object.
    collegeProxy.studyingInCollege()
  
    # Altering the balance of the student
    collegeProxy.feeBalance = 100
      
    # Client attempting to study in college at the balance of 100. Should succeed.
    collegeProxy.studyingInCollege()

Output:

Proxy in action. Checking to see if the balance of student is clear or not...
Your fee balance is greater than 500, first pay the fee

Proxy in action. Checking to see if the balance of student is clear or not...
Studying In College....

Class Diagram

Following is the class diagram for the Proxy Method:

Proxy-method-Class-Diagram

Advantages

Disadvantages

Applicability

Further Read – Proxy Design Method in Java


Article Tags :