代理模式

代理模式(Proxy) 
为其他对象提供一种代理机制以控制对这个对象的访问。 

代理模式的应用 
1) 远程代理:为一个对象在不同的地址空间提供代理,如WebService, Java RMI, Caucho Hessian 
2) 虚拟代理:根据需要创建开销很大的对象,通过它来存放实例化需要很长时间的真实对象,如:Web代理服务器,反向代理Nginx, Squid 
3) 其他代理:如控制真实对象的访问权限,计算真实对象的调用次数,记录日志、AOP事务处理等 

一个简单的AOP事务处理代码(使用JDK动态代理实现) 

Java代码   收藏代码
  1. package test.gof.proxy;  
  2. import test.gof.factory.Item;  
  3. //商品服务  
  4. public interface ItemService {  
  5.     int addItem(Item item);  
  6. }  


Java代码   收藏代码
  1. package test.gof.proxy;  
  2. import test.gof.factory.Item;  
  3. import test.gof.factory.ItemDAO;  
  4. import test.gof.factory.ItemDAOMySQLImpl;  
  5. import test.gof.factory.UserDAO;  
  6. import test.gof.factory.UserDAOMySQLImpl;  
  7. //商品服务逻辑实现  
  8. public class ItemServiceImpl implements ItemService {  
  9.   
  10.     private ItemDAO itemDAO = new ItemDAOMySQLImpl();  
  11.     private UserDAO userDAO = new UserDAOMySQLImpl();  
  12.   
  13.     @Override  
  14.     public int addItem(Item item) {  
  15.                   //添加一个商品,同时更新用户的商品总数  
  16.         int result = itemDAO.insert(item);  
  17.         userDAO.updateItemCount(item.getUserId(), 1);  
  18.         return result;  
  19.     }  
  20.   
  21.     public void setItemDAO(ItemDAO itemDAO) {  
  22.         this.itemDAO = itemDAO;  
  23.     }  
  24.   
  25.     public void setUserDAO(UserDAO userDAO) {  
  26.         this.userDAO = userDAO;  
  27.     }  
  28.   
  29. }  

Java代码   收藏代码
  1. package test.gof.proxy;  
  2. import java.lang.reflect.InvocationHandler;  
  3. import java.lang.reflect.Method;  
  4. //事务模板  
  5. public class TransactionHandler implements InvocationHandler {  
  6.   
  7.     private Object proxyObj;//被代理的对象  
  8.   
  9.     @Override  
  10.     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
  11.   
  12.         Object result = null;  
  13.         try {  
  14.             System.out.println("start transaction");  
  15.   
  16.             result = method.invoke(proxyObj, args);  
  17.   
  18.             System.out.println("commit transaction");  
  19.   
  20.         } catch (Exception e) {  
  21.             System.out.println("rollback transaction");  
  22.             e.printStackTrace();  
  23.         }  
  24.         return result;  
  25.     }  
  26.   
  27.     public void setProxyObj(Object proxyObj) {  
  28.         this.proxyObj = proxyObj;  
  29.     }  
  30.   
  31. }  

Java代码   收藏代码
  1. package test.gof.proxy;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.lang.reflect.InvocationTargetException;  
  8. import java.lang.reflect.Method;  
  9. import java.lang.reflect.Proxy;  
  10.   
  11. import org.junit.Test;  
  12.   
  13. import sun.misc.ProxyGenerator;  
  14. import test.gof.factory.Item;  
  15.   
  16. public class TestReflect {  
  17.   
  18.     @Test  
  19.     public void testProxy() {  
  20.         Item item = new Item();  
  21.         item.setUserId(1);  
  22.         item.setItemName("hello kitty");  
  23.   
  24.         ItemService proxy = (ItemService) this.getProxy(new ItemServiceImpl(), new TransactionHandler());  
  25.         proxy.addItem(item);  
  26.     }  
  27.   
  28.     /** 
  29.      * @param proxyObj 代理对象 
  30.      * @param handler 事务模板 
  31.      * @return 
  32.      */  
  33.     public Object getProxy(Object proxyObj, TransactionHandler handler) {  
  34.         handler.setProxyObj(proxyObj);  
  35.         return Proxy.newProxyInstance(proxyObj.getClass().getClassLoader(), proxyObj.getClass().getInterfaces(), handler);  
  36.     }  
  37.   
  38.     @SuppressWarnings("unchecked")  
  39.     @Test  
  40.     public void testProxyGenerator() {  
  41.                   //生成的代理类文件,可以用jad反编译查看源码  
  42.         String proxyName = "ItemServiceProxy";  
  43.         ItemServiceImpl impl = new ItemServiceImpl();  
  44.         Class[] interfaces = impl.getClass().getInterfaces();  
  45.         byte[] proxyClassFile = ProxyGenerator.generateProxyClass(proxyName, interfaces);  
  46.         File file = new File("classes/ItemServiceProxy.class");  
  47.         try {  
  48.             FileOutputStream fos = new FileOutputStream(file);  
  49.             fos.write(proxyClassFile);  
  50.             fos.flush();  
  51.             fos.close();  
  52.   
  53.             System.out.println("ItemServiceProxy: " + file.getAbsolutePath());  
  54.         } catch (FileNotFoundException e) {  
  55.             e.printStackTrace();  
  56.         } catch (IOException e) {  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60.   
  61. }  
  62.   
  63. testProxy执行结果  
  64.   
  65. start transaction  
  66. insert item to MySQL  
  67. update itemCount to MySQL  
  68. commit transaction  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值