[关键字]:java,design pattern,设计模式,《Java与模式》学习,Proxy Pattern,代理模式
[环境]:StarUML5.0 + JDK6
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys/
[正文]:
package pattern.proxy;
/**
* 代理模式:Proxy Pattern
* @version 2009-6-27
* @author Winty(wintys@gmail.com)
*/
public class ProxyTest{
public static void main(String[] args){
Subject subject;
subject = new ProxySubject();
subject.request();
}
}
/**
* 抽象主题角色
*/
abstract class Subject{
public abstract void request();
}
/**
* 真实主题角色
*/
class RealSubject extends Subject{
@Override
public void request(){
System.out.println("do request.");
}
}
/**
* 代理主题角色
*/
class ProxySubject extends Subject{
private RealSubject realSubject;
private void preRequest(){
System.out.println("do preRequest");
}
public void request(){
preRequest();
if(realSubject == null){
realSubject = new RealSubject();
}
realSubject.request();
postRequest();
}
private void postRequest(){
System.out.println("do postRequest");
}
}
运行结果:
do preRequest
do request.
do postRequest
posted on 2009-06-28 09:43
天堂露珠 阅读(1137)
评论(2) 编辑 收藏 所属分类:
Pattern