This is the 8th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

We’ll have a look when there is no intermediary

UML class diagram:

Code demo:

Description:

In the above example, an important problem is that in reality, the tenant and the landlord do not know each other, but it seems that they are already familiar with each other. In fact, most of the cases are not excluded, but they do not know each other. We often need the intervention of the intermediary to help us solve a series of problems in renting, such as: housing supply, landlord…

Introduce our key person agent (middleman, XX platform)

UML class:

Code demo:

Description:

It seems that no changes can be seen through the UML class diagram. The agent class is introduced and the rental house is abstracted into a class interface. However, we can see it through the final code execution block.

The proxy pattern

Provides a new way for other objects to access this object – a proxy

UML class diagram:

  1. Subject: Abstracts the functionality of objects that broker objects and real entities.
  2. Proxy: Saves entity objects and proxies their functions.
  3. RealSubject: The real entity that the proxy object helps.
abstract class Subject {
  public abstract request(): void;
}
Copy the code
class RealSubject extends Subject {
  public request(): void {
    console.log("I'm a tenant and I want to rent a house."); }}Copy the code
class MyProxy extends Subject {
  realSub: RealSubject;

  public request(): void {
    if (!this.realSub) this.realSub = new RealSubject();
    this.realSub.request(); }}Copy the code
new MyProxy().request();
Copy the code

Description:

As you can see from the code and class diagram above, we can save a lot of work just by finding a mediator that can do the work. The intermediary that can do the work binds our information, and it is best if only one (private) intermediary binds our information.

conclusion

In fact, we often hear about the proxy, because when we first debug the interface in a new project, we often encounter cross-domain problems. In addition to having the back end allow us cross-domain access, we use the proxy in a different way to make our real requests done by the proxy service. The function of agency is to do something for us when we are not convenient. Do you think it is reliable? 🤔 ️ 🤔 ️ 🤔 ️