设计模式(十九)责任链模式

版权声明:转载必须注明本文转自晓_晨的博客:http://blog.csdn.net/niunai112

目录

导航

设计模式之六大设计原则
设计模式(一)单例模式
设计模式(二)工厂模式
设计模式(三)策略模式
设计模式(四)适配器模式
设计模式(五)享元模式
设计模式(六)建造者模式
设计模式(七)原型模式
设计模式(八)桥接模式
设计模式(九)外观模式
设计模式(十)组合模式
设计模式(十一)装饰器模式
设计模式(十二)代理模式
设计模式(十三)迭代器模式
设计模式(十四)观察者模式
设计模式(十五)中介者模式
设计模式(十六)命令模式
设计模式(十七)状态模式
设计模式(十八)访问者模式
设计模式(十九)责任链模式
设计模式(二十)解释器模式
设计模式(二十一)备忘录模式
设计模式(二十二)模板模式
设计模式总结篇(为什么要学习设计模式,学习设计模式的好处)

前言

责任链模式:顾名思义,就是创建了一个的对象链来处理对象。这个模式对请求的发送者和接收者进行解耦。
处理抽象类(Handler):处理类的抽象父类,有指向下一个责任链成员的字段。
具体处理类(concreteHandler):具体的处理类,实现具体处理逻辑。
有很多地方需要用到责任链,一种是按等级来划分该谁来处理,比如你到店里吃饭,你发现桌子没有擦,所以你可以叫服务员来解决这个问题,不需要麻烦到经理和老板级别,但是假如你要投诉服务员,那么就得经理来处理这件事了,服务员是能力来处理的。这样实现的责任链,当任务处理完成后就结束了。
另一种实现方式是,每一个节点对象都有各自的功能,每一次请求都要走完全部的节点,我们的Filter便是这种类型的责任链,你可能有loginFilter来判断是否登录,encodeFilter来解决编码问题……等等

例子

LZ这里用服务员的场景来做例子

/***
 *
 *@Author ChenjunWang
 *@Description:处理抽象类
 *@Date: Created in 15:08 2018/4/16
 *@Modified By:
 *
 */
public abstract class Handler{

    private Handler next;//下一个处理的对象是谁

    //设置下一个处理对象,因为每一个处理对象都需要的方法,所以作为抽象类里的函数
    public void setNext(Handler next) {
        this.next = next;
    }
    public void next(Complain complain){
        next.handleComplain(complain);
    }

    abstract void handleComplain(Complain complain);
}


/***
 *
 *@Author ChenjunWang
 *@Description:具体处理服务员
 *@Date: Created in 15:08 2018/4/16
 *@Modified By:
 *
 */
public class Waiter extends Handler {
    @Override
    public void handleComplain(Complain complain) {

        if (complain.getLevel() == 3){
            System.out.println("3类投诉,服务员能处理");
        } else {
            System.out.println("服务员处理不了");
            next(complain);
        }
    }
}
/***
 *
 *@Author ChenjunWang
 *@Description:具体处理对象经理
 *@Date: Created in 15:09 2018/4/16
 *@Modified By:
 *
 */
public class Manager extends Handler {
    @Override
    public void handleComplain(Complain complain) {

        if (complain.getLevel() == 2){
            System.out.println("2类投诉,经理能处理");
        } else {
            System.out.println("经理处理不了");
            next(complain);
        }
    }


}

/***
 *
 *@Author ChenjunWang
 *@Description:具体处理对象老板
 *@Date: Created in 15:09 2018/4/16
 *@Modified By:
 *
 */
public class Boss extends Handler {
    @Override
    public void handleComplain(Complain complain) {

        if (complain.getLevel() == 1){
            System.out.println("1类投诉,老板能处理");
        } else {

            System.out.println("老板处理不了");
            System.out.println("无人能处理");
        }
    }
}



/***
 *
 *@Author ChenjunWang
 *@Description:要被处理的对象,这里是投诉类
 *@Date: Created in 17:38 2018/4/16
 *@Modified By:
 *
 */
public class Complain {
    private int level;//投诉的等级,处理对象靠这个字段来判断是否能处理
    private String content;
    public Complain(int level, String content){
        this.level = level;
        this.content = content;
    }

    public int getLevel() {
        return level;
    }

    public String getContent() {
        return content;
    }
}

/***
 *
 *@Author ChenjunWang
 *@Description:测试类
 *@Date: Created in 22:18 2018/4/16
 *@Modified By:
 *
 */
public class Test {
    public static void main(String[] args) {
        Boss boss = new Boss();
        Manager manager = new Manager();
        Waiter waiter = new Waiter();
        waiter.setNext(manager);
        manager.setNext(boss);

        System.out.println("-----------------三类投诉,桌子不干净-----------------");
        Complain complain = new Complain(3, "桌子不干净");
        waiter.handleComplain(complain);


        System.out.println("-----------------二类投诉,服务员态度不好-----------------");
        complain = new Complain(2, "我要投诉服务员");
        waiter.handleComplain(complain);
    }

}

运行结果如下
------------------------------------
-----------------三类投诉,桌子不干净-----------------
3类投诉,服务员能处理
-----------------二类投诉,服务员态度不好-----------------
服务员处理不了
2类投诉,经理能处理

责任往下,一层层传递,这就是责任链模式所起到的作用。非常的简单易懂。
filter的例子就不详细列举了
关键的地方是,先注册过滤器,即把filter假如责任链,设置Order可以改变责任链里执行的顺序,然后doFilter(request, response, chain);把请求传到下一个filter里进行处理。

总结

优点

(1)降低耦合度,它将请求的发送者和接收者解耦。
(2)对于使用者来说,责任链是黑箱,只需要调用第一个方法,后续任务,责任链会帮你解决。
(3)可以根据需要自由责任链内部结构。

缺点

(1)因为处理时以链的形式在对象间传递消息,递归调用会降低效率。

Git地址

本篇实例Github地址:https://github.com/stackisok/Design-Pattern/tree/master/src/chainOfResponsibility

回到最上方


有什么不懂或者不对的地方,欢迎留言。
喜欢LZ文章的小伙伴们,可以关注一波,也可以留言,LZ会回你们的。
觉得写得不错的小伙伴,欢迎转载,但请附上原文地址,谢谢^_^!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值