其实每个模式的名字就表明了该模式的作用,代理模式就是多一个代理类,替换原对象进行一些操作。关系图如下:
代码:
![](http://dl.iteye.com/upload/attachment/0083/1197/ea094ad9-efc5-337d-a8e8-ce9223511144.jpg)
代码:
public interface Sourceable{
public void method();
}
public class Source implements Sourceable{
@Override
public void method(){
System.out.println("this is Source!");
}
}
public class Proxy implements Sourceable{
private Source source;
public Proxy(){
super();
this.source=new Source();
}
@Override
public void method(){
before();
source.method();
after();
}
private void before(){
System.out.println("this is before!");
}
private void after(){
System.out.println("this is after!!");
}
}
public class ProxyTest{
public static void main(String[] args){
Sourceable source=new Proxy();
source.method();
}