Java Dynamic proxy Mode
Proxy: a role represents another role to implement some specific functions.
For example, the relationship between producers, middlemen, and customers
Customers who buy products do not directly deal with producers, nor do they know how the products are produced. Customers only deal with middlemen, and middlemen can carry some packaging on the products, provide some after-sales services.
Proxy mode has three roles: 1. Abstract topic role 2. Proxy topic role 3. Real-time proxy role
In this case, we come to the reality of a static proxy.
I use a tank as an example.
Abstract topic role: Moveable
Copy codeThe Code is as follows: package com. gjy. proxy;
Public interface Moveable {
Void move ();
}
Proxy topic role: TanktimeProxyCopy codeThe Code is as follows: package com. gjy. proxy;
Public class TanktimeProxy implements Moveable {
Private Moveable t;
Public TanktimeProxy (Moveable t ){
Super ();
This. t = t;
}
@ Override
Public void move (){
Long time1 = System. currentTimeMillis ();
System. out. println ("time1 =" + time1 );
T. move ();
Long time2 = System. currentTimeMillis ();
System. out. println ("time2 =" + time2 );
System. out. println ("RunTime:" + (time2-time1 ));
}
}
Real-time proxy object: TankCopy codeThe Code is as follows: package com. gjy. proxy;
Public class Tank implements Moveable {
@ Override
Public void move (){
System. out. println ("TanK moving ........");
}
}
Test:Copy codeThe Code is as follows: package com. gjy. proxy;
Public class TestTank {
Public static void main (String [] args ){
Tank t = new Tank ();
Moveable move = new TanktimeProxy (t );
Move. move ();
}
}
I want to add a diary before and after the TanK move () method:
I have to write another class to realize this function:
Copy codeThe Code is as follows: package com. gjy. proxy;
Public class TanklogProxy implements Moveable {
Private Moveable t;
Public TanklogProxy (Moveable t ){
Super ();
This. t = t;
}
@ Override
Public void move (){
System. out. println ("start move ........");
T. move ();
System. out. println ("end move ......");
}
}
Test:Copy codeThe Code is as follows: package com. gjy. proxy;
Public class TestTank {
Public static void main (String [] args ){
Tank t = new Tank ();
Moveable move = new TanktimeProxy (t );
Moveable movet = new TanklogProxy (move );
Movet. move ();
}
}
In this way, I use the agent to add logs and statistical functions before and after the move () method of Tank. Because of TanktimeProxy, TanklogProxy implements the Moveable interface, so TanklogProxy can proxy TanktimeProxy, or vice versa. They can exchange the agent order of Tank.
If I want to add more capabilities to the front and back of the Tank move () method, is it necessary to write more proxy topic roles, this sub-sample will make the code generation too fat and difficult to maintain. Is there any way to deal with it? The case can be answered. We can dynamically generate a proxy topic role, to proxy all the objects to be proxy, which is dynamic proxy.
The article ends with sharing with you some comments from programmers: IBM and Boeing 777.
Boeing 777 is the first aircraft ever designed and manufactured completely in computer virtual reality. The equipment used is completely provided by IBM. Before the test flight, the president of Boeing warmly invited IBM's technical supervisor to participate in the test flight, but the supervisor said, "Ah, I am very honored. Unfortunately, it was my wife's birthday, so .. "..
As soon as Boeing heard it, he got angry: "I haven't told you the test flight date yet !"