欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java设计模式---ChainOfResponsibility责任链模式

程序员文章站 2022-07-05 16:04:58
参考于 : 大话设计模式 马士兵设计模式视频 代码参考于马士兵设计模式视频 写在开头:职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系 图来自大话设计模式,下面我的代码中,Clien是t依赖于Handler1和Handler2的,不过可以使用配置文件或者直接给Fil ......

参考于 :

  大话设计模式

  马士兵设计模式视频

  代码参考于马士兵设计模式视频

  写在开头:职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系

  图来自大话设计模式,下面我的代码中,clien是t依赖于handler1和handler2的,不过可以使用配置文件或者直接给filter集合初始化来解决这种依赖。

  Java设计模式---ChainOfResponsibility责任链模式

 

1.场景

  在网上评论一句话,往往会经过一系列的处理,有没有敏感词之类,现在我们来模拟一下。

  把<html>,o.o,和谐

  换成 [html],^v^,/*河蟹*/

2.不使用职责链模式

  

package com.dingyu.chainofresponsibility;

/**
 * 重点在于模式,故逻辑暂时不考虑
 * 
 * @author dingyu
 *
 */
public class client01 {
    public static void main(string[] args) {
        string s = "<html>,o.o,和谐";
        s = s.replace("<", "[").replace(">", "]").replace("和谐", "/*河蟹*/").replace("o.o", "^v^");
        system.out.println(s);
    }
}

  缺点:过滤无法复用,无法记录过滤顺序。

3.使用职责链模式

  

package com.dingyu.chainofresponsibility;
/**
 * 过滤器,dofilter处理msg字符串
 * @author dingyu
 *
 */
public interface filter {
    public string dofilter(string msg);
}
package com.dingyu.chainofresponsibility;

/**
 * 处理html符号
 * 测试用的,逻辑不是很严谨
 * @author dingyu
 *
 */
public class htmlfilter implements filter {

    @override
    public string dofilter(string msg) {
        return msg.replace("<", "[").replace(">", "]");
    }

}
package com.dingyu.chainofresponsibility;
/**
 * 过滤敏感词汇
 * @author dingyu
 *
 */
public class sensitivewordfilter implements filter {

    @override
    public string dofilter(string msg) {
        return msg.replace("和谐", "/*河蟹*/");
    }

}
package com.dingyu.chainofresponsibility;
/**
 * 处理符号
 * @author dingyu
 *
 */
public class symbolfilter implements filter {

    @override
    public string dofilter(string msg) {
        return msg.replace("o.o", "^v^");
    }

}
package com.dingyu.chainofresponsibility;

import java.util.arraylist;
import java.util.list;

public class client {
    private static list<filter> filters = new arraylist<filter>();
    private static int index = 0;

    public static void main(string[] args) {
        string s = "<html>,o.o,和谐";
        filters.add(new htmlfilter());
        filters.add(new symbolfilter());
        filters.add(new sensitivewordfilter());
        for (filter filter : filters) {
            s = filter.dofilter(s);
        }
        system.out.println(s);
    }
}

  虽然使用职责链模式,但仍然有缺点,上面的顺序是先html,然后symbol,最后sensitiveword,这三个构成一个过滤链条,如果有一天我需要把另一个过滤链条插入到这根链条中间,实现起来很麻烦。

4.改进职责链模式

  

package com.dingyu.chainofresponsibility;

import java.util.arraylist;
import java.util.list;
/**
 * 
 * @author dingyu
 *
 */
public class filterchain implements filter {

    private list<filter> filters = new arraylist<filter>();

    public void add(filter filter) {
        filters.add(filter);
    }

    public list<filter> getfilters() {
        return filters;
    }

    @override
    public string dofilter(string msg) {
        for (filter filter : filters) {
            msg = filter.dofilter(msg);
        }
        return msg;
    }

}
package com.dingyu.chainofresponsibility;

import java.util.arraylist;
import java.util.list;

public class client {
//    private static list<filter> filters = new arraylist<filter>();

