例子
Proxy
public class ProxyObject implements Object {
Object obj;
public ProxyObject() {
System.out.println("这是代理类");
obj = new ObjectImpl();
}
public void action() {
System.out.println("代理开始");
obj.action();
System.out.println("代理结束");
}
}
Subject
public interface Object {
void action();
}
RealSubject
public class ObjectImpl implements Object {
public void action() {
System.out.println("========");
System.out.println("========");
System.out.println("这是被代理的类");
System.out.println("========");
System.out.println("========");
}
}
Test
public class Test {
public static void main() {
Object obj = new ProxyObject();
obj.action();
}
}
result
这是代理类
代理开始
========
========
这是被代理的类
========
========
代理结束