设计模式- 代理模式 (静态代理 和 动态代理)
原创
©著作权归作者所有:来自51CTO博客作者原小明呢的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.代理
(1)意图:
为其他对象提供一种代理以控制对这个对象的访问。
(2)适用性:
在需要用比较通用和复杂的对象指针代替简单的指针的时候,使用Proxy模式。下面是一 些可以使用Proxy 模式常见情况:
1) 远程代理(Remote Proxy )为一个对象在不同的地址空间提供局部代表。 NEXTSTEP[Add94] 使用NXProxy 类实现了这一目的。Coplien[Cop92] 称这种代理为“大使” (Ambassador )。
2 )虚代理(Virtual Proxy )根据需要创建开销很大的对象。在动机一节描述的ImageProxy 就是这样一种代理的例子。
3) 保护代理(Protection Proxy )控制对原始对象的访问。保护代理用于对象应该有不同 的访问权限的时候。例如,在Choices 操作系统[ CIRM93]中KemelProxies为操作系统对象提供 了访问保护。
4 )智能指引(Smart Reference )取代了简单的指针,它在访问对象时执行一些附加操作。 它的典型用途包括:
对指向实际对象的引用计数,这样当该对象没有引用时,可以自动释放它(也称为SmartPointers[Ede92 ] )。当第一次引用一个持久对象时,将它装入内存。在访问一个实际对象前,检查是否已经锁定了它,以确保其他对象不能改变它。
2.静态代理实例
(1)场景描述
在操作数据库的时候,我们在进行增,修,删的时候,需要提交事务操作;
(2)示例操作 :模拟数据库dao操作
1)dao接口
public interface DocmentDao {
void insertDoc();
void updateDoc();
}
2)dao实现接口
public class DocmentDaoImpl implements DocmentDao {
@Override
public void insertDoc() {
System.out.println("添加文档成功");
}
@Override
public void updateDoc() {
System.out.println("更新文档成功");
}
}
3)事务实现
public class Transtion {
public void beginTranstion(){
System.out.println("开启事务");
}
public void closeTranstion(){
System.out.println("提交事务");
}
}
4)dao代理实现
public class DocmentDaoProxy implements DocmentDao {
private DocmentDao documentDao;
private Transtion transtion;
public DocmentDaoProxy(DocmentDao documentDaoImpl, Transtion transtion) {
super();
this.documentDao = documentDaoImpl;
this.transtion = transtion;
}
@Override
public void insertDoc() {
this.transtion.beginTranstion();
this.documentDao.insertDoc();
this.transtion.closeTranstion();
}
@Override
public void updateDoc() {
this.transtion.beginTranstion();
this.documentDao.updateDoc();
this.transtion.closeTranstion();
}
}
5)测试
@Test
public void testProxy(){
DocmentDao docmentDao=new DocmentDaoImpl();
Transtion transtion=new Transtion();
DocmentDaoProxy daoProxy = new DocmentDaoProxy(docmentDao, transtion);
daoProxy.insertDoc();
}
3.示例2 : 使用Spring ,模拟dao层事务提交
(1)bean.xml 配置实现
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="docmentDaoImpl" class="cn.labelnet.proxy.spring.DocmentDaoImpl"></bean>
<bean id="transtion" class="cn.labelnet.proxy.spring.Transtion"></bean>
<bean id="docmentDaoProxy" class="cn.labelnet.proxy.spring.DocmentDaoProxy">
<constructor-arg index="0" ref="docmentDaoImpl"></constructor-arg>
<constructor-arg index="1" ref="transtion"></constructor-arg>
</bean>
</beans>
(2)测试实现
@Test
public void testSpringProxy(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
DocmentDaoProxy daoProxy=(DocmentDaoProxy) context.getBean("docmentDaoProxy");
daoProxy.updateDoc();
}
4.静态代理总结
静态代理模式的缺点:
1)如果一个系统有多个dao,则需要创建多个代理对象;
2)如果igedao中有很多方法需要事物,则代理对象的方法中重复的代码很多;
3)proxy的重用行不强;
5.动态代理
使用拦截器实现 ;
场景描述 :同样是模拟数据库dao层实现
(1)dao接口
public interface DocmentDao {
void insertDoc();
void updateDoc();
}
(2)dao接口实现
public class DocmentDaoImpl implements DocmentDao {
@Override
public void insertDoc() {
System.out.println("添加文档成功");
}
@Override
public void updateDoc() {
System.out.println("更新文档成功");
}
}
(3)事务实现
public class Transtion {
public void beginTranstion(){
System.out.println("开启事务");
}
public void closeTranstion(){
System.out.println("提交事务");
}
}
(4)拦截器实现
/**
* 事务动态代理拦截器
*
*/
public class TranstionProxy implements InvocationHandler {
private Object target;
private Transtion transtion;
public TranstionProxy(Object target, Transtion transtion) {
super();
this.target = target;
this.transtion = transtion;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
this.transtion.beginTranstion();
method.invoke(target, args);
this.transtion.closeTranstion();
return null;
}
}
(5)测试
@Test
public void testProxy(){
//目标对象
Object daoImpl = new DocmentDaoImpl();
//
Transtion transtion=new Transtion();
//拦截器
TranstionProxy transtionProxy = new TranstionProxy(daoImpl, transtion);
//
DocmentDao dao = (DocmentDao) Proxy.newProxyInstance(daoImpl.getClass().getClassLoader(),daoImpl.getClass().getInterfaces(),transtionProxy);
dao.updateDoc();
}
6.动态代理总结
产生的代理对象和目标对象实现了共同的接口;(jdk动态代理)(例如5的示例)
JDK的动态代理 :
1. 用Jdk的API做到的;
2. 代理对象时动态产生的;
2)代理对象时目标对象的子类;(spring:cglib动态代理) (后面实现)