Simple and simple Java based proxy design pattern

Source: Internet
Author: User

First, a primer

We go to the technology market for their own machine to add some luxury accessories, a lot of diyer like to find agents, because in the agents there is not only the quality assurance, but also the price and after-sales service will be much better. Customers through the agent to get what they want, but also enjoy the agents of the additional services, and manufacturers through agents to promote their products, and can be some sales services to the agent to complete the task (of course, agents and manufacturers to share risk, distribution of profits), So that they can spend more time in the design and production of products.

In the United States, any enterprise's products want to get the market to sell it must go through the agent of this link, otherwise it is illegal. It seems that agents play a key role in the business operation. Careless of the topic pull far, back to, then in our object-oriented program design, there will be agents such role? The person who came to see this article will certainly not say: No!

Then follow this article to see the wonderful agent mode.

II. Definitions and classifications

The proxy pattern is defined in design mode by providing a proxy for other objects to control access to that object. To be blunt, in some cases the customer does not want or cannot directly refer to an object, and the proxy object can mediate between the customer and the target object, removing the content and service that the customer cannot see or adding additional services that the customer needs.

So when do you want to use proxy mode? When the existing methods are used to improve or modify the original method, there are two kinds of improvements: Modify the original method to adapt to the current use, or use a "third party" method to invoke the original method and the results of the method produced a certain degree of control. The first approach is a clear violation of the "openness to expansion, to modify the close" (open and closed principle), and in the original method of modification may make the original class functions become blurred and diversified (as is now the enterprise diversity), and the second way you can divide the function of the more clear, to facilitate the maintenance behind. So to a certain extent the second way is a better choice!

Of course, then said back, if it is a very small system, the function is not very complex, then the use of proxy mode may appear bloated, as the first way to fast. This is like a family of three, housework is done by housewives or a nanny to complete is more reasonable, there is no need to hire a number of Nannies layer agent:

According to the "Java and mode" of the classification of Agent mode, Agent mode is divided into 8 kinds, here will be several common, important listed as follows:

1. Remote Proxy: Provides a local-domain representation object for an object located in a different address space. For example: You can put a machine in a corner of the world through a proxy illusion as part of your local area network.

2. Virtual agents: Created when the real need for a resource to consume a large or complex object is deferred. For example, if a large picture takes a long time to display, when the picture is included in the document, the document is opened using an editor or a browser, and the large image may affect the reading of the document, and a picture proxy is needed instead of the real picture.

3. Protection (Protect or access) Agent: Controls access to an object. For example: In the Forum, different identity landing, have the permissions are different, the use of proxy mode can control permissions (of course, the use of other methods can also be achieved).

4. Intelligent reference (Smart Reference) Agent: Provides additional services than the target object. For example: Record access traffic (this is a simple example), provide some friendly tips and so on.

Proxy mode is a useful model, from the "small structure" of several classes to the "large structure" of the huge system can see its shadow.

Third, the structure

Agent mode in the "agent" to implement the agent task, you must and the agent of the "vendor" to use a common interface (you can imagine as a product). So naturally you would think of using an abstract class or interface (recommended) in Java to implement this common interface. The agent mode then has three roles:

1. Abstract theme role: Declares a common interface between real and agent topics.

2. Agent Theme role: internally contains references to real topics, and provides the same interface as the real theme role.

3. Real-World theme roles: Defining Real objects.

Use the class diagram to indicate the relationship between the following three:

Of course, the picture shows a specific situation in the proxy pattern. Agent mode can be very flexible to use other ways to implement, so it is very different from the figure shown.

Perhaps, now that you have a macro understanding of the agent model, let's take a look at how to actually use the proxy 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.