代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问。
主方法
public class main {
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.Request();
}
}
接口类
public interface Subject {
public abstract void Request();
}
实现类(被代理类)
public class RealSubject implements Subject {
public void Request() {
System.out.println("真实的请求");
}
}
代理类
public class Proxy implements Subject {
RealSubject realSubject;
public void Request() {
if (realSubject == null) {
realSubject = new RealSubject();
}
realSubject.Request();
}
}