What is agency?
Agent, you can directly understand as we know intermediary, middleman, intermediate platform and so on. The purpose is to serve us and get there.
As you can see above, the proxy can implement our method enhancements (such as discounts on shopping), such as some common logging, caching, etc. It is also possible to implement method interception (such as true or fake substitution), and the proxy method can modify the parameters and return values of the wish method.
Static agent
Static proxy is the most basic proxy. Here is an example:
- For example, when we need to buy something, we need to provide a shopping function as follows:
public interface Shopping {
Object[] goShopping(int money);
}
Copy the code
- Next comes our implementation, where we go shopping ourselves, as follows:
public class ShoppingImpl implements Shopping { @Override public Object[] doShopping(int money) { return new Object[] { "Shoes "," clothes "}; }}Copy the code
- If we don’t want to buy by ourselves, then we need the appearance of an agent:
public class ProxyShopping implements Shopping { Shopping base; ProxyShopping(Shopping base) { this.base = base; } @override public Object[] doShopping(int money) {} @override public Object[] doShopping(int money) { System.out.println(string. format(" %s ", moneyCount)); Object[] things = base.doshopping (moneyCount); // Change the return value, replace shoes with socks if (things! = null && things. Length > 1) {things[0] = "socks "; } return things; }Copy the code
As you can see from the above, the proxy can indeed modify parameters and return values, but it can also be enhanced for use.
- Disadvantages of static proxies: The traditional static proxy model requires writing a proxy class for each class that needs a proxy, which can waste a lot of time.
A dynamic proxy
What about dynamic proxies? As follows:
public static void main(String[] args) { Shopping women = new ShoppingImpl(); Println (array.toString (women. DoShopping (100)) Proxy.newProxyInstance(Shopping.class.getClassLoader(), women.getClass().getInterfaces(), new ShoppingHandler(womenSystem.out.println(Arrays.toString(women.doShopping(100))); }Copy the code
The main classes we deal with are ShoppingHandler
Public class ShoppingHandler implements InvocationHandler {** ** implements InvocationHandler; public ShoppingHandler(Object base) { this.base = base; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {if ("doShopping".equals(method.getName())) {// Change the input parameter int money = args[0]; int moneyCount = (long) (money -500); Object[] things = (Object[]) method.invoke(base, moneyCount); // Change the return value, replace shoes with socks if (things! = null && things. Length > 1) {things[0] = "socks "; } return things; } if ("doSomething".equals(method.getName())) {return null; } if ("doSomethingElse".equals(method.getname ())) {// doSomethingElse return null; } return null; }}Copy the code
Imagine if we had many classes doing the same thing, wouldn’t it be more convenient to use dynamic proxies?
Advantages of dynamic proxies
-
You only need one dynamic proxy class to solve the problem of creating multiple static proxies, avoiding duplication and unnecessary code
-
More flexibility
Disadvantages of dynamic proxies
-
It is inefficient because a dynamic proxy calls a target object method indirectly through Java reflection, rather than directly in a static proxy
-
Application scenarios are limited because of Java’s single inheritance feature (each Proxy class inherits the Proxy class), that is, a Proxy class can only be created for an interface, but not for a class.
In Java dynamic Proxy mechanism, there are two important classes or interfaces, one is the InvocationHandler interface, the other is the Proxy class, this class and interface is necessary to implement our dynamic Proxy.
The InvocationHandler interface is implemented for dynamic Proxy classes to handle operations on proxied objects, while proxies are used to create dynamic Proxy class instance objects, because only with this object can we invoke methods that require the Proxy.