Design Patterns of Proxy

  Abstract: Proxy Model 

  Understand and use the design model, we can cultivate good habits of object-oriented programming, but in practical applications, we can如鱼得水enjoy highly of fun. 

  Proxy is more use of a model, and more variety, covering applications from structure to the structure of the whole system, Proxy agent is the meaning, we may have concepts such as a proxy server, agent concept can be explained as follows: the starting point to Destinations between a middle layer, which means agents. 

  Design Patterns in the definition: provide a target for other agents to control the object of this visit. 

  Why use Proxy? 
  1. Licensing mechanism different levels of users on the same object with different access rights, such as Jive Forum system, the use of a licensing mechanism Proxy control, there are two kinds of people visit the Forum: registered users and visitors (non-registered users), in Jive on the adoption of similar ForumProxy such agents to control both users access to the Forum. 

  2. A client can not be directly manipulated to a certain object, but it is essential that the object and interactions. 
  For example two specific situations: 
  (1) If that object is a great picture is the need to take a long time to show up, then when the picture contained in the document, the use of the editor or browser to open this document, open the file must be very rapid , can not wait for complete processing of the picture, then the need to be a picture Proxy to substitute for real picture. 

  (2) If that object in a certain remote Internet servers directly operated the network speed of the object because the reasons may be quite slow, we could be using to replace the Proxy that object. 


  In general principle is that the big spending targets, only use it when creating this principle we can save a lot of valuable Java memory. Therefore, some people think that Java memory consuming resources, I think that that is what programmers have certain ideas relations. 

  How to use Proxy? 
  Jive system as an example to the Forum, visit the Forum There are many types of users: Forum users registered general manager system administrator tourists, ordinary users can register to speak Forum managers can manage his mandate as a forum for the system administrator can manage all matters, and management of these rights is the use of Proxy completed. 

  Jive Forum is the core interface, in the Forum on display in the main operation Forum, described the Forum as the Forum names of the access and changes made to delete editing posts. 

  ForumPermissions defined in a variety of user-level permissions: 

  Public class ForumPermissions implements Cacheable ( 
  / ** 
  * Permission to read object. 
  * / 
  Public static final int READ = 0; 

  / ** 
  * Permission to administer the entire sytem. 
  * / 
  Public static final int SYSTEM_ADMIN = 1; 

  / ** 
  * Permission to administer a particular forum. 
  * / 
  Public static final int FORUM_ADMIN = 2; 

  / ** 
  * Permission to administer a particular user. 
  * / 
  Public static final int USER_ADMIN = 3; 

  / ** 
  * Permission to administer a particular group. 
  * / 
  Public static final int GROUP_ADMIN = 4; 

  / ** 
  * Permission to moderate threads. 
  * / 
  Public static final int MODERATE_THREADS = 5; 

  / ** 
  * Permission to create a new thread. 
  * / 
  Public static final int Create_THREAD = 6; 

  / ** 
  * Permission to create a new message. 
  * / 
  Public static final int Create_MESSAGE = 7; 

  / ** 
  * Permission to moderate messages. 
  * / 
  Public static final int MODERATE_MESSAGES = 8; 

…..

  Public boolean isSystemOrForumAdmin () ( 
  Return (values [FORUM_ADMIN] | | values [SYSTEM_ADMIN]); 
  ) 

…..

  ) 


  Therefore, the Forum is in the competence of the various operations and ForumPermissions definition of a user-level relations, as the realization of Interface Forum: ForumProxy such relationship is linked to, for example, modify the name Forum, the Forum only managers or system Managers can modify the code as follows: 

  Public class ForumProxy implements Forum ( 

  Private ForumPermissions permissions; 
  Private Forum forum; 
  This.authorization = authorization; 

  Public ForumProxy (Forum forum, Authorization authorization, 
  ForumPermissions permissions) 
  ( 
  This.forum = forum; 
  This.authorization = authorization; 
  This.permissions = permissions; 
  ) 

…..

  Public void setName (String name) throws UnauthorizedException, 
ForumAlreadyExistsException
  ( 
  / / Is the only forum administrators or system can be amended if the name (permissions.isSystemOrForumAdmin ()) ( 
  Forum.setName (name); 
  ) 
  Else ( 
  Throw new UnauthorizedException (); 
  ) 
  ) 



  ) 



  DbForum Interface Forum and is the true realization, as an example to change the name Forum: 

  Public class DbForum implements Forum, Cacheable ( 


  Public void setName (String name) (throws ForumAlreadyExistsException 

….

  This.name = name; 
  / / Here will be truly a new name saved to the database saveToDb (); 

….
  ) 





  ) 


  All relate to revise the name of the Forum, other procedures must first be dealing with and ForumProxy from ForumProxy decide whether there is a certain authority to do the same thing, ForumProxy is a true "gateway", "security agent system." 

  In peacetime applications, must inevitably involve the authority or security system, whether you have the use of Proxy unconscious, you have the actual use of the Proxy. 

  We continue to discuss with Jive deep point, the following should be related to the factory model, and if you do not understand the factory model, I see another article: Design Model Factory 

  We already know, through the use of Forum ForumProxy, Jive Forum in the creation of a model is to use Factory, there is a general category ForumFactory abstract, in this abstract class, call ForumFactory through the getInstance () method, used here Singleton (also One design pattern, as on many articles, I do not write, read here), getInstance () returns the ForumFactoryProxy. 

  Why not return to ForumFactory, and the realization of ForumFactoryProxy ForumFactory return? 
  The reason is obvious, you need to determine whether agents authority to create a forum. 

  ForumFactoryProxy we see in the following code: 

  Public class ForumFactoryProxy extends ForumFactory ( 
  Protected ForumFactory factory; 
  Protected Authorization authorization; 
  Protected ForumPermissions permissions; 

  Public ForumFactoryProxy (Authorization authorization, ForumFactory factory, 
  ForumPermissions permissions) 
  ( 
  This.factory = factory; 
  This.authorization = authorization; 
  This.permissions = permissions; 
  ) 

  Public Forum createForum (String name, String description) 
  Throws UnauthorizedException, ForumAlreadyExistsException 
  ( 
  / / Only system administrators can create forum 
  If (permissions.get (ForumPermissions.SYSTEM_ADMIN)) ( 
  Forum newForum = factory.createForum (name, description); 
  Return new ForumProxy (newForum, authorization, permissions); 
  ) 
  Else ( 
  Throw new UnauthorizedException (); 
  ) 
  ) 



  CreateForum method is the return of ForumProxy, Proxy as a wall, and the only other procedures Proxy interactive operation. 

  Noting there are two Proxy: ForumProxy and ForumFactoryProxy. Behalf of the two different responsibilities: Use Forum and the creation of Forum; 
  As for why objects and will be used to create separate object, which is why the use of the Factory model reason: in order to "package" and "distributed", in other words, as far as possible, single function, and easy to maintain amended. 

  Jive system in other forums such as the creation and use of Posts, in accordance with the ideas from the Forum. 

  We discussed more than how to use a licensing mechanism Proxy visit Proxy users can also hide another called copy-on-write the optimization method. Copy of a huge and complex objects is a great operating expenses, if the copy process , none of the original targets have been revised, then the copy costs would not be necessary. agents with copies of this delay. 

  For example: We have a great Collection, such as specific hashtable, many clients will visit at the same time with it. One of the client to conduct continuous data acquisition, this time demanding that other clients can no longer add or delete hashtable Dongdong. 

  The most direct solution: the use of the lock collection, this particular client access to the lock, continuous data acquisition, and then release lock. 
  Public void foFetches (Hashtable ht) ( 
  Synchronized (ht) ( 
  / / Data acquisition for specific action .. 
  ) 

  ) 


  However, this approach may be locked Collection will be very long period of time, during this period of time, other clients will not be able to visit the Collection of. 

  The second solution is to clone the Collection, then let continuous data acquisition clone out against the operation that Collection. Premise of this proposal is that the Collection can clone, and must provide the depth clone method. Hashtable provides the its clone, but not the Key value and the target clone, the Clone special meaning to refer to the article. 
  Public void foFetches (Hashtable ht) ( 


  Hashttable newht = (Hashtable) ht.clone (); 

  ) 

  Another problem, as against the target of clone from operations, if the original parent by the other client operating modified, then out to clone the object of the operation will be meaningless. 

  Finally solution: we can modify and other clients after the completion of a clone, that is to say, this particular client first by calling a clone is the means to carry out a series of data acquisition operations. Fact not really a target copy, until other clients modify this object Collection. 

  Proxy use of this programme to achieve this is copy-on-write operations. 

  Proxy Application of a wide range of now popular distributed computing methods, such as RMI and Corba are Proxy mode applications. 





  ↑ 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