博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
12.设计模式
阅读量:6162 次
发布时间:2019-06-21

本文共 6785 字,大约阅读时间需要 22 分钟。

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 List
productlist = 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同步到淘宝商城复制代码

转载于:https://juejin.im/post/5c27330ae51d4558bf39799a

你可能感兴趣的文章
Quartz
查看>>
正则表达式的语法规则
查看>>
C#一个关于委托和事件通俗易懂的例子
查看>>
类似于SVN的文档内容差异对比工具winmerge
查看>>
Cause: java.sql.SQLException: The user specified as a definer ('root'@'%') does not exist
查看>>
quratz线程
查看>>
execnet: rapid multi-Python deployment
查看>>
windows修改3389端口
查看>>
关于JavaScript词法
查看>>
FreeSwitch中的会议功能(4)
查看>>
MySQL中创建用户分配权限(到指定数据库或者指定数据库表中)
查看>>
AutoReleasePool 和 ARC 以及Garbage Collection
查看>>
重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础
查看>>
乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
查看>>
MVP Community Camp 社区大课堂
查看>>
GWT用frame调用JSP
查看>>
大型高性能ASP.NET系统架构设计
查看>>
insert select带来的问题
查看>>
EasyUI 添加tab页(iframe方式)
查看>>
mysqldump主要参数探究
查看>>