Design Patterns - structural - proxy mode

Agent mode (Proxy):

  Proxy mode is to give a certain object provides a proxy, the proxy object controlled by reference to the original object. In some cases, a customer does not want or can not directly refer to an object, and the object agent can act as an intermediary between the client and the target audience. For example shortcut windows desktop is a proxy.

Acting in accordance with the intended use pattern can be divided into:

  1) Remote Agent: Provides a local representative of a target object in a different address space. The different address spaces may be present in the computer, it may be another computer. Such as client calls a web service or wcf service.

  2) Alerts: create a needed resource consumption of larger object so that the object will not actually be created only when needed.

  3) Copy-on-Write agents: a virtual proxy, copy (or clone) to delay only when the client needs, really take action.

  4) protection (Protected or Access) Agent: control access to an object can provide different levels of permissions to different users.

  5) Cache Agent: provides temporary storage for the result, one target of the operation, so that multiple clients can operate these results.

  6) firewalls (FireWall) Agent: protection to prevent malicious users close to the target.

  7) Synchronization (Synchronization) Agent

  8) Intelligent references (Smart Reference) Agent: When an object is referenced, provide some additional operations, such as the number of times this object calls recorded.

Next to the needs of a case to illustrate proxy mode:

  Secondary development of existing toll business information inquiry system, increase the demand:

    1) adding user authentication; 2) adding a log record; 3) required in a loosely coupled manner to add features

     

  1 internal class Program
  2 {
  3     private static void Main(string[] args)
  4     {
  5         /*
  6          * <?xml version="1.0" encoding="utf-8" ?>
  7          * <configuration>
  8          *  <appSettings>
  9          *      <add key="proxy" value="Proxy,Proxy.ProxySearcher" />
 10          *  </appSettings>
 11          * </configuration>
 12          */
 13         var proxy = ConfigurationManager.AppSettings["proxy" ] .Split ( " , " );
 14          var Searcher = (Searcher) the Assembly.Load (Proxy [ 0 ]) the CreateInstance (Proxy [. . 1 ]);
 15          var Result = searcher.DoSearch ( " Yang Guo " , " Yunvxinjing " );
 16      }
 . 17  }
 18 is  
. 19  ///  <Summary> 
20 is  /// (. 1) AccessValidator: authentication class, business class, it is provided a method validate () to implement the authentication.
 21 is  ///  </ Summary> 
