1 public class TestProxy { 2 public static void main(String[] args) { 3 Object obj = new ProxyObject(); 4 obj.action(); 5 } 6 } 7 8 interface Object{ 9 void action(); 10 } 11 //代理类 12 class ProxyObject implements Object{ 13 Object obj; 14 15 public ProxyObject(){ 16 System.out.println("代理类创建成功"); 17 obj = new ObjctImpl(); 18 } 19 20 public void action(){ 21 System.out.println("代理类开始执行"); 22 obj.action(); 23 System.out.println("代理类执行结束"); 24 } 25 } 26 //被代理类 27 class ObjctImpl implements Object{ 28 29 @Override 30 public void action() { 31 System.out.println("=====被代理类开始执行======"); 32 System.out.println("=====具体的操作======"); 33 System.out.println("=====被代理类执行完毕======"); 34 35 } 36 37 }