java代理【转载】

代理模式在Spring中,运用的淋漓精致,面向切面的编程,变得很有趣

 

假设有个实际书籍供应商,网上书籍供应商,而网上书籍供应商是代理商

 

 

Java代码   收藏代码
  1. package  com.study.proxy;  
  2.   
  3. public   interface  Subject {  
  4.     public   void  sailBook();  
  5. }  

 package com.study.proxy;

 

Java代码   收藏代码
  1. //真实卖家,最初卖家   
  2. public   class  RealSubject  implements  Subject {  
  3.   
  4.     @Override   
  5.     public   void  sailBook() {  
  6.         System.out.println("卖书" );  
  7.     }  
  8.   
  9. }  

网上代理商代理类:

 

Java代码   收藏代码
  1. package  com.study.proxy;  
  2.   
  3. import  java.lang.reflect.InvocationHandler;  
  4. import  java.lang.reflect.Method;  
  5. import  java.util.Date;  
  6. //代理商   
  7. public   class  MyHandler  implements  InvocationHandler {  
  8.       
  9.     private  Object realSub =  null ;  
  10.     public   void  setSub(Object sub){  
  11.         this .realSub = sub;  
  12.     }  
  13.       
  14.     @Override   
  15.     public  Object invoke(Object proxy, Method method, Object[] args)  
  16.             throws  Throwable {  
  17.         if ( this .realSub== null ){  
  18.             System.out.println("你无权操作" ); //判断权限   
  19.             return   null ;  
  20.         }else {  
  21.             String dazhe = DaZhe();  
  22.             System.out.println(dazhe);  
  23.             Object rtObj = method.invoke(realSub, args);  
  24.             String give = Give();  
  25.             System.out.println(give);  
  26.             String log = dazhe+give+new  Date();  
  27.             System.out.println(log);  
  28.             return  rtObj;  
  29.         }  
  30.           
  31.     }  
  32.     private  String DaZhe(){  
  33.         return   "打折信息" ;  
  34.     }  
  35.       
  36.     private  String Give(){  
  37.         return   "送代金券" ;  
  38.     }  
  39.       
  40. }  

 

 客户端,测试类

 

Java代码   收藏代码
  1. package  com.study.proxy;  
  2.   
  3. import  java.lang.reflect.Proxy;  
  4.   
  5. public   class  Client {  
  6.   
  7.     public   static   void  main(String[] args) {  
  8.         Subject sub = new  RealSubject();  
  9.         MyHandler handler1 = new  MyHandler();  
  10.         MyHandler handler2 = new  MyHandler();  
  11.         handler1.setSub(sub);  
  12.         Subject proxySub1 = (Subject) Proxy.newProxyInstance(  
  13.                 RealSubject.class .getClassLoader(),  
  14.                 RealSubject.class .getInterfaces(), handler1);  
  15.         Subject proxySub2 = (Subject) Proxy.newProxyInstance(  
  16.                 RealSubject.class .getClassLoader(),  
  17.                 RealSubject.class .getInterfaces(), handler2);  
  18.         proxySub1.sailBook();  
  19.         proxySub2.sailBook();  
  20.     }  
  21.   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值