动态代理 + 责任链

 流程图

示例代码

  1 package DPCOR;
  2 
  3 import java.lang.reflect.InvocationHandler;
  4 import java.lang.reflect.Method;
  5 import java.lang.reflect.Proxy;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 
  9 /**
 10  * 假设有一个保存数据的方法 保存前需要2次验证,保存后需要1次验证
 11  * 
 12  * @author renguanyu
 13  *
 14  */
 15 public class ProxyCORDemo {
 16 
 17     public static void main(String[] args) {
 18 
 19         System.out.println("============ 动态代理 + 责任链 =============");
 20 
 21         // 示例数据
 22         Emp e1 = new Emp("1", "liubei");
 23 
 24         EmpDao empImpl = new EmpImpl();
 25 
 26         Filter filter1 = new Filter1(empImpl);// 验证1
 27         Filter filter2 = new Filter2(empImpl);// 验证2
 28         Filter filter3 = new Filter3(empImpl);// 业务处理
 29         Filter filter4 = new Filter4(empImpl);// 验证3
 30 
 31         FilterChain filterChain = new FilterChain();
 32 
 33         filterChain.addFilter(filter1);
 34         filterChain.addFilter(filter2);
 35         filterChain.addFilter(filter3);
 36         filterChain.addFilter(filter4);
 37 
 38         EmpDao empDao = (EmpDao) Proxy.newProxyInstance(Emp.class.getClassLoader(), new Class[] { EmpDao.class },
 39                 filterChain);
 40 
 41         empDao.save(e1);
 42     }
 43 }
 44 
 45 class Emp {
 46 
 47     String id;
 48     String name;
 49 
 50     public Emp(String id, String name) {
 51         super();
 52         this.id = id;
 53         this.name = name;
 54     }
 55 
 56     @Override
 57     public String toString() {
 58         return "Emp [id=" + id + ", name=" + name + "]";
 59     }
 60 
 61 }
 62 
 63 interface EmpDao {
 64 
 65     void save(Emp e);
 66 
 67 }
 68 
 69 class EmpImpl implements EmpDao {
 70 
 71     @Override
 72     public void save(Emp e) {
 73 
 74         System.out.println("save - > " + e);
 75 
 76     }
 77 
 78 }
 79 
 80 interface Filter {
 81 
 82     void doFilter(Object proxy, Method method, Object[] args, FilterChain filterChain) throws Throwable;
 83 
 84 }
 85 
 86 class Filter1 implements Filter {
 87 
 88     private Object object;
 89 
 90     public Filter1(Object object) {
 91         this.object = object;
 92     }
 93 
 94     @Override
 95     public void doFilter(Object proxy, Method method, Object[] args, FilterChain filterChain) throws Throwable {
 96         System.out.println("验证1 - OK ");
 97         filterChain.invoke(proxy, method, args);
 98     }
 99 
100 }
101 
102 class Filter2 implements Filter {
103 
104     private Object object;
105 
106     public Filter2(Object object) {
107         this.object = object;
108     }
109 
110     @Override
111     public void doFilter(Object proxy, Method method, Object[] args, FilterChain filterChain) throws Throwable {
112         System.out.println("验证2 - OK ");
113         filterChain.invoke(proxy, method, args);
114     }
115 
116 }
117 
118 class Filter3 implements Filter {
119 
120     private Object object;
121 
122     public Filter3(Object object) {
123         this.object = object;
124     }
125 
126     @Override
127     public void doFilter(Object proxy, Method method, Object[] args, FilterChain filterChain) throws Throwable {
128 
129         System.out.println("执行业务逻辑 ↓ ");
130 
131         Object o = method.invoke(object, args);
132 
133         System.out.println("执行业务逻辑 ↑ ");
134 
135         filterChain.invoke(proxy, method, args);
136 
137     }
138 
139 }
140 
141 class Filter4 implements Filter {
142 
143     private Object object;
144 
145     public Filter4(Object object) {
146         this.object = object;
147     }
148 
149     @Override
150     public void doFilter(Object proxy, Method method, Object[] args, FilterChain filterChain) throws Throwable {
151 
152         System.out.println("验证3 - OK ");
153 
154         filterChain.invoke(proxy, method, args);
155 
156     }
157 
158 }
159 
160 class FilterChain implements InvocationHandler {
161 
162     List<Filter> filters = new ArrayList<>();
163     int index = 0;
164 
165     public void addFilter(Filter f) {
166         filters.add(f);
167     }
168 
169     @Override
170     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
171 
172         if (index == filters.size())
173             return null;
174         Filter filter = filters.get(index);
175         index++;
176 
177         filter.doFilter(proxy, method, args, this);
178 
179         return null;
180     }
181 
182 }
原文地址:https://www.cnblogs.com/renguanyu/p/12944334.html