标签:
public abstract classAbstractObject{
//操作
public abstract void operation();
}
publicclassRealObject extends AbstractObject{
@Override
publicvoid operation(){
//一些操作
System.out.println("一些操作");
}
}
publicclassProxyObject extends AbstractObject{
RealObject realObject =newRealObject();
@Override
publicvoid operation(){
//调用目标对象之前可以做相关操作
System.out.println("before");
realObject.operation();
//调用目标对象之后可以做相关操作
System.out.println("after");
}
}
publicclassClient{
publicstaticvoid main(String[] args){
AbstractObject obj =newProxyObject();
obj.operation();
}
}
publicclassProxyImage implements Image{
privateRealImage realImage;
privateString fileName;
publicProxyImage(String fileName){
this.fileName = fileName;
}
@Override
publicvoid display(){
if(realImage == null){
realImage =newRealImage(fileName);
}
realImage.display();
}
}
标签:
原文地址:http://www.cnblogs.com/Doing-what-I-love/p/5621171.html