首页 > 代码库 > 代理模式原理java实例

代理模式原理java实例

package test;class ProxyTest {    public static void main(String[] args) {        ProxyClass proxy = new ProxyClass();        proxy.method_1();        /*         Console:             This is ProxyClass.method_1()            ---Before---            This is RelClass.method_1()            ---After----         */    }}class ProxyClass implements RelClassInf {    RelClass rel = new RelClass();        @Override    public void method_1() {        System.out.println("This is ProxyClass.method_1()");        System.out.println("---Before---");        rel.method_1();        System.out.println("---After----");    }    @Override    public void method_2() {        System.out.println("This is ProxyClass.method_2()");        System.out.println("---Before---");        rel.method_2();        System.out.println("---After----");    }    }interface RelClassInf {        void method_1();        void method_2();}class RelClass implements RelClassInf {    @Override    public void method_1() {        System.out.println("This is RelClass.method_1()");    }    @Override    public void method_2() {        System.out.println("This is RelClass.method_2()");    }    }

 

代理模式原理java实例