The agent mode of JavaScript

Source: Internet
Author: User
Tags abstract object interface log string client

Agent Mode description

Description: As the name implies is to use a class to replace another class to perform the method function, this pattern is somewhat similar to the decoration pattern, not the same, the proxy mode is to initialize the proxy object class instead of the client, and the decoration mode is implemented by the interface or the initial adorner parameter reference.

In the dynamic object-oriented language, the agent mode plays the role of controlling and modifying the proxy class, and also plays a full hidden protection role for the proxy class. The proxy class is only indirectly initialized when we need it;

Scenario Description:

It is common to rent a house example, tenants want to rent a house, the landlord to rent out the house, but the tenant and the landlord did not how to find a house or at home waiting for someone to look at the house, went to find a housing intermediary. The landlord entrusted the room to the intermediary agency for rent and sale, when the right people need it, intermediary to see the room, give rent live, so help the landlord to rent out the room, the tenant entrusted room is intermediary help recruit request room position condition, intermediary help tenant find a house, both rent live, so that tenants rented to live place;

Instance source code

According to the above example to achieve;

1. Landlord rental category;

function Fangdong () {
This.room = ' room name ';
}
Fangdong.prototype.chuzu = function () {
Console.log (' Landlord rent Room: ' + this.room ');
}

2. Intermediary Agent class:


function Proxy () {
This.fangdong = new Fangdong ();
}

Proxy.prototype.chuzu = function () {
This.fangdong.chuzu ();
Console.log (' rental after the intermediary fee ');
}



3. How to use:

The tenant asked the intermediary to help find the house;
var proxy = new Proxy ();
Proxy.chuzu ();

Look at the use of the above, the proxy class at the client is completely unnecessary, as long as the use of proxy can be very good to use in the kind of need to protect certain business logic processes in the scenario, the use of proxy mode, can be a good protection of the agents need to protect the class;

Other Notes

As with the decorator mode, the agent mode is also a good embodiment of the object oriented thinking to expand the open, to modify the closure of the principle;

Agent mode, you can use interfaces or abstract classes to standardize common interfaces: (The following provides Java-mode proxy mode)

1. Abstract form of abstraction;


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 ("Rent after the intermediary fee");
}
}

Use
House House = new Proxy ();
House.chuzu ();



2. Interface Interface mode:


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 ("Rent after the intermediary fee");
}
}

Use
House House = new Proxy ();
House.chuzu ();



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.