The so-called proxy refers to a class that has the same interface as the proxy element (the proxy object). The client must interact with the proxy target class through the proxy, and the proxy generally performs certain actions during the interaction process (before and after the interaction). some special treatment. Proxy mode is a very common mode that can be seen everywhere in our lives. For example, we don't have to go to the train station to buy train tickets, but we can buy them at some train ticket sales points. It is not necessary to send a letter by yourself, you can entrust the letter to the post office, and the post office will deliver the letter to the destination.
Taking buying train tickets through a sales agency as an example, the code is implemented as follows:
//Provide public interface for buying tickets interface Passenger { public void buyTicket(); }
// passenger entity public class RealPassenger implements Passenger { @Override public void buyTicket() { // TODO Auto-generated method stub System.out.print("Bought a train ticket"); } }
//point of sale public class Proxy implements Passenger { Passenger passenger; public Proxy(Passenger p) { this.passenger = p; } @Override public void buyTicket() { // TODO Auto-generated method stub System.out.println("Through the point of sale"); passenger.buyTicket(); } }
// test class public class Client { public static void main(String[] args) { Passenger passenger = new RealPassenger(); Passenger proxy = new Proxy(passenger); proxy.buyTicket(); } }
Output result: purchased
train tickets through the sales agency The above can also be called static proxy, in order to distinguish another implementation of proxy mode in Java - dynamic proxy. The application of dynamic proxy is very extensive, and the dynamic proxy mechanism is used in the interceptor of struts2. Let's take buying a ticket as an example. Now we use dynamic proxy to implement it. If you don't understand the interface method, you will understand it by checking the Java api. I won't explain it here. The code is as follows (note the PassengerProxy class):
import java.lang.reflect.*; public interface Passenger { public void buyTicket(); }
public class RealPassenger implements Passenger { @Override public void buyTicket() { // TODO Auto-generated method stub System.out.println("Buy a ticket"); } }
// Implemented with dynamic proxy public class PassengerProxy implements InvocationHandler { public Object obj; // Pass obj to the proxy class public Object obj(Object obj) { this.obj = obj; return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj .getClass().getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // TODO Auto-generated method stub System.out.println("Through the proxy"); method.invoke(obj, args); return null; } }
public class Client { public static void main(String[] args) { PassengerProxy proxy = new PassengerProxy(); Passenger passenger = (Passenger) proxy.obj(new RealPassenger()); passenger.buyTicket(); } }