First, the investigator we go to the technology market to add some extravagant accessories for your own machine, many DIYERs like to go to the agent, because the things you get in the agent are guaranteed not only, 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, definitions and classified proxy mode definitions in design patterns are: Provide 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, housework is completely reasonable by housewives or a babysitter. It does not need to hire several babysitting agents :) Classification of proxy models in Java and Mode books The agent model is divided into 8 kinds. Several common, important enumerations are as follows: 1. Remote proxy: Provide a local area representative object for 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, the "agents" in the structural agent model want to realize the agent task, and the "manufacturer" "manufacturer" must be used by 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 the class diagram to indicate that the relationship between the three three is as follows: Of course, it is a specific situation in the proxy mode. 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. IV. For example, the authority of the registered users and tourists in the forum is different: the registered user has the post, modify its own registration information, modify its own post, and the tourists can only see others. No posts without 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 ();} This interface is implemented in real theme roles and proxy theme characters. 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: