There’s a lot of talk about the proxy model,
Often used in development is the static proxy pattern, which is well decoupled.
Dynamic proxy is another implementation of the proxy pattern.
What is the difference between dynamic proxies?
What are the benefits of dynamic proxies?
Today we are going to look at these issues.
Review static proxy
We looked at static proxies once before,
Write code elegantly in proxy mode
A typical Proxy class in Proxy mode would look like this,
For the caller, the constructed instance needs to be passed to the proxy, which can then be used instead of manipulating the real instance.
The problem with static proxies is,
That’s fine when there’s less interface code, but when there are more interfaces,
The Proxy class needs to respond to the addition of interfaces, such as the Func interface above,
You might start with just a read() method, but then you have write(), you have mark(),
As the number of interfaces increases, the workload of Proxy maintenance gradually increases.
So how can dynamic proxies solve this problem?
Implementation of dynamic proxy
The implementation steps of dynamic proxy are as follows:
· Define a public interface (like Func) and an implementation class (like User), which is the same as static proxies
· Define a DynamicProxy class to implement the InvocationHandler interface. This Proxy is similar to the static Proxy but does not implement the Func interface
· Call the Proxy class to construct the Proxy object
Two things that are important in implementing dynamic proxies are the Proxy class and the InvocationHandler interface, both of which are located under the Reflect package,
Shall we look at the DynamicProxy defined in step 2
It also holds a delegate object instance, but unlike a static proxy,
It does not implement the interface methods of the delegate object,
Instead, the invoke method of InvocationHandler is implemented and method.invoke(this.user, objects) is called.
Take a look at the Invoke method and continue our analysis later,
I’m going to post the code for the Client class, which is where we use the Proxy,
The difference with static proxies is that, although you need to instantiate a delegate class object and pass it to the Proxy constructor,
But here you instantiate the InvocationHandler object, not the DynamicProxy object.
This completes the code for a dynamic proxy, with the following output
before invoke user method
user read
after invoke user method
Comparison & Analysis
· Let’s first talk about the implementation of DynamicProxy in the second step.
Unlike static proxies, which implement the read() method of the Func interface, dynamic proxies implement the Invoke method of InvocationHandler,
Static proxies need to call different methods of the User interface in different interfaces,
The dynamic proxy does not need to care about which specific method of User needs to be called when invoke is invoked.
Methods are encapsulated in method objects, and the required parameters are in Object[] objects,
Just call it directly
This means that even if the Func interface is added later, there is no additional maintenance for DynamicProxy.
· What does proxy. newProxyInstance do
In a StaticProxy, we’ll use the static object created by new StaticProxy(user) directly,
In the dynamic Proxy, we operate the dynamic Proxy object proxyUser constructed by proxy. newProxyInstance.
So if you’re new to dynamic proxies you might get confused here,
“Isn’t the proxy class an object from New DynamicProxy?”
No, if I print out the proxyUser class name,
It will be in the form of $ProxyN, N starting from 0, and this will be the actual proxy object generated by the dynamic proxy,
This is what dynamic ProxyN is all about. The object $ProxyN is created at run time,
If explained in code, the code for $ProxyN would look like this
This is the real proxy class.
Interested in this article, welcome to pay attention to the author, if you have different opinions, welcome to leave a message.