concept

When you cannot or do not want to access this object directly, you can provide a proxy for other objects to control access to this object.

implementation

  • Subject — Abstract topic class. Is the common interface or abstract class of the delegate and proxy classes
  • RealSubject — Delegate class. The client indirectly calls the methods of the delegate class through the proxy class
  • Proxy — A Proxy class that holds a reference to the delegate class and executes it by calling the corresponding interface method of the delegate class in the interface method.

1 Static Proxy

public interface Subject {

    void submit();
}

public class RealSubject implements Subject {
    @Override
    public void submit(){
        System.out.println("Submitted"); } } public class MProxy implements Subject { private Subject msubject; public Proxy (Subject s){ subject = s; } @Override public void submit()[ s.submit(); } } public class Test{ public static void main(String[] args){ Subject s =  new RealSubject(); Subject p = new MProxy(s); p.submit(); } }Copy the code

2 Dynamic Proxy

Public class DyTest {/** * public interface Subject {void buy(); } /** * implements Subject {@override public void */ implements Subject {@override public void */buy() {
            System.out.println("Buy millet");
        }
    }

    /**
     * 苹果粉
     */
    public class IphoneFun implements Subject {
        @Override
        public void buy() {
            System.out.println("Buy apples"); Public class DynamicProxy implements InvocationHandler {// DynamicProxy implements InvocationHandler Invoke () Private Object proxyObject; Invoke () private Object proxyObject; public Object newProxyInstance(Object proxyObject) { this.proxyObject = proxyObject;returnProxy.newProxyInstance(proxyObject.getClass().getClassLoader(), proxyObject.getClass().getInterfaces(), this); // proxy.newProxyInstance () generates an instance of a dynamic Proxy class based on the specified class loader, a set of interfaces & call handlers, and returns // parameter 1: specifies the class loader that generated the Proxy object, which needs to be specified as the same class loader as the target object. Specify the target object's implementation interface // that is, what set of interfaces to provide to the target object. If it is provided with a set of interfaces, the proxy object implements that interface by default, so it can call the method // argument 3 in the set of interfaces: specify the InvocationHandler object. } // Invoke () overwrites the InvocationHandler interface // Before the dynamic proxy invokes any methods of the target object, // Parameter 1: dynamic proxy object (that is, which dynamic proxy object calls method ()) // Parameter 2: the method that the target object is called // Parameter 3: @override public Object invoke(Object proxy, Method Method, Object[] args) throws Throwable { System.out.println("Proxy name ="+ proxy.getClass().getName()); Object result = null; Result = method.invoke(proxyObject, args);return result;
        }
    }

    public void testDynamicProxy = new DynamicProxy(); // Create target object XiaoMiFun miFun = new XiaoMiFun(); / / create the proxy class object Subject phoneProxy = (Subject) dynamicProxy. NewProxyInstance (miFun); / / act as purchasing agency phoneProxy. Buy (); IphoneFun IphoneFun = new IphoneFun(); dynamicProxy.newProxyInstance(iphoneFun); phoneProxy.buy(); } public static void main(String[] args) { DyTest dyTest = new DyTest(); dyTest.test(); }}Copy the code

Dynamic proxy decoupled the agent and the delegate class from the static proxy, so that the two are not directly coupled. A single proxy class can be used to proxy N delegate classes