First, what is a dynamic agent?
First we think about what is an agent? For example, in the real life of the boss on a business trip, you can not do something in the company, please his secretary "agent". In the program "agent" means that the original is a dry thing, but for some reason is not convenient to carry out, but through Class B to carry out their own duties, Class B is not a complete class A, it just called a Class A execution method, as the secretary agent is actually called the boss of a certain power.
Code implementation: in the proxy class's constructor to pass in the real object, then the real object can call its own method in the proxy class, thereby implementing the proxy, this is the idea of the agent. We look at the action from the outside like the proxy class implementation, in fact, the real object is implemented.
Agents are divided into static agents and dynamic agents. Static proxy is created by the programmer to create the source code to compile, before running it has fixed the type of real objects can be proxied, so when there are many different types of real objects to be proxied, the programmer will be different types of real objects to create different proxy classes, which is obviously very troublesome;
Dynamic agent is the reflection mechanism of Java, which dynamically obtains the type information when the program is running, and realizes the dynamic proxy.
Second, the implementation of dynamic agents:
Java Dynamic Agent