java code that implements design patterns of proxy mode

/ * Agent model
*
* Life:
* (1) is not convenient to do their own things, for example: to go abroad to find purchasing renters to buy a house, and so on ...
* 1, proxy mode
* (1) Theme Interface: Requires proxy class with the agent class implements the same interface, for example: the DAO Interface
* (2) the broker
* (3) broker
* must have a reference in the broker
*
* 2, static proxy mode
* drawbacks: a proxy class for only a Acting themes (interfaces) agency work
*
* 3, dynamic proxy mode
* advantages: a processor agency work, can work for multiple agents topics agency, agency work only content as can be.
* Requires:
* (1) the preparation of an agent working class processor, this class must implement an interface InvocationHandler
* (2) with the JDK provides a Proxy class to create an object proxy class
* (3) call the method

Example: Static Proxy

interface A{
  void run();
}

class B implements A{
  @Override
  public void run() {
    for (int i = 0; i < 1000; i++) {
      System.out.println(i);
    }
  }
}

class StaticProxy implements A{
  private A a;
  public StaticProxy(A a) {
    this.a = a;
  }

  @Override
  public void RUN () {
    System.out.println ( "RUN method performs");
    Long Start = System.currentTimeMillis ();
    a.run ();
    Long End = System.currentTimeMillis ();
    System.out.println ( "execution time:" + (end - Start));
    System.out.println ( "RUN method has finished executing");
  }
}

Static agent use:. New StaticProxy (new B ()) run ()

 

Example: Dynamic Proxy

interface C {
  void run();
}

C {CImpl the implements class
  @Override
  public void RUN () {
    System.out.println ( "RUN interface implementation Method C");
  }
}

class DynamicProxy implements InvocationHandler {

  // the proxy object
  Private Object obj;
  public DynamicProxy (Object obj) {
    this.obj = obj;
  }

  

  / *
  * A parameter: the object proxy class
  * Parameters II: The method to be executed by broker
  * three parameters: the method is broker to execute the required argument list
  * This method is not a program called, is a the method is called automatically when an object will be executed proxy class
  * /

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println(method.getName() + "方法执行");
    long start = System.currentTimeMillis();
    Object object = method.invoke(obj, args);
    long end = System.currentTimeMillis();
    System.out.println("执行时间:" + (end - start));
    System.out.println(method.getName() + "方法执行结束");
    return object ;
  }
}

Dynamic agent uses:

public class TestProxy2 {
  public static void main(String[] args) {
    Class clazz = CImpl.class;
    ClassLoader loader = clazz.getClassLoader();
    Class[] interfaces = clazz.getInterfaces();
    C proxy = (C) Proxy.newProxyInstance(loader, interfaces, new DynamicProxy(clazz));
    proxy.run();

  }

}

Guess you like

Origin www.cnblogs.com/dirsoen/p/12642747.html