A netizen pointed out some of my biased articles a few days ago. These articles are my summary when I was reading Java core. By the way, I added my personal understanding. Because of the e-version, it may be difficult to understand some aspects. Next, let's talk about proxy in Java.
Agent, presumably everyone should know what it is in winter. Generally, mobile phone manufacturers have agents and fans all have their own agents. This can be seen as a proxy mode. Next, I chose the following scenario to describe the situation: a chairman of the board was on a business trip, but at this time the company had a party and the CHAIRMAN paid for the bill, but his secretary settled the bill. We can regard this as a proxy, ^_^.
First, we define an interface: merchant, as shown below:
Package cn.edu. HUST. cm. test;
Public interface merchant {
Void treat (); // guests are welcome to dinner, ^ _ ^
}
Then we define a class: Director, as shown below:
Package cn.edu. HUST. cm. test;
Public class director implements merchant {
Public Director (){
}
Public void treat (){
System. Out. println ("the Chairman invited everyone to dinner ");
}
}
Okay. Now we are looking for an agent for him. This agent is his secretary ). Some proxy mechanisms are involved here. In Java, the invocationhandler must be implemented for the classes used for proxy, and its invoke method should be implemented. As for this, I don't know why, ^_^. In addition, when we generate a proxy instance, as long as we call any method through this instance, it will lead to the call of the invoke method. Let's take a look at the example, as shown below:
Package cn.edu. HUST. cm. test;
Import java. Lang. Reflect. invocationhandler;
Import java. Lang. Reflect. method;
Public class secretary implements invocationhandler {
Public Secretary (Director ctor ){
This. Director = Director;
}
Public object invoke (Object arg0, method arg1, object [] arg2)
Throws throwable {
Director. Treat ();
System. Out. println ("checked by the secretary! ");
Return NULL;
}
Private Director ctor;
}
Now we start to build our subject test code, as shown in the following code:
Package cn.edu. HUST. cm. test;
Import java. Lang. Reflect. invocationhandler;
Import java. Lang. Reflect. proxy;
Public class proxytest extends proxy {
Public proxytest (invocationhandler arg0 ){
Super (arg0 );
}
Public static void main (string [] ARGs ){
Director dire= new director ();
Invocationhandler Secretary = new secretary (Director );
Merchant merchant = (merchant) proxy. newproxyinstance (director. getclass (). getclassloader (), director. getclass (). getinterfaces (), Secretary );
Merchant. Treat ();
}
}
Run the program and the output will be: Chairman of the Board of Directors
Check out by the secretary!
Here are a few things to explain:
1. The role of the proxy. newproxyinstance method. It creates a proxy object for an object. In this example, a proxy object is created for ctor. Which class does this object belong? We add such a system in the program. out. println (merchant. getclass (). getname (). The output is $ proxy0. It indicates that a proxy class has been generated. This class implements all interfaces in the class to which ctor belongs.
2. Proxy Implementation Mechanism issues. In my opinion, this seems to be an event processing mechanism. When a modern object calls a method, it will trigger the corresponding invoke method. Therefore, the third parameter of the proxy. newproxyinstance method is equivalent to registering a listener for this proxy object.
There are several methods for the proxy class. You can view the corresponding API documentation for usage.