作用
多一个代理类,替原对象做一些操作
写法
public interface Sourceable {
public void method();
}
public class Source implements Sourceable {
@Override
public void method() {
System.out.println("the original method!");
}
}
public class Proxy implements Sourceable {
private Source source;
public Proxy(){
super();
this.source = new Source();
}
@Override
public void method() {
before();
source.method();
atfer();
}
private void atfer() {
System.out.println("after proxy!");
}
private void before() {
System.out.println("before proxy!");
}
}
测试类:
public class ProxyTest {
public static void main(String[] args) {
Sourceable source = new Proxy();
source.method();
}
}
输出:
before proxy!
the original method!
after proxy!
应用场景
如果已有的方法在使用的时候需要对原有的方法进行改进,此时有两种办法:
- 修改原有的方法来适应。这样违反了“对扩展开放,对修改关闭”的原则
- 采用一个代理类调用原有的方法,且对产生的结果进行控制。这种方法就是代理模式
使用代理模式,可以将功能划分的更加清晰,有助于后期维护
补充:
此文中我们提到的代理模式是静态代理,实际上在大的项目中需要使用动态代理
网友评论