例:
// 抽象角色
abstract public class Subject
{
abstract public void request();
}
//真实角色:实现了Subject的request()方法
public class RealSubject extends Subject
{
public RealSubject()
{
}
public void request()
{
System.out.println("From real subject.");
}
}
//代理角色
public class ProxySubject extends Subject
{
private RealSubject realSubject; // 以真实角色作为代理角色的属性
public ProxySubject()
{
}
public void request() // 该方法封装了真实对象的request方法
{
preRequest();
if (realSubject == null)
{
realSubject = new RealSubject();
}
realSubject.request(); // 此处执行真实对象的request方法
postRequest();
}
private void preRequest()
{
// something you want to do before requesting
}
private void postRequest()
{
// something you want to do after requesting
}
}
//客户端调用
public class Client
{
public static void main(String[] args)
{
Subject sub = new ProxySubject();
sub.request();
}
}