23种设计模式学习之代理模式

被代理接口

public interface Source {
     void method1();
}

 被代理对象

public class SourceImpl implements Source {
    @Override
    public void method1() {
        System.out.println("2");
    }
}

 代理对象

public class Proxy implements Source {
    private SourceImpl source;

    public Proxy() {
        this.source = new SourceImpl();
    }

    @Override
    public void method1() {
        before();
        source.method1();
        after();
    }
    private void before(){
        System.out.println("before");
    }
    private void after(){
        System.out.println("after");
    }
}

 实例

public class Demo {
    public static void main(String[] args) {
        Source source =new Proxy();
        source.method1();
    }
}

 

posted @ 2018-01-16 15:07  我_会飞的鱼  阅读(129)  评论(0编辑  收藏  举报