责任链模式

1、行为型模式概述

2、责任链模式的动机

3、责任链模式的定义

避免请求和处理耦合在一起,将多个处理对象串联起来,并沿着这条线传递请求,直到请求处理完成返回为止;     

4、责任链模式的类结构图

image

模式中包含三个角色:抽象处理类(接口或者抽象类),具体处理类,客户端;

5、 优缺点和适用场景

优点:

缺点:

适用场景:

6、实例

6.1

例1:餐费申请,先通过项目组长审批,若项目组长没权限,则由经理审批;
说明:改责任链的通过上家对下家的引用进行串联起来的,即依赖对象间的聚合;
a、定义接口

public abstract class Handler {
    private Handler success;

    public Handler getSuccess() {
        return success;
    }

    public void setSuccess(Handler success) {
        this.success = success;
    }
    public abstract String handle(String user, double fee);
}

b、实现具体处理类

public class ProjectHandler extends Handler {

    public String handle(String user, double fee) {
        if (fee < 500) {
            if ("张三".equals(user)) {
                return "同意" + user + "报销餐费" + fee;
            } else {
                return "不统一同意" + user + "报销餐费" + fee;
            }
        }
        if (getSuccess() != null) {
            return getSuccess().handle(user, fee);
        }
        return null;
    }
}
public class ManageHandler extends Handler {
    public String handle(String user, double fee) {
        if (fee > 500) {
            if ("李四".equals(user)) {
                return "同意" + user + "报销餐费" + fee;
            } else {
                return "不统一同意" + user + "报销餐费" + fee;
            }
        }
        if (getSuccess() != null) {
            return getSuccess().handle(user, fee);
        }
        return null;
    }
}

c、客户端调用

public class TestHandler {

    public static void main(String[] args) {
        Handler projectHandler = new ProjectHandler();
        Handler manageHandler = new ManageHandler();
        projectHandler.setSuccess(manageHandler);
        String msg = projectHandler.handle("李四", 400);
        System.out.println(msg);
    }
}

例2:由多个对象过滤掉消息中的铭感字符串
注意:该责任链主要通过对象间的依赖进行串联起来的, 强烈建议使用这种方法进行串联

a、定义接口

public interface Filter {
    String doFilter(String msg, ChainFilter chainFilter);
}

b、实现具体处理类

public class HTMLFilter implements Filter {

    public String doFilter(String msg, ChainFilter chainFilter) {
        System.out.println("HTMLFilter.doFilter");
        msg = msg.replace("zy", "张三");
        return chainFilter.doFilter(msg,chainFilter);
    }
}
public class MessageFilter implements Filter {

    public String doFilter(String msg, ChainFilter chainFilter) {
        System.out.println("HTMLFilter.doFilter");
        msg=msg.replace("mm", "李四");
        return chainFilter.doFilter(msg,chainFilter);
    }
}

c、实例串联类

public class ChainFilter implements Filter {

    private List<Filter> chainFilter=new ArrayList<Filter>();
    private int index;

    public String doFilter(String msg, ChainFilter chainFilter) {
        if (index == this.chainFilter.size()) {
            return msg;
        }

        Filter filter1 = this.chainFilter.get(index);
        index++;
        return filter1.doFilter(msg,chainFilter);
    }

    public void addFilter(Filter filter) {
        chainFilter.add(filter);
    }

}

d、客户端的调用

public class Client {

    public static void main(String[] args) {

        String msg="zymm";
        ChainFilter chainFilter = new ChainFilter();
        chainFilter.addFilter(new HTMLFilter());
        chainFilter.addFilter(new MessageFilter());
        msg=chainFilter.doFilter(msg,chainFilter);

        System.out.println(msg);

    }
}

以上串联方式主要通过ChainFilter进行处理类的串联,取出一个处理对象,处理对象处理请求,然后在到串联对象中,由串联对象再取 一个处理对象,处理请求,依次进行下去;