简单举例代理的使用,其中ActionProxy和UserAction都继承接口Action //代理类 class ActionProxy implements Action{ private Action tager; public ActionProxy(Action action){ this.tager = action; } @Override public void doSamthing() { //多加一层做自己的事情 this.tager.doSamthing(); } } //业务接口 interface Action{ void doSamthing(); } //实现对象 class UserAction implements Action{ @Override public void doSamthing() { System.out.println("做工作"); } }
调用:
Action action = new UserAction(); ActionProxy proxy = new ActionProxy(action); proxy.doSamthing();