静态代理设计与动态代理设计

静态代理设计模式

代理设计模式最本质的特质:一个真实业务主题只完成核心操作,而所有与之辅助的功能都由代理类来完成。

 

例如,在进行数据库更新的过程之中,事务处理必须起作用,所以此时就可以编写代理设计模式来完成。

 

范例:结合传统的代理设计模式以及以购物车CartDao为例来编写代理设计模式

package so.strong.mall.proxy;
import java.util.List;
public interface CartDao {boolean insert(Cart cart) throws Exception;List<Cart> findAll() throws Exception;
}

以上CartDao接口定义的方法,更行插入一定需要事务控制,对于查询操作,不需要事务控制。

 

定义CartDao真实实现

package so.strong.mall.proxy;
import java.util.List;
public class CartDAOImpl implements CartDao{@Overridepublic boolean insert(Cart cart) throws Exception {System.out.println("=====执行数据增加操作=====");return false;}@Overridepublic List<Cart> findAll() throws Exception {System.out.println("=====执行数据列表操作=====");return null;}
}

 

定义代理主题类

package so.strong.mall.proxy;
import java.util.List;
public class CartDAOProxy implements CartDao {private CartDao cartDao;public CartDAOProxy() {}public void setCartDao(CartDao cartDao) {this.cartDao = cartDao;}public void prepare() {System.out.println("=====取消掉jdbc的自动提交");}public void commit() {System.out.println("=====手工提交事务");}public void rollback() {System.out.println("=====出现错误,事务回滚");}@Overridepublic boolean insert(Cart cart) throws Exception {try {this.prepare();boolean flag = this.cartDao.insert(cart);this.commit();return flag;} catch (Exception e) {this.rollback();throw e;}}@Overridepublic List<Cart> findAll() throws Exception {return this.cartDao.findAll();}
}

 

业务层现在并不关心到底是代理类还是真实主题类,它只关心一点,只要取得了CartDao接口对象就可以,那么这一操作可以通过工厂类来隐藏。

package so.strong.mall.proxy;
public class DAOFactory {public static CartDao getCartDaoInstance() {CartDAOProxy proxy = new CartDAOProxy();proxy.setCartDao(new CartDAOImpl());return proxy;}
}

此时业务层暂时不需要继续进行,只需要通过客户端模拟业务层调用即可。

public class TestDemo {
public static void main(String[] args) throws Exception{CartDao dao = DAOFactory.getCartDaoInstance();dao.insert(new Cart());}
}
//=====取消掉jdbc的自动提交
//=====执行数据增加操作=====
//=====手工提交事务

因为事务和处理本身与核心业务有关的功能,但是它不是核心,那么用代理解决是最合适的方式。

 

动态代理设计模式

上面给出的代理设计模式的确可以完成代理要求,但是有一个问题:如果说现在项目里面有200张数据表,那么至少也需要200个左右的DAO接口,如果用上面的代理设计模式,那么意味着除了编写200个的DAO接口实现,还要编写200个代理类,并且有意思的是,这些代理类实现几乎相同。

以上的代理设计模式属于静态代理设计模式,只能够作为代理模式的雏形出现,并不能购作为代码使用的设计模式,为此专门引入了动态代理设计模式的概念。

即:利用一个代理类可以实现所有被代理的操作。

 

如果要想实现动态设计模式,那么必须首先观察一个接口:java.lang.reflect.InvocatonHandler.   它里面有一个方法

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;

这个方法就属于代理中调用真实主题类的操作方法,这个方法里面的参数意义如下:

  • Object proxy:表示代理类的对象;
  • Method method:表示现在正在调用的方法;
  • Object[] args:表示方法里面的参数。

但是这个方法没有所对应的真实对象,所以需要在创建这个类对象的时候设置好真实代理对象。

 

如果要想找到代理对象则要使用java.lang.reflect.Proxy类来动态创建,此类主要使用以下方法:

public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h) throws IllegalArgumentException

此方法参数定义如下:

  • ClassLoader loader :指的是取得对象的加载器;
  • Class<?>[] interfaces: 代理设计模式的核心是围绕接口进行的,所以此处必须取出全部的接口;
  • InvocationHandler h:代理的实现类。

 

范例:使用动态代理实现上面的代理

CartDao不变,修改CartDAOProxy代理类

