Java-based agent design patterns

  Abstract: Based on the Java proxy design patterns 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="279" height="86" align="center" valign="top"> </ td> <td width="405" valign="top"> I. Introduction 

  We go to market technology for their own machines add luxury accessories, like many DIYer find agents, because the agents get things where not only guaranteed quality, and price and customer service, will be much better.    Agents have been customers of the things they want, but also enjoy agents additional services and manufacturers through agents will be out to promote their own products and services can be some of the tasks to the sales agents to complete ( Of course, agents and vendors to share the risk, distribution of profits), so can he can spend more things in the design of the products and the production. 
  </ Td> </ tr> </ table> 

  In the United States, any enterprise in order to get products on the market forward selling agents must be a part of it, otherwise it will be illegal.    Appear in the commercial operation of the agents played a very key role.    Careless pull far away, the topic of conversation, come back to, in our object-oriented programming design, the agents will not have such a role?    Of this article, people will definitely not be said: No! 

  The article then follow the model to look at the wonders of agents it. 

  Second, definitions and classifications 

  Acting model in the design mode is defined as: to provide a target for other agents to control the object of this visit.    Said bluntly that, in some cases, the client does not want or can not directly invoke an object, and agents can target customers and play an intermediary between the target, can not be removed, see the content and services, or add additional services to the needs of customers . 

  So when to use proxy mode?    In the existing methods used by the original time and the need to improve or modify methods, to improve this time there are two options: to amend the original method to adapt to the current use, or use a "third party" method Calling the original method and the results of the method of a certain control.    The first method is a clear violation of "the expansion of open, closed on the revised" (opening and closing principles), but also in the original method may result in modification of the original function becomes blurred and diversified (the same as now diversified enterprises) , and use the second method can be more clear and functional, to the maintenance of the back.    So to a certain extent, the second approach is a better choice. 

  Of course, Huayoushuihuilai, if it is a small system, function is not very complicated, then use a proxy model may seem cumbersome, as a way to the first of the fast.    This is like a three homes, household chores or by the housewife to complete a nanny is more reasonable, it does not need nanny employed in several layers of agents:) 

  According to the "Java and model," in the classification of the proxy mode, agent model is divided into eight kinds, will be here several common and important are listed below: 

  1. Remote (Remote) agents: a different address space in the target to provide a local representative objects.    For example: You can be a corner somewhere in the world a false impression of the machine through a proxy as a part of your LAN. 

  2. Virtual (Virtual) agents: the need to consume a lot of resources or more complicated objects delayed when the real needs of the creation.    For example: If a big picture, the need to spend a very long time to show up, then when the picture contained in the document, the use of the editor or browser to open this document, the big picture may affect the file reading , then Proxy picture needs to be a substitute for real picture. 

  3. Protection (Protect or Access) Agent: control of the access to an object.    For example: in the forums, landing different identities, has a different mandate, the use of proxy mode can be controlled authority (of course, also use can be achieved in other ways). 

  4. Smart references (Smart Reference) agent: the target audience than additional services.    For example: the flow of records access (this is a more simple example), provide some Tips and so on. 

  Acting model is a more useful model, from several categories of "small structure" to the huge system of "structure" can be seen its shadow. 

  Third, the structure 

  Acting in the mode of "agent" in order to achieve agent tasks, we must be agents and "manufacturers" use of a common interface (you can imagine for products).    So naturally you think of java in the use of an abstract class or interface (recommended) to achieve this common interface.    Acting alone model was composed of three roles: 

  1.    The role of abstract themes: the actual theme of the statement and the common theme agent interface. 

  2.    Acting role theme: internal contains references to reality theme, and provide real theme and the role of the same interface. 

  3.    Reality theme roles: the real definition of the object. 

  Use of graphs to show that the relations among the three as follows: 


  Of course, shown on the map is a model agent specific circumstances.    The agent model can be very flexible to use other means to achieve this on the map and shown to be much different. 

  Perhaps now you have agents already have a macro mode awareness, Below we look at how the actual use of proxy mode. 

  4, for example 

  Forum to have registered users and visitors authority as a different example: registered users have posting revise its own registration information, revise its own posts, and other functions and tourists can only see the posts of others, no other authority.    To simplify the code, a better model showed agents of the skeleton, we realize here only posting authority control.    First of all, we first achieve an abstract theme of the role of MyForum, define the true theme and the theme of the common interface agents - posting function. 

  Code as follows: 

  Public interface MyForum 

  ( 

  Public void AddFile (); 

  ) 

  Thus, the real theme of the role and the role of agent theme to achieve this interface.    The theme of the role of truth is the basic method of this interface content filled come.    Therefore, it will not repeat them here as well.    We concentrate on the main theme of the key roles of agents.    Acting general theme of the role of the code as follows: 

  Public class MyForumProxy implements MyForum 
  ( 
  Private RealMyForum forum; 
  Private int permission; / / Privilege value 

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

  / / Interface Implementation 

  Public void AddFile () 
  ( 
  / / Set the time to meet authority will be able to run operations 
  / / Is a constant type Constants 
  If (Constants.ASSOCIATOR == permission) 
  ( 
  Forum.AddFile (); 
  ) 
  Else 
  System.out.println ( "You are not a associator of MyForum, please registe!"); 
  ) 
  ) 

  Acting on the realization of this model functions.    Of course, you can also add the Agency their own methods to achieve additional services, such as the number of visits to statistics posted record user's login and more. 

  Another very common example of the use of proxy mode on the large picture here is the control.    In our common site visit by the above information, I do not know you have noted that the picture here is a narrow place, when people have to carefully read this picture, you can activate by clicking on the picture to a link, in a new website opened depends on the picture.    This for improving the speed here is very good, because not everyone has to carefully look at the map information.    This situation can use a proxy model to the full realization.    Here, I will express ideas out, as realized as a result of reasons, not stated as such in the way of B / S mode feasibility of the true, I do not have confirmation, but was just abstract thought.    If it is not feasible, then the case can be placed in a C / S down in, this is absolutely no problem and, in many design patterns on the use of books and articles.    Two ways of achieving interested can come to give it a try. 

  We visited in the browser page is called the loading picture is not true method, but the method of targeting agents, in this object, the first thread to use a browser loading a diminished version of the picture, and background to the use of another thread calls the real big picture loading methods will be loaded into the local picture, and when you want to view the picture at its new website in the show.    Of course, if you want to browse the picture has not loaded when successful, can start a thread to display messages until loading success. 

  This proxy mode functionality embodied in the head in the above - through a proxy to the real picture will be loaded into the background to the operation, it does not affect the prospects here. 

  V. GENERAL 

  Acting model can be coordinated call and call, to a certain extent, reduce the coupling of the system.    But we must remember that in front of the use of Agent model stresses the conditions, otherwise the use of the agent model will not have good results, perhaps also the problem 

  </ Td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply