Java Proxy Mode

Source: Internet
Author: User

Proxy mode

Decoupling of implementation logic and implementation of proxy mode

Proxy mode in order to provide additional operations, insert an object to replace the actual object. These operations typically involve communication with the actual object and the role of the agent acting as the middleman

    • Interface
/** * 接口 */public interface Interface {    void doSomething();    void somethingElse(String arg);}
    • Actual Object
/** * 实际对象 */public class RealObject implements Interface {    public void doSomething() {        System.out.println("doSomething");    }    public void somethingElse(String arg) {        System.out.println("somethingElse" + arg);    }}
    • Proxy Object
/** * 代理对象 */public class Proxy implements Interface {    private Interface proxied;    public Proxy(Interface proxied) {        this.proxied = proxied;    }    public void doSomething() {        System.out.println("Proxy doSomething");        proxied.doSomething();    }    public void somethingElse(String arg) {        System.out.println("Proxy somethingElse" + arg);        proxied.somethingElse(arg);    }}
    • Test
    /**     * 测试代理,比较原对象与代理对象     *     * @param args     */    public static void main(String[] args) {        Interface iface = new RealObject();        iface.doSomething();        iface.somethingElse("bonobo");        Interface iface2 = new Proxy(iface);        iface2.doSomething();        iface2.somethingElse("bonobo");    }
Dynamic Agent

The Java dynamic agent can dynamically create proxies and dynamically handle calls to the methods being proxied

All calls made in the dynamic are redirected to a single calling processor, and its job is to reveal the types of calls and determine the corresponding countermeasures

    • Dynamic Agent
public class DynamicProxyHandler implements InvocationHandler {    private Object proxied;    public DynamicProxyHandler(Object proxied) {        this.proxied = proxied;    }    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {        System.out.println("**** proxy:" + proxy.getClass() + ".method: " + method + ".args: " + args);        if (args != null) {            for (Object arg : args) {                System.out.println("    " + args);            }        }        return method.invoke(proxied, args);    }}
    • Test
    public static void main(String[] args){        RealObject real = new RealObject();        real.doSomething();        real.somethingElse("bonobo");        Interface proxy = (Interface) Proxy.newProxyInstance(                Interface.class.getClassLoader(),                new Class[]{Interface.class},                new DynamicProxyHandler(real));        proxy.doSomething();        proxy.somethingElse("bonobo");    }

Java Proxy Mode

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.