package so.strong.mall.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class CartDAOProxy implements InvocationHandler {private Object obj; //这个是真实对象主题/*** 将要操作的真实主题对象绑定到代理之中,而后返回一个代理类对象* @param obj 真实对象主题* @return 代理类对象*/public Object bind(Object obj) {this.obj = obj;return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(), this);}public void prepare() {System.out.println("=====取消掉jdbc的自动提交");}public void commit() {System.out.println("=====手工提交事务");}public void rollback() {System.out.println("=====出现错误,事务回滚");}//只要执行了操作方法,那么就一定会触发invoke
    @Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Object object = null;//接收返回值if (method.getName().contains("insert")) { //更新插入类操作this.prepare();try {object = method.invoke(this.obj, args); //反射调用方法this.commit();} catch (Exception e) {this.rollback();}} else {object = method.invoke(this.obj, args);//查询操作不需要事务支持
        }return object;}
}
//修改工厂
package so.strong.mall.proxy;
public class DAOFactory {public static Object getCartDaoInstance(Object realObject) {return new CartDAOProxy().bind(realObject);}
}
//修改调用
package so.strong.mall.proxy;
public class TestDemo {public static void main(String[] args) throws Exception{CartDao dao =(CartDao) DAOFactory.getCartDaoInstance(new CartDAOImpl());dao.insert(new Cart());}
}

 

CGLIB实现动态代理设计模式

动态代理模式的确好用,而且也解决了代理类重复的问题,但是不管是传统静态代理或动态代理都有个设计缺陷,以动态代理为例:

return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this); //传入真实主题类,返回代理主题类

代理设计模式有一个硬性要求,就是类必须要有接口,所以业界很多人认为应该在没有接口的环境下也能使用代理设计模式。

所以在此时在开源社区里面提供了一个组件包——CGLIB,利用此包可以在没有接口的情况下也能够使用动态代理设计模式,它是模拟的类。

如果要想使用CGLIB,那么必须首先搞清楚对应关系:

  • Proxy:net.sf.cglib.proxy.Enhancer
  • InvocationHandler:net.sf.cglib.proxy.MethodInterceptor
  • 真实主题调用:net.sf.cglib.proxy.MethodProxy

老师课上使用的是引入CGLIB的jar包,我去mvn仓库找了一下,找到了一个cglib,放到pom里面发现也可以。

 <dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.2.2</version>
</dependency>

 

范例:使用CGLIB实现动态代理设计模式

