JAVA-based design pattern of the proxy mode

  • concept

    Baoqiang a broker called Song Zhe, the broker is suspended, the agent can do a series of things Baoqiang, activities with fans, microblogging maintenance Baoqiang, Baoqiang travel arrangements as well as what and so on. If Baoqiang a person to do it not exhausted. Through this saves a lot of agents to Baoqiang.

    Agent model is not changing the code structure of the original class to class to develop new features.

  • Class diagram: Subject to an abstract target interface, client interface dream, realSubject original class, proxy for the proxy object

  

  • Static agents
// Public primary classes and interfaces of the proxy class 
public  interface the Subject {
     public  void Request ();
}

public class RealSubject implements Subject{

    public void request() {
        System.out.println ( "Wang Baoqiang movie" );
    }
}

public class Proxy implements Subject {
    private Subject realSubject;

    public void setRealSubject(Subject realSubject) {
        this.realSubject = realSubject;
    }

    public void request() {
        System.out.println ( "Song Zhe Wang Baoqiang maintenance instead of micro-blog" );
        realSubject.request();
        System.out.println ( "Song Zhe Wang Baoqiang instead of interacting with fans" );
    }
}

public class Main {
    public static void main(String[] args) {
        Subject realSubject=new RealSubject();
        Proxy proxy=new Proxy();
        proxy.setRealSubject(realSubject);
        proxy.request();
    }
}
  • Dynamic Proxy
public interface Subject {
    public void request();
}

public class RealSubject implements Subject {
    public void request() {
        System.out.println ( "Wang Baoqiang movie" );
    }
}

public class ProxyMain {
    public static void main(String[] args) {
        final RealSubject realSubject=new RealSubject();
        Subject proxy=(Subject) Proxy.newProxyInstance(
                /**
                 * ClassLoader agent class is loaded
                 * Interfaces with the proxy objects proxy object public interface
                 * InvocationHandle event processing
                 */
                realSubject.getClass().getClassLoader(),
                realSubject.getClass().getInterfaces(),
                new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args)
                            throws Throwable {
                        System.out.println ( "Song Zhe Wang Baoqiang maintenance instead of micro-blog" );
                        Object returnValue=method.invoke(realSubject,args);
                        System.out.println ( "Song Zhe and Wang Baoqiang fans instead of interaction" );
                         return returnValue;
                    }
                }
        );
        proxy.request();
    }
}
  • Cglib agent
public class RealSubject {
    public void request() {
        System.out.println ( "Wang Baoqiang movie" );
    }
}

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class ProxyFactory implements MethodInterceptor {
    private Object realSubject;
    public ProxyFactory(Object realSubject){
        this.realSubject=realSubject;
    }
    public Object getProxyInstance () {
         // 1. tools 
        Enhancer Enhancer = new new Enhancer ();
         // 2. Set the parent 
        enhancer.setSuperclass (realSubject.getClass ());
         // 3. disposed callback 
        enhancer.setCallback ( the this );
         // 4. subclassed 
        return enhancer.create ();
    }
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)
            throws Throwable {
        System.out.println ( "Song Zhe Wang Baoqiang maintenance instead of micro-blog" );
        Object returnValue=method.invoke(realSubject,objects);
        System.out.println ( "Song Zhe and Wang Baoqiang fans instead of interaction" );
         return returnValue;
    }
}

public class Main {
    public static void main(String[] args) {
        RealSubject realSubject=new RealSubject();
        RealSubject proxy=
                (RealSubject)new ProxyFactory(realSubject)
                .getProxyInstance();
        proxy.request();
    }
}
  • Difference: Static Proxy difference is that with JDK dynamic proxy class is to write to a combination of a target object, a function internal to write anonymous, both in common with the original class proxy class must work together to achieve the target interface. The Cglib then do not achieve the target interface. The first two code maintenance phase for Cglib inconvenient.
  • Application scenarios: AOP programming section can usually handle transaction management (open affairs, operations , commit the transaction), safety testing (verification authority, enter ), the cache ( to access the database , stored in the cache). AOP defaults to using dynamic proxies.

Guess you like

Origin www.cnblogs.com/hbsdljz/p/11077377.html