This is the second day of my participation in Gwen Challenge
Use of proxy mode
A layer of proxy classes is added between the delegate class and the client to proxy the implementation of the delegate class. Simply put, it is the business agent between manufacturers and consumers. Consumers from the business agent to buy products, consumers do not care about the situation of the manufacturer, and the business agent from the manufacturer to obtain products, manufacturers do not care about the sale of products. The advantages of proxy classes are:
- Hide the implementation of the delegate class.
- Delegate classes are decoupled from the client. There are some additional operations that can be done through the proxy class without modifying the delegate class.
Static agent
Static proxies work by manually implementing a proxy class such as the following ProxyHttp class, which holds a DelegateHttp delegate class internally and implements the delegate class indirectly by calling the methods of the proxy class.
interface HttpService {
void http(String msg,int target);
}
/** * delegate class that implements the HttpService interface */
static class DelegateHttp implements HttpService{
@Override
public void http(String msg, int target) {
System.out.println("delegate msg="+msg+" target="+target); }}/** * static proxy class */
private static class ProxyHttp {
private DelegateHttp delegateHttp;
public ProxyHttp(DelegateHttp o) {
this.delegateHttp = o;
}
public void http(String msg, int target) { delegateHttp.http(msg, target); }}Copy the code
The disadvantage of static proxies is that the proxy classes are implemented at compile time and may need to be modified or added if more than one delegate class exists or additional operations need to be performed on the delegate class. If the revision of the place is too much, too much change, some of the gains outweigh the losses.
A dynamic proxy
Dynamic proxies generate corresponding proxy classes at run time through reflection. The Dynamic proxy uses the InvocationHandler interface, which implements this interface and the corresponding Invoke method to invoke the implementation of the delegate class.
public static void main(String[] args) {
DelegateHttp delegateHttp = new DelegateHttp();
getProxy(delegateHttp).http("this is proxy".10000);
}
/** * Proxy class */
private static HttpService getProxy(HttpService delegateHttp) {
HttpInvocationHandler invocationHandler = new HttpInvocationHandler(delegateHttp);
return (HttpService) Proxy.newProxyInstance(HttpService.class.getClassLoader(),
new Class[]{HttpService.class}, invocationHandler);
}
/** * intermediate class */
static class HttpInvocationHandler implements InvocationHandler {
private Object delegate;
public HttpInvocationHandler(Object delegate) {
this.delegate = delegate;
}
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
System.out.println("dynamic proxy start------");
Object result = method.invoke(delegate, objects);
System.out.println("dynamic proxy end------");
returnresult; }}Copy the code
InvocationHandler is equivalent to the mediation class, which calls the mediation class and holds the delegate object reference. When a delegate class method is called, the Invoke method of InvocationHandler is executed, and we can do some additional operations in the Invoke method, such as adding logs, etc.
On the blackboard
The proxy mode is to delegate the implementation of the delegate class to achieve the extension and decoupling of the implementation. Note that dynamic proxy is for interfaces only, and will cause an error if it is a normal class. Finally, don’t think of it as an implementation of an interface, which is what I used to think.
The last
The principle of dynamic Proxy is to dynamically generate a $Proxy0 Proxy class, which inherits from Proxy and implements the interface requiring Proxy. Add the following line to view and generate the corresponding proxy class:
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles"."true");
Copy the code