Proxy mode-block mode, proxy Mode

Source: Internet
Author: User

Proxy mode-block mode, proxy Mode
Proxy mode-Building Block Mode

The proxy mode is indeed very common and commonly used in actual development. On the surface, the proxy mode generates a proxy class. as a proxy for accessing the actual implementation class, it controls external access to the actual proxy class, in addition, some enhanced functions are added. In fact, the actual implementation is used as a granularity to separate and decouple some of the enhanced functions.

The following code is recorded:

Static Proxy:

Public interface Subject {void toilet ();} public class RealSubject implements Subject {public void toilet () {System. out. println ("toilet") ;}/ *** the proxy class controls the access permissions of real roles *. From the perspective of the proxy class's own enhancement, it can be said that it controls the granularity subdivision of the proxy function */public class SubjectProxy implements Subject {Subject subject = new RealSubject (); public void toilet () {System. out. println ("take my phone"); subject. toilet (); System. out. println ("smell bad ");}}

Dynamic proxy (java proxy ):

public class ProxyHandler implements InvocationHandler {    private Object object;    public ProxyHandler() {    }    public ProxyHandler(Object object) {        this.object = object;    }    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {        System.out.println("take my phone");        Object result = method.invoke(object,args);        System.out.println("smell bad");        return result;    }}public class TestProxy {    public static void main(String[] args) {        Subject subject = new RealSubject();        ProxyHandler proxyHandler = new ProxyHandler(subject);        Subject subjectProxy = (Subject)Proxy.newProxyInstance(subject.getClass().getClassLoader(),                subject.getClass().getInterfaces(),proxyHandler);        subjectProxy.toilet();    }}

Dynamic proxy (cglib ):

public class CglibProxyInterceptor implements MethodInterceptor{    public Object intercept(Object o, Method method,                            Object[] args, MethodProxy methodProxy)            throws Throwable {        System.out.println("take my phone");        Object o1 = methodProxy.invokeSuper(o, args);        System.out.println("smell bad");        return o1;    }}public class CglibProxyTest {    public static void main(String[] args) {        CglibProxyInterceptor cglibProxyInterceptor = new CglibProxyInterceptor();        Enhancer enhancer = new Enhancer();        enhancer.setCallback(cglibProxyInterceptor);        enhancer.setSuperclass(RealSubject.class);        Subject subject = (Subject)enhancer.create();        subject.toilet();    }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.