(Four), proxy mode

First, what is the proxy mode? And there is the role?

In proxy mode, we create an object with an existing object to provide a functional interface to the outside world. Then by controlling access to the method is a good proxy class to hide foreign agent specific information on this, this is the class. Providing a proxy to control access to the object to other objects. To put it plainly: Access is through a proxy class to achieve direct access to a number of extensions were made before and after the operation proxy class.

A distinction, and the adapter mode: the main adapter mode changes contemplated object's interface, but can not change the interface mode proxy proxy class.

The difference between 2 and decorator patterns: Decorator for enhancements, and proxy mode is to be controlled.

A static proxy implementation

1, ISource interface proxy class with the proxy class together to achieve.

public interface ISource {
  public void method();
}

2, the proxy class

ISource {class Sourceable the implements public
  @Override
  public void Method () {
    System.out.println ( "I was proxy class!");
  }
}

3, the proxy class: hold the object instance proxy class.

public class Proxy implements ISource {
private Sourceable source;
public Proxy() {
  this.source = new Sourceable();
}
@Override
public void method() {
  before();
  source.method();
  after();
}
private void before(){
  System.out.println("aa");
}
private void after(){
  System.out.println("bb");
}
}

4, the test class

{class ProxyTest public
public static void main (String [] args) {
  // via an interface, the proxy class is instantiated,
  ISource the Proxy Source new new = ();
  // call the method of the proxy class
  source.method ();
  }
}

Two, JDK dynamic proxies

1,1,2 steps above;

2, Acting Class

public class SourceProxy implements InvocationHandler{
  //目标对象
  private Object target;
  public SourceProxy(Object target){
    this.target = target;
  }
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("aa");
    Object rs = method.invoke(target,args);
    System.out.println("bb");
    return rs;
  }
}

3, the test class

public static void main(String[] args)  {
  ISource source = new Source();
  SourceProxy myInvocationHandler = new SourceProxy(source);
  //代理对象
  ISource proxySource = (ISource) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{ISource.class}, myInvocationHandler);
  proxySource.method();
}

Three, Cglib dynamic proxies (packet requires cglib-2.2.3.jar; cglib-nodep-2.2.2.jar; asm.jar)

1, the proxy class Source

public  class  Source {

    // can be proxied

    public void eat() {

        . System OUT .println ( "I was the proxy class aa");

    }

    // final method is never generated word class overrides

    public final void work() {

        . System OUT .println ( "I was the proxy class bb");

    }

    // private method will not be generated in the word class overrides

    private void play() {

        . System OUT .println ( "I was the proxy class cc");

    }

}

2, Acting Class

public class CglibProxy implements MethodInterceptor {

    private Object target;

    public CglibProxy(Object target) {

        this.target = target;

    }

    @Override

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println("aa");

        Object result = method.invoke(target, objects);

        System.out.println("bb");

        return result;

    }

    public static Object getProxy(Object target) {

        Enhancer enhancer = new Enhancer();

        // set requires a proxy object

        enhancer.setSuperclass(target.getClass());

        // set Attorney

        enhancer.setCallback(new CglibProxy(target));

        return enhancer.create();

    }

}

3, the test class

public class Test {

 

    public static void main(String[] args) {

        // generate Cglib  proxy class

        Source engineerProxy = (Source) CglibProxy.getProxy(new Source());

        // call the relevant methods

        engineerProxy.eat();

    }

}

Guess you like

Origin www.cnblogs.com/dwxblogs/p/10935284.html