The 16th chapter of the design pattern-proxy mode (Java implementation)

Source: Internet
Author: User

The 16th chapter of the design pattern-proxy mode (Java implementation)

" now the circle of friends is so painful," he asked. "What do you say?" " a bunch of agents, all kinds of selling things, looking good annoying." "Ah, delete the chant." "All are friends, where the nerve to delete ah." "This is also 、、、 hey, forced to livelihood, no way." Fortunately I don't play. "Right, you are not the originator of the agent, as a proxy model, how do you feel." "" to the agent of the way to govern the body of the agent AH. "

Self-introduction of proxy mode

The recent appearance rate is super high, oh no, has been very high appearance rate is said that the famous model, that is, I-agent mode is also. The definition for me is as follows: Provide a surrogate or placeholder for another object to control access to it. Translation comes to say: Provide an agent for other objects to control access to this object. The general class diagram is as follows:

  The proxy mode is also called the delegate mode, which is a basic design technique. Many other patterns, such as strategy, visitor patterns, are essentially proxy patterns in more special situations, and proxy patterns can provide very good access control in everyday applications. The dynamic proxy pattern is used in the form element mapping in Struts2.

Self-analysis of proxy mode

The benefits of using me are as follows:

    • Clear responsibilities. The real role is to realize the actual business logic, not to care about other non-responsibility transactions.
    • High extensibility, the specific theme role can change at any time, as long as it implements the interface, regardless of how it changes, you can use the interface to use the method.

Implementation of proxy mode

The specific situation we would like to sell a friend circle to eat, well, sell spicy bar. First is a company interface class that implements the simple functionality of each company:

1  Public Interface icompany{2     // make a brand. 3      Public void Brand (String brand); 4     // Sell 5      Public void sell (); 6 }

  Then is the company's specific implementation class:

1  Public classCompanyImplementsicompany{2     PrivateString name = "";3     //Constructor Gets the name4      PublicCompany (String name) {5          This. Name =name;6     }7 8     //registered trademarks9      Public voidBrand (String name) {TenSystem.out.println ("Company's name is:" +name); One     } A  -     //Sell Products -      Public voidSell () { theSystem.out.println ("Last three days clearance, buy a box to send three boxes ... "); -     } -}
View Code

  Finally, the most important proxy implementations are:

1  Public classProxyImplementsicompany{2     PrivateIcompany CP =NULL;3     //the company that passed the agent through the constructor4      PublicProxy (icompany cp) {5          This. CP =CP;6     }7     //agents in the circle of friends to play brand8      PublicViod Brand (String name) {9          This. Cp.brand (name);Ten     } One  A     //agent is selling things. -      Public voidSell () { -          This. Cp.sell (); the     } -}
View Code

  Well, basically the realization is this, the last friendship sent to the scene class one:

1  Public classclient{2      Public Static voidMain (string[] args) {3         //Weilong hot strips, my favorite4Icompany CP =NewCompany ("Guardian dragon"));5         //define an agent to sell spicy strips6Icompany proxy =NewProxy (CP);7         //Agent Brand8Proxy.brand ("Guardian dragon"));9         //Agent for SaleTen Proxy.sell (); One     } A}
View Code

  At this point, the implementation is partially complete.

Application scenario of proxy mode

Scene really number also countless, looking for someone lol, warcraft all kinds of leveling, looking for agents to sell the house to buy what the old more. Here are a few common agent scenarios:

    • Remote proxy: Provides a local proxy representation for an object in a different address space.
    • Virtual Proxy: Create overhead as needed very large object.
    • Protection agent: Controls access to the original object.
    • Smart guide: Instead of a simple pointer, it performs some additional operations when accessing an object.

  Jing Ke Thorn qin king, design mode heart hidden, well, today to this end.

PS: This blog welcome forwarding, but please specify the blog address and author ~

Blog Address: http://www.cnblogs.com/voidy/

Blog: http://voidy.net

<. ))) ≦

The 16th chapter of the design pattern-proxy mode (Java implementation)

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.