22  Internal  class AccessValidator
 23 {
 24      // analog implementation login authentication 
25      public  BOOL the Validate ( String userId)
 26      {
 27          Console.WriteLine ($ " authenticate users in a database {userId} is a valid user? " );
 28          IF (userId.Equals ( " John Steinbeck " ))
 29          {
 30              Console.WriteLine ( " '{0}' login is successful! " , the userId);
 31 is              return  to true ;
 32          }
 33 is          the else 
34 is          {
 35             Console.WriteLine ( " '{0}' Login failed! " , The userId);
 36              return  to false ;
 37 [          }
 38 is      }
 39  }
 40  
41 is  ///  <Summary> 
42 is  /// (2) Logger: logging class, business class, which provides a method for the log () to save the log.
43 is  ///  </ Summary> 
44 is  Internal  class Logger
 45  {
 46 is      // analog implementation, logging 
47      public  void the Log ( String the userId)
 48      {
 49         Console.WriteLine ($ " update the database, user queries {userId} plus. 1! " );
 50      }
 51 is  }
 52 is  
53 is  ///  <Summary> 
54 is  /// (. 3) Searcher: abstract query class, the role of serving as the abstract theme it declares doSearch () method.
55  ///  </ Summary> 
56 is  Internal  interface Searcher
 57 is  {
 58      String doSearch ( String the userId, String keyWord);
 59  }
 60  
61 is  ///  <Summary> 
62 is  ///(4) RealSearcher: specific query classes, act as real theme of the role, which implements the search function, a method DoSearch () to find information.
63  ///  </ the Summary> 
64-  Internal  class RealSearcher: Searcher
 65  {
 66      public  String doSearch ( String userId, String keyWord)
 67      {
 68          Console.WriteLine ($ " User {userId} {keyWord} using the keyword query business information! " );
 69          return  " return specific content " ;
 70      }
 71 is  }
 72  
73 is  ///  <Summary> 
74  ///(5) ProxySearcher: Acting query classes, theme acts as a proxy role, it is the inquiry agency, maintained, references to RealSearcher objects and objects AccessValidator Logger objects.
75  ///  </ Summary> 
76  Internal  class ProxySearcher: Searcher
 77  {
 78      Private Searcher Searcher;
 79      Private AccessValidator Validator;
 80      Private Logger Logger;
 81  
82      public ProxySearcher ()
 83      {
 84          Searcher = new new RealSearcher ();
 85      }
 86  
87      public  String doSearch ( StringuserId, String keyword)
 88      {
 89          // If authentication succeeds, the query is executed 
90          IF ( the this .Validate (userId))
 91          {
 92              String the Result = searcher.DoSearch (userId, keyword); // call the real theme object query method 
93              the this .Log (the userId); // record the query log 
94              return result; // return the query result 
95          }
 96          the else 
97          {
 98              return  null ;
 99          }
 100     }
 101  
102      // create access authentication object and call its Validate () method for authentication 
103      public  BOOL the Validate ( String the userId)
 104      {
 105          Validator = new new AccessValidator ();
 106          return Validator.validate (the userId);
 107      }
 108  
109      // Create object and call logging which the log () method implementation logging 
110      public  void the log ( String the userId)
 111      {
 112          Logger = new new Logger ();
 113          Logger.log (the userId);
114     }
115 }

  Acting sub-four role models:

    1) Theme Interface (Searcher): Public proxy class and method definitions of foreign real theme, but also the real theme of the proxy method proxy class;

    2) the real theme (RealSeracher): real business logic class;

    3) proxy class (ProxySeracher): for transactions relating agents and packaging;

    4) Client: Using the proxy class and themes to complete the work.

The advantages and disadvantages of proxy mode:

  advantage:

    1) proxy model can be used to invoke the object is actually invoked isolation, the coupling degree is reduced to some extent;

    2) a proxy object acts as an intermediary between the client and the target audience, so you can play the protection of the target object. A proxy object may be additional operations before the request for a target object, for example, permission checks and the like.

  Disadvantages:

    1) Because between the client and the real theme adds a proxy object, it will result in slower processing speed of the request;

    2) Implementing a proxy class also requires additional work, thus increasing the implementation complexity of the system.

Proxy mode and the adapter mode difference?

  After reading Adapter mode and proxy mode, you will have this question: almost looks like two modes? Both are defined a target object (abstract objects), dependent on the client complete abstract object corresponding function, so a explanation seems to be the same, then why large cattle will be divided into two modes it? Interface Adapter pattern is inconsistent because the old and new client leads to a problem can not be met, but the old interface can not be reconstructed completely out, because we want to use this interface to achieve a number of services. Then in order to implement the service before using the old interface, we should convert the new interface into the old interfaces; to achieve this transition class is the abstract meaning of the converter. Compared to the adapter scenario, the agent is not the same, although the agency also adds a layer is, however, provided by the agent interface and the interface is the same as the original, the role of the agent model is not to achieve exposed directly to the customer end, but this layer through a proxy, the proxy can do some processing.

Difference proxy mode and delegate?

  1)代理是模式提供一种"一个类对另外一个类的控制权"是类与类之间关系;委托提供了"一种方法的执行会同时执行加载在上面的方法"是方法与方法之间的关系。

  2)委托可以代替代理,但是代理不能代替委托。

  3)委托可以动态加载方法,代理不能实现。

  4)委托对象所加载的方法不一定要属于同一个类。但是代理的类必须属于同一个类。   

参考:https://blog.csdn.net/lovelion/article/details/8228042 

  https://www.cnblogs.com/zhili/p/ProxyPattern.html 

  https://www.cnblogs.com/chenwolong/p/Proxy.html

Guess you like

Origin www.cnblogs.com/az4215/p/11562848.html