隐藏真实访问对象,对外代理对象
package structural;
public class ProxyMain {
public static void main(String[] args) {
ProxySubject proxy = new ProxySubject();
proxy.getRequest();
}
}
interface Subject{
void getRequest();
}
class RealSubject implements Subject{
@Override
public void getRequest() {
System.out.println("你访问到真的我了!");
}
}
class ProxySubject implements Subject{
private RealSubject realSubject;
@Override
public void getRequest() {
if(realSubject == null){
realSubject = new RealSubject();
}
preRequest();
realSubject.getRequest();
endRequest();
}
private void preRequest() {
System.out.println("处理前!");
}
private void endRequest() {
System.out.println("处理后!");
}
}