Design Mode Agent mode (Proxy)

Proxy mode to a certain object provides a proxy object, the proxy object controlled by a reference to the original object, proxy mode is popular in terms of our life in common intermediary. This type of design pattern belongs structural model

 

 

classification

Static Agent: Create or specific tool automatically generates source code by the programmer, in its compilation. Before programmers run, the proxy class .class file has been created.

Dynamic Agent: are created dynamically at runtime through reflection.

Cglib Agent: static and dynamic agent Proxy models are required to achieve the target object is a target object interface, but sometimes the target object is just a single object, and does not implement any interfaces, this time we can use the target object is a subclass agent class implements manner .Cglib agent, also known as sub-class of agents, it is to build a subclass object in memory to achieve function expansion of the target object.


Feature

Intermediary isolation : In some cases, a client class does not want or can not directly reference a delegate object, and the proxy class object can play the role of intermediary between the client and the delegate object class, which is a proxy class and delegate class implements the same interface.

Opening and closing principle, increased functionality : In addition to being an intermediary proxy class customer class and delegate class, we can also extend the functionality of the delegate by proxy class to add additional functionality, we only need to do this without the need to modify the proxy class then modify the delegate class, in line with the principle of opening and closing the code design. Acting principal responsible for pretreatment of the delegate messages, filter messages, forward messages to the delegate, and the subsequent handling of returned results and so on. Acting class itself does not really achieve service, but the same correlation method by calling the delegate class to provide specific services. The real business function is implemented by the delegate class, but you can add some public service before and after the execution of business functions. For example, we want to join the project cache, log these functions, we can use a proxy class to complete, and no need to open the delegate class has been packaged.

 

Scenarios

  • When we want to hide a class, you can provide proxy class.
  • When a class needs to provide different calling privileges for different callers, you can use a proxy class to implement (not necessarily only a proxy class, we can create multiple proxy class to implement, permissions can also be a proxy class gold authority to judge different function calls).
  • When we want to extend a function of a class, you can use proxy mode, a simple extension (only can be carried out with a simple extension until after the reference for the delegate class statement) in the proxy class.

 

Guess you like

Origin www.cnblogs.com/lucky1024/p/11549786.html