Design pattern Learning--Agent mode

Source: Internet
Author: User

proxy mode, as the name implies, the meaning and our daily life of the agent almost, to give a simple example, we know that our railway station tickets have a lot of train ticket sales point, this train ticket sales point can replace our railway station ticket office let us buy tickets, of course, Sales point compared to the train station ticketing point can also provide telephone booking and advance booking, so that the train ticket sales point can basically achieve the basic function of the station ticketing point, but also can provide other functions relative to the station ticketing point. So, the proxy pattern in our design pattern is that meaning.

In our program, in many classes of methods we always need some additional common functions, such as log and performance optimization operations, if all these classes are added to these same functions, will make our program very redundant, then our proxy mode is working, Proxy mode does not affect the execution of the original method in our program, we use an agent to execute the original function of the class, while adding our common function module, not homogeneous all use this general entity to generate the agent, so that our program becomes very robust (this is the implementation of the spring AOP idea).

In our initial study, we first understand the two basic proxy modes, static agents and dynamic agents:

The static proxy is the inheriting target class that produces a proxy class, and then joins our other functions in the method of the target class.

A dynamic proxy is a target class and a proxy class that implements the same interface, adding a new feature to the proxy class that adds a reference to the target class.

Of course, these basic agents can only implement different functions for specific classes of agents, this nature is unreasonable, for this reason, our dynamic agent in our JDK provides an interface that can generate agents for all objects, as long as the interface is implemented, you can add functionality and return the proxy of the target class.

Another generic agent that has been implemented is the dynamic agent that our cglib provides.

Specific examples are not listed, only for their own thinking to remember to use.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design pattern Learning--Agent mode

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.