1, What is the proxy model
Agent mode is a kind of design mode. If you have to give an example, it is like landlord (agent), intermediary (agent). There are three relationships in agent mode: agent, agent and enhancement method (additional code executed by the agent). Agent mode is the method of executing the agent within the agent, In addition, some other additional methods are added to the agent for execution. These extra executed code is called as an enhancement to the agent.
2, What's the use of proxy mode
Sometimes in development, when using the methods created by others, the methods can not meet the requirements, and these methods can not be changed. At this time, using the proxy mode can perfectly solve such problems. Using the proxy mode, new functions can be added on the premise of still using the original method. Agent mode makes the program more flexible and changeable.
3, How to use proxy mode
Agent models are divided into two basic categories: static agent and dynamic agent.
Static proxy
Static agent refers to the agent. The classes in which the agent plays these roles are fixed. They all have a fixed relationship. Two classes implement the agent and the agent. If we need such a set of agent relationship, we need to create the classes of these two relationships repeatedly. The classes represented by the agent and the classes that the agent can represent are fixed and inflexible.
Example:
Agent
public class Landlord { /*The price at which the landlord rents the house*/ int price = 1000; /*The landlord needs to rent the house*/ public String rent(){ String info = "xxx Rental price:"+price; return info; } }
Agent
public class Agent { /*The landlord has a house, but the intermediary doesn't, so the intermediary needs the landlord*/ private final Landlord landlord = new Landlord(); /*The intermediary rents the house for the landlord. The intermediary does not have a house. The intermediary uses the landlord's house for external rent*/ public void oversell(){ /*The intermediary increased the rent of the landlord as his reward. This part can be regarded as enhancement*/ landlord.price+=200; /*The intermediary must use the landlord's house to rent out, because the intermediary is only an agent and the intermediary has no house.*/ String s = landlord.rent(); System.out.println(s); } }
test
public class test { public static void main(String[] args) { /*In this implementation process, we only face the agent and do not need to care about who the agent is, but because there is no other room The East intermediary can only act as an agent for one landlord, while the agent for other landlords needs to change the code or re implement the two classes of such a relationship*/ new Agent().oversell(); } }
Dynamic agent
Dynamic agent can be realized: only one agent or agent logic can be used to agent various agents, and different enhancements can be made to decouple the agent and the agent, and only the agent remains unchanged. The problem of coupling and reuse of static agents is solved.
JDK dynamic agent
summary
java implements the proxy mode, which is called JDK dynamic proxy.
Principle of JDK dynamic agent:
(to be supplemented in the future)
It should be noted that the JDK dynamic proxy must implement the interface for the proxy object, that is, only the implementation class of the interface can be proxy, and the class without interface cannot be proxy.
Classes used by JDK dynamic proxy
location | summary | type |
---|---|---|
java.lang.reflect.InvocationHandler | Enhanced content | Interface |
java.lang.reflect.Method | The method that needs to be represented in the agent | class |
java.lang.reflect. Proxy | Agent | class |
JDK dynamic proxy example
The steps of using JDK dynamic agent are fixed.
Create proxies and interfaces
/*Interface*/ public interface MyInterface { public String rent(); } /*Agent*/ public class Landlord implements MyInterface { /*The price at which the landlord rents the house*/ int price = 1000; /*The landlord needs to rent the house*/ public String rent(){ String info = "xxx Rental price:"+price; return info; } }
Create an agent. Implement the abstract method in the InvocationHandler interface, declare the enhanced code in this method, and create a method to obtain the proxy class.
Example:
package Dynamic agent; import com.sun.org.apache.bcel.internal.generic.ObjectType; import sun.rmi.transport.ObjectTable; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class MyInvocationHandler implements InvocationHandler { /*Create an attribute to store the agent and use it in the formal parameter of invoke() and createProxy().*/ private Object object = null; /*The surrogate object is passed in when the object is initialized*/ public MyInvocationHandler(Object o){ this.object = o; } /** * * @param proxy java Created proxy object instance (not required) * @param method The method that the agent needs to be represented (not required) * @param args The surrogate needs the parameters required by the surrogate's method * @return Return the final result */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { /*Claim enhancement*/ if (object != null){ Landlord landlord = (Landlord)object; landlord.price+=100; } /*Execute the method of the agent and return the result after the method is executed*/ Object obj = method.invoke(object,args); return obj; } /*Create a proxy object.*/ public Object createProxy(){ /* * newProxyInstance Three parameters of: * The class loader of the agent, * Interface of the agent. * MyInvocationHandler * */ return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(),this); } }
Test:
public class test { private static java.lang.Object Object; public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException { MyInterface myInterface = (MyInterface)new MyInvocationHandler(new Landlord()).createProxy(); System.out.println(myInterface.rent()); } }