Proxy mode in Java Design Mode

Source: Internet
Author: User

Proxy mode:
ArticleLink: http://www.iteye.com/topic/517835
Proxy mode: provides a proxy object for an object, and the proxy object controls the reference of a specific object.

Proxy refers to a role that represents another role to take action. Just like in life, a wine manufacturer does not directly sell wine to retail customers,
The customer does not need to look for factories to drink red wine. He just needs to find the local agent of the manufacturer,
The specific wine factory is there, and the customer does not have to worry about it. The agent will help him deal with it.
Roles involved in proxy mode:
1: Abstract topic roles. public interfaces of proxy themes and real themes are declared, so that proxy themes can be used in any place where real themes are needed.
2: The proxy topic role contains the reference of the real topic, so that you can operate on the real topic at any time. The proxy topic has contributed to providing the same interface as the real topic,
The proxy topic can replace the real topic at any time,
You can not only create or delete a real topic, but also intercept a real topic before it is called or perform some operations after it is called.
3: Real proxy object. It defines the specific object represented by the proxy role.
Java mainly supports the proxy mode through the proxy class and the invocationhandler interface.
The interception mechanism is one of the important ways to use the proxy mode,
In addition to interception, the proxy mode is also commonly used for resource loading. When the resources to be loaded are large, we can let real topic roles load Resources in the background, the proxy topic role is responsible for handling the waiting prompt information at the front end.
There is also the authorization mechanism, through which the proxy can intercept real theme capabilities, to control the access permissions of real theme.
When talking about proxies, we often need to talk about dynamic proxies,
If the agent is static, it looks like it is no different from the decorator, so the agent is often associated with dynamic and reflection, and is always related to the ability of the language itself ......
The proxy mode is usually used to control the original object and does not add new behaviors, such as what the original object was or what it was, and common such as cglib plus the transaction mechanism, but no new behaviors were added, the original service has not changed.
However, the decoration mode usually adds new behaviors, and the behavior can be dynamically combined and can be in any order. For example, you can set the color of the wall, set the background color, then red, and then green, you can also set the background color first,
Click green and then red.
Both the proxy mode and the decoration mode can be constructed to reference the original class in the new class and form the delegate of the original class. In this way, the original class can be controlled and new behaviors can be created, you can also add other controls.
However, is there an aggregation relationship between the agent and the agent ?? The aggregation relationship is the relationship between the whole and the local, but the whole part can survive, such as airports and airplanes,
The relationship between the two in the proxy mode should be dependent, more like the user-a relationship. Because it is dependency, we use interfaces for decoupling, instead of displaying dependency Direct Injection implementation classes.

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.