Introduction to the proxy model of JavaScript design pattern

  • 2020-05-09 18:11:13
  • OfStack

Proxy mode description

As the name implies, it is to use a class to replace another class to perform method functions, this pattern is similar to the decorative pattern, not the same as the proxy pattern is to replace the client initialization of the proxy object class, and the decorative pattern USES the interface or the initial decorator parameter reference to execute.

In the dynamic object-oriented language, the proxy pattern not only controls and decorates the proxy class, but also fully protects the proxy class. The proxy class is invoked indirectly only when we need it.

Scene description:

It is very common to see the example of renting a house. The tenant wants to rent a house and the landlord wants to rent out the house. However, neither the tenant nor the landlord has much time to find a house or wait for someone to see the house at home. The landlord entrusts the room to the intermediary agent to rent and sell the room. When there is a suitable person in need, the intermediary will take the room with him and pay for the rent, thus helping the landlord rent out the room. Tenant entrusts the room to belong to intermediary to help recruit the room position condition that asks, after intermediary helps tenant to find share a room, both hand in money to rent live, rent the place that lives for tenant so;

Example source code

Follow the example above;

1. Landlord leasing;



function Fangdong() {
    this.room = ' The room name ';
}
Fangdong.prototype.chuzu = function() {
    console.log(' Landlord rents room : ' + this.room);
}

2. Intermediary agent:


function Proxy() {
    this.fangdong = new Fangdong();
} Proxy.prototype.chuzu = function() {
    this.fangdong.chuzu();
    console.log(' There is a commission after the lease ');
}

3. Usage:



// The tenant asked an agent to help him find an apartment ;
var proxy = new Proxy();
proxy.chuzu();

Looking at the above usage, the proxy class is completely unnecessary on the client side, as long as the Proxy class is used, which can be well used in situations where some business logic process needs to be protected. The proxy mode can protect the proxied classes that need to be protected.

Other instructions

Like decorator mode 1, proxy mode also well embodies the object-oriented idea of open to extension, closed to modification principle;
Proxy pattern, which can use interfaces or abstract classes to specify common interfaces :(the JAVA proxy pattern is provided below)

1. Abstract class abstract mode;



public abstract class House {
    public void abstract chuzu();
} public class Fangdong extends House {
    private String room = "Room name" ;
    @Override
    public void chuzu() {
        System.out.println(room);
    }
} public class Proxy extends House {
    private Fangdong fangdong;
    public Proxy() {
        this.fangdong = new Hangdong();
    }
    @Override
    public void chuzu() {
        this.fandong.chuzu();
        System.out.println( "Payment of intermediary fee after letting" );
    }
} // use
House house = new Proxy();
house.chuzu();

2. Interface Interface:


interface House {
    public void chuzu();
} public class Fangdong implements House {
    private String room = "Room name" ;
    @Override
    public void chuzu() {
        System.out.println(room);
    }
} public class Proxy implements House {
    private Fangdong fangdong;
    public Proxy() {
        this.fangdong = new Hangdong();
    }
    @Override
    public void chuzu() {
        this.fandong.chuzu();
        System.out.println( "Payment of intermediary fee after letting" );
    }
} // use
House house = new Proxy();
house.chuzu();


Related articles: