proxy pattern
In Proxy Pattern, one class represents the functions of another class. This type of design pattern belongs to structural pattern.
In proxy mode, we create objects with existing objects to provide functional interfaces to the outside world.
introduce
Intent: to provide a proxy for other objects to control access to this object.
It mainly solves the problems caused by directly accessing objects. For example, the object to be accessed is on a remote machine. In the object-oriented system, some objects will bring a lot of trouble to users or system structure due to some reasons (such as high cost of object creation, or some operations need security control, or need out of process access). We can add an access layer to this object when accessing this object.
When to use: I want to do some control when accessing a class.
How to solve: add an intermediate layer.
Key code: combination of implementation and proxy class.
Application examples: 1. Shortcuts in Windows. 2. Pig Bajie went to Gao Cuilan, but the result was that the monkey king changed. It can be understood as follows: Abstract Gao Cuilan's appearance. Gao Cuilan himself and the monkey king have realized this interface. When pig Bajie visited Gao Cuilan, he couldn't see that this is the monkey king, so it is said that the monkey king is the agent class of Gao Cuilan. 3. To buy a train ticket, you don't have to buy it at the railway station. You can also go to the selling point. 4. A check or certificate of deposit is an agent for the funds in an account. Cheques are used to replace cash in market transactions and provide control over the funds on the issuer's account number. 5,spring aop.
Roles: 1. Abstract entity 2. Entity 3. Agent
Advantages: 1. Clear responsibilities. 2. High scalability. 3. Intelligent.
Disadvantages: 1. Due to the addition of proxy objects between the client and the real topic, some types of proxy modes may slow down the processing speed of requests. 2. Implementing the proxy pattern requires additional work, and the implementation of some proxy patterns is very complex.
Usage scenarios: divided by responsibilities, there are usually the following usage scenarios: 1. Remote agent. 2. Virtual agent. 3. Copy on write agent. 4. Protect or Access agent (controls access to the original object when the object has different access rights). 5. Cache agent. 6. Firewall agent. 7. Synchronization agent. 8. Smart Reference proxy.
Note: 1. Difference between adapter mode and adapter mode: adapter mode mainly changes the interface of the object under consideration, while proxy mode cannot change the interface of the proxy class. 2. The difference between decorator mode and decorator mode: decorator mode is for enhancement, while agent mode is for control.
Here are some simple code examples:
from abc import ABCMeta, abstractmethod class Subject(metaclass=ABCMeta): """Abstract entity""" @abstractmethod def get_content(self): pass @abstractmethod def set_content(self, *args, **kwargs): pass class RealSubject(Subject): """entity""" def __init__(self, filename): self.filename = filename f = open(filename, "r", encoding="utf-8") self.content = f.read() f.close() def get_content(self): return self.content def set_content(self, content): f = open(self.filename, "w", encoding="utf-8") f.write(content) f.close() class VirtualProxy(Subject): """Virtual agent""" def __init__(self, filename): self.filename = filename self.subject = None def get_content(self): if not self.subject: self.subject = RealSubject(self.filename) return self.subject.get_content() def set_content(self, content): if not self.subject: self.subject = RealSubject(self.filename) return self.subject.set_content(content) class ProtectedProxy(Subject): """ agent Protection agent: Read only,No write permission """ def __init__(self, filename): self.subject = RealSubject(filename) def get_content(self): return self.subject.get_content() def set_content(self, content): raise PermissionError("No write permission")