1 概念
1 定义
为对象提供代理,以控制对这个对象的访问
代理对象在客户端和目标对象之间起到中介作用
分类:静态代理和动态代理
2 应用场景
3 优缺点
优点:
- 代理模式可以将代理对象和目标对象接耦,扩展性好
- 增强目标对象
缺点:
- 造成类数目增加
- 在客户端和目标对象间增加了一个代理对象,会导致请求速度变慢
- 增加系统复杂度
2 代码实现
场景:发送邮件前进行额外的操作
由程序员创建或特定工具自动生成源代码,再对其编译。在程序运行前,代理类的.class文件就已经存在了。
1 2 3
| public interface Email { public void send(); }
|
1 2 3 4 5 6 7 8
| public class FlashEmail implements Email { @Override public void send() { System.out.println("邮件发送中。。。。。"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class EmailProxy implements Email { Email email; public EmailProxy(Email email) { this.email=email; } @Override public void send() { System.out.println("发送邮件前准备。。。"); email.send(); System.out.println("邮件发送后。。。。。。"); } }
|
1 2 3 4 5 6 7 8 9
| public class Main { public static void main(String[] args) { Email email=new FlashEmail(); EmailProxy emailProxy=new EmailProxy(email); emailProxy.send(); } }
|
输出:
发送邮件前准备。。。
邮件发送中。。。。。
邮件发送后。。。。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class EmailDynamicProxy implements InvocationHandler { Object target; public EmailDynamicProxy(Object obj) { super(); this.target = obj; }
public Object bind(){ Class cls = target.getClass(); return Proxy.newProxyInstance(cls.getClassLoader(),cls.getInterfaces(),this); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("发送邮件前准备。。。"); method.invoke(target); System.out.println("发送后。。。。。。"); return target; } }
|
1 2 3 4 5 6 7
| public class Main { public static void main(String[] args) { Email email = new FlashEmail(); Email emailProxy = (Email) new EmailDynamicProxy(email).bind(); emailProxy.send(); } }
|