为其他对象提供一种代理以控制对这个对象的访问
这里写图片描述实际上,代理跟realsubject是同一个父类(如此,某种意义下realsubject与proxy是一样的),proxy中包含了realsubject,proxy通过调用realsubject对象的方法来实现代理
代码
Main
public class Main {
public static void main(String[] args) {
Subject proxy=new Proxy(new RealSubject());
proxy.request();
}
}
interface:Subject
public interface Subject {
public void request();
}
imp:RealSubject
public class RealSubject implements Subject {
@Override
public void request() {
System.out.println("this is real subject");
}
}
imp:Proxy
public class Proxy implements Subject {
Subject subject=null;
public Proxy(Subject subject){
this.subject=subject;
}
@Override
public void request() {
this.subject.request();
}
}
网友评论