Agent mode & dynamic agent
proxy pattern
summary
For some reasons, it is necessary to provide a proxy for some objects to control the access to the object. The service object is not suitable to directly reference the target object. The proxy object acts as an intermediary between the access object and the target object.
advantage
- The proxy pattern acts as an intermediary between the client and the target object and protects the target.
- Proxy objects can extend the functionality of target objects.
- Proxy mode can separate the client from the target object, reduce the coupling of the system to a certain extent, and increase the degree of scalability.
shortcoming
- The agent pattern will increase the number of classes in the system design.
- Adding a proxy object between the client and the target object will slow down the request processing.
- Increase the complexity of the system.
How to correct this shortcoming:
Dynamic proxy after.
Implementation mode
Implementation (Interface)
Implementation steps:
- Create an interface - > both the target object and the proxy object should implement this topic. - --- > There is an object to proxy in the proxy object. - --- > It is implemented by calling the proxy object (passing in the target object).
//Suppose the above topic interface public interface ZuiQui { void dosth(); } //Target class (implement the above interface) public class Cha implements ZuiQui{ //The target method of the final call @Override public void dosth() { System.out.println("I want to pursue a beautiful woman"); } } //Proxy class (also implement the above interface) public class Hao implements ZuiQui{ //Write the classes that need proxies here Cha cha=new Cha(); @Override public void dosth() { System.out.println("I'm a middleman————————"); //Call the method of the target cha.dosth(); } } //Finally, the Main method call is used Main: ZuiQui zuiQui=new Hao(); zuiQui.dosth();
Inheritance (abstract class)
Similar to the above, the difference is that the above is the implementation interface, which is the inheritance of abstract classes
//This is an abstract class with an abstract method public abstract class Abstruect { abstract void fun(); } //This is a target class public class Mubiao extends Abstruect{ @Override void fun() { System.out.println("I'm the target——————--"); } } //This is a proxy class public class DaiLi extends Abstruect{ Mubiao mubiao=new Mubiao(); @Override void fun() { System.out.println("I'm an agent"); mubiao.fun(); } } //Called through the Main method public class Main { public static void main(String[] args) { DaiLi daiLi=new DaiLi(); daiLi.fun(); } }
Feature application scenarios
characteristic
When you cannot or do not want to directly reference an object or have difficulty accessing an object, you can access it indirectly through a proxy object. Objective: to protect and enhance the target object.
Application scenario
- Remote proxy is usually used to hide the fact that the target object has different address spaces and facilitate client access.
- Virtual agent is usually used when it is expensive to establish the target object.
- Security agent, which is usually used to control the access rights of different kinds of client objects.
- Delay loading, in order to improve the performance of the system, delay the loading of the target.
Dynamic agent
Create proxy classes at run time through the reflection mechanism.
-
The handler should preferably be an interface to facilitate future expansion.
In extension, the implemented interface is judged according to the interface and no interface.
-
If there is an interface, implement the InvocationHandle interface.
Implement the MethodInterceptor interface without an interface.
-
Override invoke (object proxy, method, method, object [] arges) to implement the InvocationHandle interface;
Override intercept (object o, method, method, object [] objects, methodproxy, methodproxy) to implement the MethodInterceptor interface.
-
When calling, return the Proxy instance of an interface through the static method newProxyInstance of the Proxy class. For different Proxy classes, pass in the corresponding handler () in the Proxy controller;
-
The method is called through the object of the created Proxy class static method newProxyInstance.
.
-
When calling, return the Proxy instance of an interface through the static method newProxyInstance of the Proxy class. For different Proxy classes, pass in the corresponding handler () in the Proxy controller;
-
The method is called through the object of the created Proxy class static method newProxyInstance.