Keywords: proxy mode Java
First, the primer
We went to the technology market to add a luxury accessories for our own machines. Many DiYer like to find the agent, because the things you get there are not only quality, but also the price and after-sales service will be much better. The customer has got the things you want through agents, and also enjoyed the additional services of the agent; and manufacturers promote their own products through agents, and they can hand over some sales services to agents to complete ( Of course, the agent is going to share the risk and distribute profits with the manufacturer, so you can spend more mind to design and produce the product.
In the United States, any company's products must go to the market to sell, they must pass the link, otherwise it is illegal. It seems that agents play a critical role in business operations.
I accidentally pulled the topic, turning back, then in our object-oriented programming, will there be a role in the agent? People who come to see this article must not say: no!
Then follow this article to see the wonder of the proxy model ~~~~
Second, definition and classification
A: What's proxy mode in english?
B: IT is proxy or surrogate!
The definition of proxy mode in design mode is: Providing a proxy for other objects to control access to this object. To put it bluntly, in some cases, customers don't want to directly reference an object, and the agency object can play a mediation role between the customer and the target object, remove the content and services that the customer cannot see or add additional services to customers. So when is it necessary to use a proxy mode? When the existing method is used, it is necessary to improve or modify the original method. At this time, there are two improvement options: modify the original method to accommodate the current use, or use a "third party" method. Call the original method and perform certain control on the results generated. The first method is to make it clear about "Open, the modification is closed" (opening and closing principle), and in the original method may make the original class function to be blurred and diversified (just like now ), While using the second way, the functional division can be clearer and contribute to the back maintenance. So a certain degree of way is a better choice! Of course, the words come back, if it is a small system, the function is not very complicated, then the use of proxy mode may appear bloated, it is not as fast as the first way. This is like a family of three, family live is complete by the housewife or a babysitter. It does not need to hire several babysitting devices :)
According to the "Java and Mode" books, the agent model is divided into 8 kinds. Several common, important enumerations are as follows:
1. Remote proxy: Provides a local area representative object to an object in different address spaces. For example: You can use a machine in a corner of the world to make a part of your local area network.
2. Virtual (Virtual): Create a resource consumption or complicated object delay as needed. For example, if a big picture, it takes a long time to display it, then when this picture is included in the document, use the editor or browser to open this document, this big picture may affect the reading of the document, this Need to be a picture proxy to replace the real picture.
3. Protect or Access proxy: Controls access to an object. For example: in the forum, different identity logins, the privileges are different, using proxy mode can control permissions (of course, can be implemented in other ways). 4. Smart Reference Agent: Provides additional services to the target object. For example: the traffic of records (this is a simple example), providing some friendly tips, etc.
Agent mode is a relatively useful mode, from several classes of "small structure" to the "big structure" of a huge system, it can see its shadow.
Third, structure
"Agents" in proxy model To implement a proxy task, you must use a common interface with the "manufacturer" of the agent (you can imagine product). So naturally you will think of this common interface in Java using an abstract class or interface (recommended). So the agent mode has three characters:
1. Abstract Theme Role: Declare the common interface of true subject and agent theme.
2. Agent Theme Role: Internal contains references to true topics and provides the same interface as the real topic role.
3. Realistic topic role: Define real objects.
Use class diagrams to indicate that the relationship between the three three is as follows:
Of course, it is a specific situation in the proxy mode as shown. The proxy model can be implemented very flexible to use other ways, so that there is a big difference in the picture.
Perhaps now, now you have already had a macro understanding of the agent model. Let's take a look at how actually use the agent mode.
Fourth, an example
Different from the permissions of the registered users and tourists in the forum as the first example: The registered users have posts, modify their registration information, modify their own posts; and visitors can only see the posts sent by others, no Other permissions. In order to simplify the code, better display of the skeleton of the proxy mode, we only implement the control of the post permission here.
First we first implement an abstract theme character myforum, which defines the common interface of the real topic and proxy topics - posting function.
code show as below:
Public Interface MyForum
{
Public void addfile ();
}
In this way, the real theme role and the agency theme role must implement this interface. The true subject role is basically filled in the method of this interface. Therefore, it will not be described here again. We put the main energy into the key agent theme role. The agency theme character code is generally as follows:
Public Class MyForumProxy Implements MyForum
{
Private realmyforum forum;
Private int permission; // authority value
Public myforumproxy (int Permission)
{
Forum = new realmyforum () this.permission = permission;
}
// Implemented interface
Public void addfile ()
{
/ / Can perform operation when you meet permission settings?
// constants is a constant class
IF (constants.associator == permission)
{
Forum.addfile ();
}
Else
System.out.println ("You Are Not a Associator of MyForum, please registe!");
}
}
This enables the functionality of the agent mode. Of course, you can also add your own methods to this proxy class to achieve additional services, such as the number of browsing, record the user's login, and so on. There is also a very common way of use of a large number of proxy models. When browsing the information on our common website, I don't know if you have noticed that the image location is reduced. When someone wants to view this picture carefully, you can activate a link by clicking on the image. A new web page opens the picture you want to see. This is very good for increasing the speed of view, because it is not every person to see the information on the track. This situation can be fully implemented using proxy mode. Here I will express my thinking. As for the implementation of the work, I don't say it, as in this manner in the true feasibility of B / S mode, I didn't confirm, just the imagination of air. If it is not a feasible way, this example can be put in a C / S down to implement, this is absolutely no problem, and it is used in many books and articles that describe design patterns. The implementation of the two ways can try it :) We are calling the webpage in the browser that is not a real loaded picture method, but in the method of the agent object, in this object, use one The thread loads a reduced version of the picture to the browser, and use another thread in the background to call the real loaded large picture to load the picture to the local, when you want to browse this picture, put it in the new web page display. Of course, if the picture is not yet loaded when you want to browse, you can start a thread to display the prompt information until it is successful. The function of such agent model is embodied above - through the agent to put the load on the real picture in the background, making it not affecting the front desk browsing.
Five, summary
The proxy model can coordinate the caller and the modifier to reduce the coupling degree of the system to a certain extent. However, be sure to remember the conditions for the use of proxy models earlier, otherwise, using agent models will not only have a good effect, maybe there will be problems ~~