一、定义:为其他对象提供一种代理以控制对这个对象的访问。
二、示例代码:
//代理类和实际对象类共同实现的接口
public interface Subject {
void dancing();
void singing();
}
//实际对象类
public class RealObject implements Subject{
@Override
public void dancing() {
System.out.println("跳舞");
}
@Override
public void singing() {
System.out.println("唱歌");
}
}
//代理类
public class Proxy implements Subject {
RealObject realObject;
public Proxy() {
realObject = new RealObject();
}
@Override
public void dancing() {
realObject.dancing();
}
@Override
public void singing() {
realObject.singing();
}
}
//客户端类
public class Client {
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.dancing();
proxy.singing();
}
}