package so.strong.mall.proxy;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;class ItemDAOImpl {public void insert(Item item) {System.out.println("=====增加操作=====");}
}class MyProxy implements MethodInterceptor {private Object target; //真实操作主题public MyProxy(Object target) {this.target = target;}@Overridepublic Object intercept(Object proxy, Method method, Object[] args,MethodProxy methodProxy) throws Throwable {Object object = null;this.prepare();object = method.invoke(this.target, args);this.commit();return object;}public void prepare() {System.out.println("=====取消掉jdbc的自动提交=====");}public void commit() {System.out.println("=====手工提交事务=====");}
}public class TestCGLIB {public static void main(String[] args) {ItemDAOImpl itemDAO = new ItemDAOImpl(); //真实主题对象//代理设计模式之中必须要有公共的集合点,例如:接口,而CGLIB没有接口Enhancer enhancer = new Enhancer(); //创建一个代理工具类enhancer.setSuperclass(ItemDAOImpl.class); //设置一个虚拟的父类enhancer.setCallback(new MyProxy(itemDAO)); //设置代理的回调操作ItemDAOImpl proxyDao = (ItemDAOImpl) enhancer.create();proxyDao.insert(new Item());}
}

可以发现此时没有了对接口的依赖,也可以实现动态代理设计,但是需要模拟代理的父类对象。

转载于:https://www.cnblogs.com/itermis/p/8940582.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390591.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

svm机器学习算法_SVM机器学习算法介绍

svm机器学习算法According to OpenCVs "Introduction to Support Vector Machines", a Support Vector Machine (SVM):根据OpenCV“支持向量机简介”&#xff0c;支持向量机(SVM)&#xff1a; ...is a discriminative classifier formally defined by a separating …

6.3 遍历字典

遍历所有的键—值对 遍历字典时&#xff0c;键—值对的返回顺序也与存储顺序不同。 6.3.2 遍历字典中的所有键 在不需要使用字典中的值时&#xff0c;方法keys() 很有用。 6.3.3 按顺序遍历字典中的所有键 要以特定的顺序返回元素&#xff0c;一种办法是在for 循环中对返回的键…

Google Guava新手教程

以下资料整理自网络 一、Google Guava入门介绍 引言 Guavaproject包括了若干被Google的 Java项目广泛依赖 的核心库&#xff0c;比如&#xff1a;集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [comm…

HTML DOM方法

querySelector() (querySelector()) The Document method querySelector() returns the first element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.Document方法querySelector()返回文档中与…

leetcode 773. 滑动谜题

题目 在一个 2 x 3 的板上&#xff08;board&#xff09;有 5 块砖瓦&#xff0c;用数字 1~5 来表示, 以及一块空缺用 0 来表示. 一次移动定义为选择 0 与一个相邻的数字&#xff08;上下左右&#xff09;进行交换. 最终当板 board 的结果是 [[1,2,3],[4,5,0]] 谜板被解开。…

数据科学领域有哪些技术_领域知识在数据科学中到底有多重要?

数据科学领域有哪些技术Jeremie Harris: “In a way, it’s almost like a data scientist or a data analyst has to be like a private investigator more than just a technical person.”杰里米哈里斯(Jeremie Harris) &#xff1a;“ 从某种意义上说&#xff0c;这就像是数…

python 算术运算

1. 算术运算符与优先级 # -*- coding:utf-8 -*-# 运算符含有,-,*,/,**,//,% # ** 表示^ , 也就是次方 a 2 ** 4 print 2 ** 4 , aa 16 / 5 print 16 / 5 , aa 16.0 / 5 print 16.0 / 5 , a# 结果再进行一次floor a 16.0 // 5.0 print 16.0 // 5.0 , aa 16 // 5 print …

c语言编程时碰到取整去不了_碰到编程墙时如何解开

c语言编程时碰到取整去不了Getting stuck is part of being a programmer, no matter the level. The so-called “easy” problem is actually pretty hard. You’re not exactly sure how to move forward. What you thought would work doesn’t.无论身在何处&#xff0c;陷…

初创公司怎么做销售数据分析_为什么您的初创企业需要数据科学来解决这一危机...

初创公司怎么做销售数据分析The spread of coronavirus is delivering a massive blow to the global economy. The lockdown and work from home restrictions have forced thousands of startups to halt expansion plans, cancel services, and announce layoffs.冠状病毒的…

leetcode 909. 蛇梯棋

题目 N x N 的棋盘 board 上&#xff0c;按从 1 到 N*N 的数字给方格编号&#xff0c;编号 从左下角开始&#xff0c;每一行交替方向。 例如&#xff0c;一块 6 x 6 大小的棋盘&#xff0c;编号如下&#xff1a; r 行 c 列的棋盘&#xff0c;按前述方法编号&#xff0c;棋盘格…

Python基础之window常见操作

一、window的常见操作&#xff1a; cd c:\ #进入C盘d: #从C盘切换到D盘 cd python #进入目录cd .. #往上走一层目录dir #查看目录文件列表cd ../.. #往上上走一层目录 二、常见的文件后缀名&#xff1a; .txt 记事本文本文件.doc word文件.xls excel文件.ppt PPT文件.exe 可执行…

WPF效果(GIS三维篇)

二维的GIS已经被我玩烂了&#xff0c;紧接着就是三维了&#xff0c;哈哈&#xff01;先来看看最简单的效果&#xff1a; 转载于:https://www.cnblogs.com/OhMonkey/p/8954626.html

css注释_CSS注释示例–如何注释CSS

css注释Comments are used in CSS to explain a block of code or to make temporary changes during development. The commented code doesn’t execute.CSS中使用注释来解释代码块或在开发过程中进行临时更改。 注释的代码不执行。 Both single and multi-line comments in…

r软件时间序列分析论文_高度比较的时间序列分析-一篇论文评论

r软件时间序列分析论文数据科学 &#xff0c; 机器学习 (Data Science, Machine Learning) In machine learning with time series, using features extracted from series is more powerful than simply treating a time series in a tabular form, with each date/timestamp …

leetcode 168. Excel表列名称

题目 给你一个整数 columnNumber &#xff0c;返回它在 Excel 表中相对应的列名称。 例如&#xff1a; A -> 1 B -> 2 C -> 3 … Z -> 26 AA -> 27 AB -> 28 … 示例 1&#xff1a; 输入&#xff1a;columnNumber 1 输出&#xff1a;“A” 示例 2&…

飞机订票系统

1 #include <stdio.h>2 #include <stdlib.h>3 #include <string.h>4 #include <conio.h>5 typedef struct flightnode{6 char flight_num[10]; //航班号7 char start_time[10]; //起飞时间8 char end_time[10]; //抵达时间9 char st…

解决Mac10.13 Pod报错 -bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.fram

升级10.13以后Pod命令失效&#xff0c;解决办法如下&#xff1a; 终端执行 brew link --overwrite cocoapods 复制代码尝试 Pod 命令是否已经恢复 若报错继续执行 brew reinstall cocoapodsbrew install rubybrew link --overwrite cocoapods 复制代码尝试 Pod 命令是否已经恢复…

angular示例_用示例解释Angular动画

angular示例为什么要使用动画&#xff1f; (Why use Animations?) Modern web components frequently use animations. Cascading Style-sheets (CSS) arms developers with the tools to create impressive animations. Property transitions, uniquely named animations, mu…

selenium抓取_使用Selenium的网络抓取电子商务网站

selenium抓取In this article we will go through a web scraping process of an E-Commerce website. I have designed this particular post to be beginner friendly. So, if you have no prior knowledge about web scraping or Selenium you can still follow along.在本文…

剑指 Offer 37. 序列化二叉树

题目 序列化是将一个数据结构或者对象转换为连续的比特位的操作&#xff0c;进而可以将转换后的数据存储在一个文件或者内存中&#xff0c;同时也可以通过网络传输到另一个计算机环境&#xff0c;采取相反方式重构得到原数据。 请设计一个算法来实现二叉树的序列化与反序列化…