Proxy mode: proxy proxy object real object, the purpose of enhancing the function of a real object
Static agent: there is a class file description proxy mode
Dynamic Agent: Agent class is formed in memory
Proxy object and real object implement the same interface
Proxy object = Proxy.newProxyInstance ();
Use a proxy object to call methods
Enhancement
Enhanced parameter list
Enhance the return value type
Enhancement logic executor
References: sell mobile phones
1. Create a real object
2. Dynamic Agent enhancements Moblie objects
Three parameters:
Class loader real object .getClass (). GetClassLoader ()
An array of interfaces real object .getClass (). GetInterfaces ()
Processor new InvocationHandler ()
SaleComputer proxy_Moblie = (SaleComputer) Proxy.newProxyInstance(moblie.getClass().getClassLoader(), moblie.getClass().getInterfaces(), new InvocationHandler() {
The method of preparation of the proxy logic: the proxy object to call all methods will trigger the execution of the method
Three parameters:
proxy proxy object
method call proxy object method, an object is encapsulated after the
actual proxy object args parameter to the method call, passing
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Reinforcement parameter args [0]
Use real object to call the method
Object obj = method.invoke(moblie, args);
2 enhanced return value
return xxx;
}
Easy to remember. . . .