[design mode] JavaScript proxy mode

Source: Internet
Author: User

Proxy mode description

Description: As the name implies is to use a class to replace another class to execute the method function, this pattern is a little similar to the decorative mode, unlike the proxy mode is to replace the client initialization of the proxy object class, and the decoration mode is used in the interface or the method of the initial decorator parameter reference to execute.

In the dynamic object-oriented language, the proxy pattern is the function of controlling and modifying the proxy class, and it also has the full hiding protection function to the proxy class; The proxy class is indirectly initialized only when we need it.

Scenario Description:

It is common to rent a house, tenants want to rent a house, the landlord to rent out the house, but tenants and landlords have no time to find a house or at home waiting for someone to look at the house, together to find housing intermediary. The landlord entrusted the room to the intermediary agent for rent and sale, the appropriate person needs, the intermediary with the room, pay rent live, so help the landlord to rent out the room, the tenant commissioned the room is the intermediary to help recruit the room location conditions, intermediary help tenants find a house, the two rent live, so that tenants rent to live place;

Example Source

This is achieved by the example above;

1. Landlord Rental class;

function Fangdong () {    this' room name '= function () {    Console.log ("this.");}

2. Mediation Agent Class:

function Proxy () {    thisnew= function () {    this. Fangdong.chuzu ();    Console.log (' post-rental fee ');}

3. How to use:

// The tenant asked the intermediary to help find the house; var New Proxy ();p Roxy.chuzu ();

Look at the use of the above, the proxy class at the client is completely unnecessary, as long as the use of the proxy class can be used in the kind of situations that need to protect some business logic process, the use of proxy mode, can be very good to protect the proxy class needs to be protected;

Other Notes

As with the decorator pattern, the proxy mode also embodies the object-oriented idea of the extended opening, the principle of the closure of the modification;

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

1. Abstract class abstraction mode;

 Public Abstract classHouse { Public void AbstractChuzu ();} Public classFangdong extends House {PrivateString hostel ="Room Name "; @Override Public voidChuzu () {System. out. println (guest); }} Public classProxy extends House {PrivateFangdong Fangdong;  PublicProxy () { This. Fangdong =NewHangdong (); } @Override Public voidChuzu () { This. Fandong.chuzu (); System. out. println ("intermediary fees after rental"); }}//UseHouse House =NewProxy (); House.chuzu ();

2. Interface Interface mode:

InterfaceHouse { Public voidChuzu ();} Public classFangdongImplementsHouse {PrivateString hostel ="Room name”; @Override Public voidChuzu () {System.out.println (); }} Public classProxyImplementsHouse {PrivateFangdong Fangdong;  PublicProxy () { This. Fangdong =NewHangdong (); } @Override Public voidChuzu () { This. Fandong.chuzu (); System.out.println ("Post-lease payment agency fee"); }}//UseHouse House =NewProxy (); House.chuzu ();

[design mode] JavaScript proxy mode

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.