Agent Mode Description
Note: As the name suggests, it is to use one class to replace another class to perform method functions. This pattern is a bit similar to the decorative mode. The difference is that the proxy mode replaces the client to initialize the proxy object class, while the decorative mode is executed by interface or reference to the parameter of the initial decorator.
In dynamic object-oriented language, the proxy mode plays the role of controlling and modifying the proxy class, and also plays a full hidden and protected role for the proxy class; the proxy class is indirectly initialized and called only when we need it;
Scene description:
A very common example of renting a house. Tenants want to rent a house and the landlord wants to rent out the house, but neither the tenant nor the landlord have much time to find a house or wait for someone to see the house at home, so they go to find a real estate agent together. The landlord entrusts the room to the agency for renting and selling. When there is a suitable person, the agent will take the room to see it and pay the money to rent it, so that the landlord will rent the room out; the tenant entrusts the house to the agency to help the room location required by the agency. After the agent finds the tenant, the two will pay the money to rent it, so that the tenant can rent it to the place where he lives;
Instance source code
Follow the above example to achieve;
1. Landlord rental category;
The code copy is as follows:
function Fangdong() {
this.room = 'room name';
}
Fangdong.prototype.chuzu = function() {
console.log('Landholder rents room: ' + this.room);
}
2. Intermediary agent class:
The code copy is as follows:
function Proxy() {
this.fangdong = new Fangdong();
}
Proxy.prototype.chuzu = function() {
this.fangdong.chuzu();
console.log('Carry agency fees after renting');
}
3. How to use:
The code copy is as follows:
//The tenant asks an agent to help find a house;
var proxy = new Proxy();
proxy.chuzu();
Look at the above usage. The proxy class is not available at all on the client side. Just use the Proxy class, which can be used well in a scenario where certain business logic processes need to be protected; using the proxy mode can be used well in a proxy class that needs to be protected;
Other Instructions
Like the decorator mode, the proxy mode also well reflects the principle of object-oriented thinking of openness to extension and closing modification;
Proxy mode, you can use interfaces or abstract classes to regulate common interfaces: (The following provides JAVA proxy mode)
1. Abstract class abstract method;
The code copy is as follows:
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("Pay agency fee after renting");
}
}
//use
House house = new Proxy();
house.chuzu();
2. Interface method:
The code copy is as follows:
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("Pay agency fee after renting");
}
}
//use
House house = new Proxy();
house.chuzu();