Java猿社区—设计模式之责任链模式—敏感消息过滤链设计

前言

想要做网页Web聊天系统,但是如何对消息特殊字符以及敏感词进行过滤、以及处理呢?
请见下方。

敏感消息过滤链设计

在这里插入图片描述

代码

不啰嗦直接上源码:

package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter;

/**
 * @author zhouzhixiang
 * @company 叮当快药科技集团有限公司
 * @Date 2019-11-02
 */
public interface Filter {

    String doFilter(String message);
}

package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 过滤器链
 * @author zhouzhixiang
 * @company 叮当快药科技集团有限公司
 * @Date 2019-11-03
 */
public class FilterChain implements Filter {
    private List filters = new ArrayList<>();

    public FilterChain addFilter(Filter filter) {
        filters.add(filter);
        return this;
    }

    public String doFilter(String message) {
        AtomicReference s = new AtomicReference<>(message);
        filters.forEach(f -> s.set(f.doFilter(s.get())));
        return s.get();
    }
}

package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter;

/**
 * 表情包过滤器
 * @author zhouzhixiang
 * @company 叮当快药科技集团有限公司
 * @Date 2019-11-03
 */
public class FaceFilter implements Filter {
    @Override
    public String doFilter(String message) {
        return message.replace("@", "^V^").replace("&", "^V^");
    }
}

package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter;

/**
 * Html内容过滤器
 * @author zhouzhixiang
 * @company 叮当快药科技集团有限公司
 * @Date 2019-11-02
 */
public class HtmlFilter implements Filter {
    @Override
    public String doFilter(String message) {
        return message.replace("<", "[").replace("/>", "]");
    }
}

package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter;

/**
 * 敏感词过滤器
 * @author zhouzhixiang
 * @company 叮当快药科技集团有限公司
 * @Date 2019-11-02
 */
public class SensitiveFilter implements Filter {
    @Override
    public String doFilter(String message) {
        return message.replace("王八蛋", "*").replace("软蛋", "*").replace("滚犊子", "*");
    }
}

package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain;

import com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter.FilterChain;
import lombok.AllArgsConstructor;

/**
 * 信息处理器
 * @author zhouzhixiang
 * @company 叮当快药科技集团有限公司
 * @Date 2019-11-02
 */
@AllArgsConstructor
public class MessageProsessor {
    private String message;

    private FilterChain filterChain;

    public String process() {

        return filterChain.doFilter(message);
    }
}

测试类:

package com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain;

import com.zzx.design.pattern.zzxdesignpattern.mashibin.zerenlian_pattern.sensitive_filter_chain.filter.*;

/**
 * 敏感信息过滤-》责任链设计模式
 * @author zhouzhixiang
 * @company 叮当快药科技集团有限公司
 * @Date 2019-11-02
 */
public class Main {

    public static void main(String[] args) {
        String msg = "你好,软蛋,@,
                    
                    

你可能感兴趣的:(Java猿社区—设计模式之责任链模式—敏感消息过滤链设计)