代理模式即某个对象代理其他对象并对外提供功能,如下例ProxyFuncImpl 代理FuncImpl对外提供功能。
- Func
public interface Func { public void doAction(); }
- FuncImpl
public class FuncImpl implements Func { @Override public void doAction() { System.out.println("FuncImpl"); } }
- ProxyFuncImpl
public class ProxyFuncImpl implements Func { private Func func; ProxyFuncImpl(){ func = new FuncImpl(); } @Override public void doAction() { System.out.println("ProxyFuncImpl"); func.doAction(); } }
- APP 测试类
public class App { public static void main(String[] args) { ProxyFuncImpl proxy = new ProxyFuncImpl(); proxy.doAction(); } }
- 结果输出
ProxyFuncImpl FuncImpl
时间: 2024-10-25 00:53:37