    public static void main(string[] args) {
        string s = "<html>,o.o,和谐";
        // 链条1
        filterchain chain1 = new filterchain();
        chain1.add(new htmlfilter());
        // 链条2
        filterchain chain2 = new filterchain();
        chain2.add(new htmlfilter());
        chain2.add(new sensitivewordfilter());
        // 链条2插到链条1后
        chain1.add(chain2);
        chain1.add(new symbolfilter());
        s = chain1.dofilter(s);
        system.out.println(s);
        // filters.add(new htmlfilter());
        // filters.add(new symbolfilter());
        // filters.add(new sensitivewordfilter());
        // for (filter filter : filters) {
        // s = filter.dofilter(s);
        // }
        // system.out.println(s);
    }
}

  建议debug , eclipse快捷键

Java设计模式---ChainOfResponsibility责任链模式

5. 职责链实现双向过滤  

  思路类似于递归,建议使用debug一步步调试。

Java设计模式---ChainOfResponsibility责任链模式

package com.dingyu.chainofresponsibility;

/**
 * 过滤器,dofilter处理msg字符串
 * 
 * @author dingyu
 *
 */
public interface filter {
    // public string dofilter(string msg);
    public void dofilter(request request, reponse reponse, filterchain filterchain);
}
package com.dingyu.chainofresponsibility;

/**
 * 处理html符号 测试用的,逻辑不是很严谨
 * 
 * @author dingyu
 *
 */
public class htmlfilter implements filter {

    @override
    public void dofilter(request request, reponse reponse, filterchain filterchain) {
        request.getrequestmsg().replace("<", "[").replace(">", "]");
        filterchain.dofilter(request, reponse, filterchain);
        reponse.setreponsemsg(reponse.getreponsemsg() + "-----htmlfilter");
    }

    // @override
    // public string dofilter(string msg) {
    // return msg.replace("<", "[").replace(">", "]");
    // }

}
package com.dingyu.chainofresponsibility;

/**
 * 过滤敏感词汇
 * 
 * @author dingyu
 *
 */
public class sensitivewordfilter implements filter {

    @override
    public void dofilter(request request, reponse reponse, filterchain filterchain) {
        request.getrequestmsg().replace("和谐", "/*河蟹*/");
        filterchain.dofilter(request, reponse, filterchain);
        reponse.setreponsemsg(reponse.getreponsemsg() + "-----sensitivewordfilter");
    }

    // @override
    // public string dofilter(string msg) {
    // return msg.replace("和谐", "/*河蟹*/");
    // }

}
package com.dingyu.chainofresponsibility;

/**
 * 处理符号
 * 
 * @author dingyu
 *
 */
public class symbolfilter implements filter {

    @override
    public void dofilter(request request, reponse reponse, filterchain filterchain) {
        request.getrequestmsg().replace("o.o", "^v^");
        filterchain.dofilter(request, reponse, filterchain);
        reponse.setreponsemsg(reponse.getreponsemsg() + "-----symbolfilter");
    }

    // @override
    // public string dofilter(string msg) {
    // return msg.replace("o.o", "^v^");
    // }

}
package com.dingyu.chainofresponsibility;

import java.util.arraylist;
import java.util.list;

/**
 * 
 * @author dingyu
 *
 */
public class filterchain implements filter {

    private list<filter> filters = new arraylist<filter>();
    private int index=-1;

    public void add(filter filter) {
        filters.add(filter);
    }

    public list<filter> getfilters() {
        return filters;
    }

    // @override
    // public string dofilter(string msg) {
    // for (filter filter : filters) {
    // msg = filter.dofilter(msg);
    // }
    // return msg;
    // }

    @override
    public void dofilter(request request, reponse reponse, filterchain filterchain) {
        if (index == filters.size()-1)
            return;
        index++;
        filters.get(index).dofilter(request, reponse, filterchain);
        
    }

}
package com.dingyu.chainofresponsibility;

public class client03 {
    public static void main(string[] args) {
        request request = new request();
        reponse reponse = new reponse();
        request.setrequestmsg("<html>,o.o,和谐");
        reponse.setreponsemsg("hahahahaha");
        filterchain chain = new filterchain();
        chain.add(new htmlfilter());
        chain.add(new sensitivewordfilter());
        chain.add(new symbolfilter());
        chain.dofilter(request, reponse, chain);
        system.out.println(request.getrequestmsg());
        system.out.println(reponse.getreponsemsg());
    }
}