代理模式结构图
代码
public class ProxyTest {
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.Request();
}
}
//抽象类
public abstract class Subject {
public abstract void Request();
}
//被代理的对象
public class RealSubject extends Subject{
@Override
public void Request() {
System.out.println("真实的请求");
}
}
//代理对象
public class Proxy extends Subject{
Subject subject;
@Override
public void Request() {
if (subject == null){
subject = new RealSubject();
}
System.out.println("代理方法");
subject.Request();
}
}