A proxy is usually an intermediary system between the seeker and the provider. The core idea is that the client (the seeker) does not deal directly with the provider (the real object), but instead accomplishes the resource or operation provided by the provider through a proxy object.
A proxy is actually a wrapper or agent that encapsulates the actual object being served. A proxy can provide additional functionality to the object it wraps without changing the code of this object. The main purpose of the proxy pattern is to provide a surrogate or placeholder for other objects, thus controlling access to the actual object.
Three common different types or agents for different application scenarios:
- Virtual Proxy: If an object will take up a lot of memory when instantiated, it can be represented using placeholders first, and the actual object will be created only when the client requests or accesses the object.
- Remote Proxy: Provides a local representation of an actual object located on a remote server or a different address space. For example, an application program may need to obtain information about objects on different servers or spatial addresses, and can then use a local proxy to obtain the relevant information without having to "deal" directly with the objects on the various servers or spatial addresses.
- Protecting Proxies: The real actual object is accessed through a proxy, and when accessing it, the proxy checks and controls the request permissions, authentication, authorization, etc. from the client, thus protecting the real actual object.
Proxy Pattern Notes:
- Clients can actually access real objects directly to get the results they want, but there are many advantages to using proxies, and as the name "proxy" implies, it is possible to do so, but the exact use needs to be determined on a case-by-case basis.
- Proxies can add additional operations to the proxy's interface as needed, but care needs to be taken that these additional operations do not become a "burden".
- Since proxies are equivalent to a layer of encapsulation for real objects, they may add a certain amount of time-consumption.
Simple Example:
from abc import ABCMeta, abstractmethod class HouseOwner(metaclass=ABCMeta): """Abstract class of homeowners: all can rent out their homes""" @abstractmethod def rent_house(self, rental): pass class Landlord(HouseOwner): """Real Subject: Homeowner""" def __init__(self): = 0 self.house_key = 'house key' def rent_house(self, rental): """Collects rent and keys the house to the person renting it.""" += rental return self.house_key class HouseAgent: """Agency type: agents, who act as agents for landlords to rent out their homes""" def __init__(self): = 0 self.house_resource = [] # There must be more than one listing, so I'll just put one here briefly # self.house_resource.append(Landlord()) # In general, the proxy class has the same interface as the class representing the real object # Indicates that this method proxies an operation on a real object. def rent_house(self, rental, agency_fee): """Collecting rent and brokerage fees and renting out houses to clients""" += agency_fee house_key = self.house_resource[0].rent_house(rental) return house_key class Renter: """Client class: tenant""" def __init__(self): = 10000 self.house_key = None self.house_agent = HouseAgent() def find_house(self): """Renting a house with a particular agent (the person for whom the agent is acting)""" self.house_key = self.house_agent.rent_house(3000, 1000) print("You've rented a house!") if __name__ == '__main__': renter = Renter() renter.find_house()
This is the whole content of this article.