代理模式原理java实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代理模式原理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实例的主要内容,如果未能解决你的问题,请参考以下文章