1.责任链模式
责任链模式其实就是多层拦截器 定义接口
public interface interceptor1 { public boolean before(Object proxy, Object target, Method method,Object [] args); public void around(Object proxy,Object target, Method method, Object [] args); public void after(Object proxy, Object target, Method method, Object [] args);}复制代码
实现接口1
package com.test3;import java.lang.reflect.Method;/** * Created by leewihong on 2018/6/19. */public class myinterceptor1 implements interceptor1 { @Override public boolean before(Object proxy, Object target, Method method, Object[] args) { System.out.println("进入了拦截器1"); return true; } @Override public void around(Object proxy, Object target, Method method, Object[] args) { System.out.println("拦截器1around方法"); } @Override public void after(Object proxy, Object target, Method method, Object[] args) { System.out.println("拦截器1after方法"); }}复制代码
实现接口2
package com.test3;import java.lang.reflect.Method;/** * Created by leewihong on 2018/6/19. */public class myinterceptor2 implements interceptor1 { @Override public boolean before(Object proxy, Object target, Method method, Object[] args) { System.out.println("进入拦截器2"); return true; } @Override public void around(Object proxy, Object target, Method method, Object[] args) { System.out.println("拦截器2around方法"); } @Override public void after(Object proxy, Object target, Method method, Object[] args) { System.out.println("拦截器2after方法"); }}复制代码
实现接口3
package com.test3;import java.lang.reflect.Method;/** * Created by leewihong on 2018/6/19. */public class myinterceptor3 implements interceptor1 { @Override public boolean before(Object proxy, Object target, Method method, Object[] args) { System.out.println("进去拦截器3方法"); return true; } @Override public void around(Object proxy, Object target, Method method, Object[] args) { System.out.println("拦截器3around方法"); } @Override public void after(Object proxy, Object target, Method method, Object[] args) { System.out.println("拦截器3after方法"); }}复制代码
创建代理
package com.test3;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;/** * Created by leewihong on 2018/6/19. */public class interceptorJDKProxy implements InvocationHandler { public Object target = null; public String interceptorString = null; public interceptorJDKProxy(Object target, String interceptorString){ this.target = target; this.interceptorString = interceptorString; } public static Object bind(Object target, String interceptorString){ return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass() .getInterfaces(),new interceptorJDKProxy(target,interceptorString)); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (interceptorString == null){// 反射原有的方法 return method.invoke(target,args); } Object result = null; interceptor1 interceptor1 =(interceptor1) Class.forName(interceptorString).newInstance(); if (interceptor1.before(proxy,target,method,args)){ result = method.invoke(target,args); } else { interceptor1.around(proxy,target,method,args); } interceptor1.after(proxy,target,method,args); return result; }}复制代码
定义测试接口
package com.test3;/** * Created by leewihong on 2018/6/19. */public interface test3_helloword { public void sayHelloWorld();}复制代码
实现测试接口
package com.test3;/** * Created by leewihong on 2018/6/19. */public class test3_helloworldImpl implements test3_helloword { @Override public void sayHelloWorld() { System.out.println("test3HelloWorld"); }}复制代码
测试多层拦截器
package com.test3;/** * Created by leewihong on 2018/6/19. */public class test3 { public static void main(String [] args){ test3_helloword test3_helloword1 = (test3_helloword) interceptorJDKProxy.bind(new test3_helloworldImpl(), "com.test3.myinterceptor1"); test3_helloword test3_helloword2 = (test3_helloword) interceptorJDKProxy.bind(test3_helloword1,"com" + ".test3.myinterceptor2"); test3_helloword test3_helloword3 = (test3_helloword) interceptorJDKProxy.bind(test3_helloword2,"com" + ".test3.myinterceptor3"); test3_helloword3.sayHelloWorld(); }}复制代码
测试结果
进去拦截器3方法进入拦截器2进入了拦截器1test3HelloWorld拦截器1after方法拦截器2after方法拦截器3after方法复制代码
2.观察者模式
定义一个被观察者
package com.test4;import java.util.ArrayList;import java.util.List;import java.util.Observable;import java.util.Observer;/** * Created by leewihong on 2018/6/20. */public class ProductList extends Observable {// 产品列表 private Listproductlist = null;// 类唯一实例 private static ProductList instance; private ProductList(){ } public static ProductList getInstance(){ if (instance == null){ instance = new ProductList(); instance.productlist = new ArrayList (); } return instance; }// 增加观察者 public void addProductListObserver(Observer observer){ this.addObserver(observer); }// 新增产品 public void addProduct(String newProduct){ productlist.add(newProduct); System.out.println("产品列表新增了产品"+newProduct); this.setChanged(); this.notifyObservers(newProduct); }}复制代码
定义一个京东观察者
package com.test4;import java.util.Observable;import java.util.Observer;/** * Created by leewihong on 2018/6/20. */public class JingDongObserver implements Observer { @Override public void update(Observable o, Object arg) { String newProduct = (String) arg; System.out.println("发送新产品"+newProduct+"同步到京东商城"); }}复制代码
定义一个淘宝观察者
package com.test4;import java.util.Observable;import java.util.Observer;/** * Created by leewihong on 2018/6/20. */public class TaoBaoObserver implements Observer { @Override public void update(Observable o, Object arg) { String newProduct = (String) arg; System.out.println("发送新产品"+newProduct+"同步到淘宝商城"); }}复制代码
测试结果
package com.test4;/** * Created by leewihong on 2018/6/20. */public class test4 { public static void main(String [] args){ ProductList observable = ProductList.getInstance(); TaoBaoObserver taoBaoObserver = new TaoBaoObserver(); JingDongObserver jingDongObserver = new JingDongObserver(); observable.addObserver(taoBaoObserver); observable.addObserver(jingDongObserver); observable.addProduct("iphoneX"); }}复制代码
输出结果
产品列表新增了产品iphoneX发送新产品iphoneX同步到京东商城发送新产品iphoneX同步到淘宝商城复制代码