Java-based proxy Design Mode

Source: Internet
Author: User

I. Introduction

We went to the technology market to add luxury accessories for our machines. Many DIYer liked to find agents, because the quality of the items obtained by agents was not only guaranteed, in addition, the price and after-sales service will be much better. The customer obtains what they want through the agent, and also enjoys the additional services of the agent. The manufacturer promotes the product through the agent, in addition, some sales service tasks can be handed over to the agent for completion (of course, the agent must share the risks with the manufacturer and allocate profits ), in this way, you can spend more time on product design and production.

In the United States, any enterprise's products must go through the agency before they can be sold in the market. Otherwise, they are illegal. It seems that agents play a key role in business operations. I accidentally pulled the topic away and looked back. Will there be agents in our object-oriented programming? Those who read this article will certainly not say: No!

So let's take a look at the wonders of the proxy mode with this article.

  Ii. definition and classification

The definition of proxy mode in design mode is to provide a proxy for other objects to control access to this object. To put it bluntly, in some cases, the customer does not want or cannot directly reference an object, and the proxy object can play a mediation role between the customer and the target object, remove content and services that the customer cannot see or add additional services required by the customer.

So when should I use the proxy mode? When you use an existing method, you need to improve or modify the original method. At this time, there are two ways to improve: modify the original method to adapt to the current usage, or use a "third party" method to call the original method and control the results of the method. The first method is obviously against the principle of "Opening and Closing extension and modification ), in addition, modifications made in the original method may blur and diversify the functions of the original class (just like the current enterprise diversification), while the second method can make the function division clearer, it facilitates subsequent maintenance. So to some extent, the second method is a good choice!

Of course, if it is a very small system, and the functions are not very complex, then the use of proxy mode may seem bloated, not as fast as the first method. This is like a three-person house. It is reasonable to have all the housework done by a housewife or a nanny, and there is no need to hire several nanny agents at all :)

According to the classification of proxy modes in Java and patterns, there are eight types of proxy modes. Here we will list several common and important ones as follows:

1. Remote proxy: provides a local area for objects in different address spaces. For example, you can create a machine in a certain corner of the world as a part of your LAN through proxy.

2. Virtual Proxy: a Virtual proxy is created only when a real need for a large resource consumption or complex object latency is required. For example, if a large image takes a long time to be displayed, you can use the editor or browser to open the image when it is included in the document, this large image may affect reading the document. In this case, you need to create an image Proxy to replace the real image.

3. Protection (Protect or Access) proxy: controls Access permissions on an object. For example, in a forum, different identities have different permissions for Logon. You can use the proxy mode to control permissions (of course, other methods can also be used ).

4. Smart Reference Proxy: provides additional services for comparing target objects. For example, record the access traffic (this is a simple example) and provide some friendly tips.

Proxy mode is a useful mode. From the "small structure" of several classes to the "Large Structure" of a large system, you can see its shadow.

  Iii. Structure

In proxy mode, "agent" must use the same interface with the "vendor" of the agent to implement proxy tasks (you can imagine it as a product ). So naturally, you will think of using an abstract class or interface (recommended) in java to implement this common interface. As a result, the proxy mode has three roles:

1. Abstract topic role: Declares the common interfaces of real themes and proxy themes.

2. Proxy topic role: contains the reference to the real topic and provides the same interface as the real topic role.

3. Real topic roles: Define real objects.

Use a class diagram to show the relationship between the three:


Of course, the figure shows a specific situation in the proxy mode. The proxy mode can be implemented using other methods flexibly, which is quite different from the one shown in the figure.

Maybe you already have a macro understanding of the proxy mode. Let's take a look at how to actually use the proxy mode.

   Iv. Examples

Take the permissions of registered users and tourists in the Forum as the first example: registered users have the ability to post, modify their registration information, and modify their posts; visitors can only view posts posted by others without other permissions. To simplify the code and better display the skeleton of the proxy mode, we only implement the post permission control here. First, we need to implement an abstract topic role MyForum, which defines the common interface-post function for real themes and proxy themes.

The Code is as follows:

Public interface MyForum

{

Public void AddFile ();

}

In this way, both the real topic role and the proxy topic role must implement this interface. The actual topic role is to fill in the Method Content of this interface. So here we will not go into detail about its implementation. We focus on key proxy topic roles. The code for the proxy topic role is as follows:

Public class MyForumProxy implements MyForum
{
Private RealMyForum forum;
Private int permission; // permission Value

Public MyForumProxy (int permission)
{
Forum = new RealMyForum ()
This. permission = permission;
}

// Implemented Interface

Public void AddFile ()
{
// The operation can be performed only when the permission settings are met
// Constants is a constant class
If (Constants. ASSOCIATOR = permission)
{
Forum. AddFile ();
}
Else
System. out. println ("You are not a associator of MyForum, please registe! ");
}
}

In this way, the proxy mode is implemented. Of course, you can also add your own methods on this proxy class to implement additional services, such as counting the number of browsing posts and recording user logon information.

Another common example of proxy mode is to control large image browsing. When browsing text and text information on a common website, I wonder if you have noticed that the image is placed in a narrow position. When someone wants to carefully view the image, you can click an image to activate a link to open the desired image on a new webpage. This is very good for improving the browsing speed, because not everyone has to look at the information on the picture carefully. In this case, you can use the proxy mode to fully implement it. I will express my ideas here. I will not express the implementation for work reasons. As for the practical feasibility of this method in the B/S mode, I have not confirmed it, just imagine it out of thin air. If it is not feasible, the example can be implemented in a C/S, which is absolutely no problem and used in many books and articles about the design pattern. If you are interested in the implementation of the two methods, try it.

When accessing a webpage in a browser, we call methods in the proxy object instead of loading images. In this object, first, use a thread to load a thumbnail image to the browser, and then use another thread in the background to call the real method of loading large images to load the image locally, when you want to browse this image, display it on the new page. Of course, if the image has not been loaded successfully when you want to browse, you can start another thread to display the prompt information until it is loaded successfully.

In this way, the proxy mode is fully embodied in the above-the real image is loaded to the background through the proxy, so that it does not affect the foreground browsing.

   V. Summary

The agent mode can coordinate callers and callers, and reduce the Coupling Degree of the system to a certain extent. However, you must remember the conditions for using the proxy mode mentioned above. Otherwise, using the proxy mode will not have a good effect, but may cause problems.

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.