美文网首页
设计模式-责任链模式

设计模式-责任链模式

作者: MarkShen | 来源:发表于2018-03-01 22:13 被阅读0次

    话题引入

    • 在做一个BBS系统,这些信息里面是否包含脚本语言,敏感信息等,我们要把这些内容替换掉或者删掉。所以,要录入到数据库的信心都要,都要对其进行过滤。对于敏感信息的处理,我的做法是在网上找到一些敏感信息并建立数据库,然后以这个数据库为准,过滤掉敏感信息。
    • 补充:去网上查一查极限编程资料。
    package com.alan.chain.m1;
    
    public class MessageProcessor {
        private String msg;
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
        // 现在的处理是,大而全的处理方法,违反了设计模式6原则中的单一职责原则。
        public String process() {
            // 返回处理后的字符串, 链式编程
            String r = msg.replace("<", "[")
                          .replace(">", "]");
            // 处理敏感字眼
            r = r.replace("被就业", "就业")
                 .replace("敏感", "");
            // ...
            return r;
        }
    }
    
    package com.alan.chain.m1;
    
    public class Client {
        public static void main(String[] args) {
            String msg = "大家好:),<script>, 敏感,被就业,网络授课没感觉,因为看不见大伙儿";
            MessageProcessor mp = new MessageProcessor();
            mp.setMsg(msg);
            String result = mp.process();
            System.out.println(result);
        }
    }
    
    • 上例子程序中的处理方式,大而全,即不满足设计模式6原则中的单一职责原则,又不满足开闭原则。那么如何解决这个问题,将耦合度高的内容变成耦合度较低的代码呢?(对消息进行过滤,而且过滤规则要进行动态的指定)由此引出下面的程序。
    package com.alan.chain.m2;
    
    public interface Filter {
        // 对字符串进行过滤
        String doFilter(String msg);
    }
    
    package com.alan.chain.m2;
    
    public class HTMLFilter implements Filter {
        public String doFilter(String msg) {
            return msg.replace("<", "[")
                      .replace(">", "]");
        }
    }
    
    package com.alan.chain.m2;
    
    public class SensitiveFilter implements Filter {
        public String doFilter(String msg) {
            return msg.replace("被就业", "就业")
                      .replace("敏感", "");
        }
    }
    
    package com.alan.chain.m1;
    
    import com.alan.chain.m2.Filter;
    import com.alan.chain.m2.HTMLFilter;
    import com.alan.chain.m2.SensitiveFilter;
    
    public class MessageProcessor {
        private String msg;
        private Filter[] filters = {new HTMLFilter(), new SensitiveFilter()};
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public String process() {
            // 返回处理后的字符串, 链式编程
            String r = msg;
            for (Filter f : filters) {
                r = f.doFilter(r);
            }
            return r;
        }
    }
    
    package com.alan.chain.m1;
    
    public class Client {
        public static void main(String[] args) {
            String msg = "大家好:),<script>, 敏感,被就业,网络授课没感觉,因为看不见大伙儿";
            MessageProcessor mp = new MessageProcessor();
            mp.setMsg(msg);
            String result = mp.process();
            System.out.println(result);
        }
    }
    
    • 这样做可以进行规则的排序,新的规则可以做扩展,如果哪个信息不需要了,直接给删掉就可以了。下一个版本要解决的问题就是,两个链条之间如何组合。
    package com.alan.chain.m3;
    
    public interface Filter {
        // 对字符串进行过滤
        String doFilter(String msg);
    }
    
    package com.alan.chain.m3;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class FilterChain implements Filter {
        private List<Filter> filters = new ArrayList<Filter>();
        
        public FilterChain addFilter(Filter filter) {
            // 将过滤器加入FilterChain中,将FilterChain返回
            this.filters.add(filter);
            return this;
        }
        
        public String doFilter(String msg) {
            String result = msg;
            for (Filter f : filters) {
                result = f.doFilter(result);
            }
            return result;
        }
    }
    
    package com.alan.chain.m3;
    
    public class HTMLFilter implements Filter {
        public String doFilter(String msg) {
            return msg.replace("<", "[")
                      .replace(">", "]");
        }
    }
    
    package com.alan.chain.m3;
    
    public class SensitiveFilter implements Filter {
        public String doFilter(String msg) {
            return msg.replace("被就业", "就业")
                      .replace("敏感", "");
        }
    }
    
    package com.alan.chain.m3;
    
    public class MessageProcessor {
        private String msg;
        private FilterChain fc;
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public FilterChain getFc() {
            return fc;
        }
    
        public void setFc(FilterChain fc) {
            this.fc = fc;
        }
    
        public String process() {
            return fc.doFilter(msg);
        }
    }
    package com.alan.chain.m3;
    
    public class Client {
        public static void main(String[] args) {
            String msg = "大家好:),<script>, 敏感,被就业,网络授课没感觉,因为看不见大伙儿";
            MessageProcessor mp = new MessageProcessor();
            FilterChain filterChain = new FilterChain();
            filterChain.addFilter(new HTMLFilter())
                       .addFilter(new SensitiveFilter());
            
            FilterChain filterChain2 = new FilterChain();
            filterChain2.addFilter(filterChain);
            
            // 用Filter处理Msg
            mp.setFc(filterChain2);
            mp.setMsg(msg);
            mp.process();
            String result = filterChain.doFilter(msg);
            System.out.println(result);
        }
    }
    
    • 上面的代码中的MessageProcessor类可以删掉。将其内容整合到含有Main方法的类中。
    package com.alan.chain.m4.web;
    
    public interface Filter {
        void doFilter(Request request, Response response);
    }
    
    package com.alan.chain.m4.web;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class FilterChain implements Filter {
        private List<Filter> filters = new ArrayList<Filter>();
        
        public FilterChain addFilter(Filter filter) {
            // 将过滤器加入FilterChain中,将FilterChain返回
            this.filters.add(filter);
            return this;
        }
        
        public void doFilter(Request request, Response response) {
            for (Filter f : filters) {
                f.doFilter(request, response);
            }
        }
    }
    
    package com.alan.chain.m4.web;
    
    public class HTMLFilter implements Filter {
        public void doFilter(Request request, Response response) {
            request.requestStr = request.requestStr.replace("<", "[")
                                        .replace(">", "]") + "---HTMLFilter";
            
            response.responseStr = response.responseStr + "---HTMLFilter";
        }
    }
    
    package com.alan.chain.m4.web;
    
    public class SensitiveFilter implements Filter {
        public void doFilter(Request request, Response response) {
            request.requestStr = request.requestStr.replace("被就业", "就业")
                                        .replace("敏感", "") + "---SensitiveFilter";
            
            response.responseStr = response.responseStr + "---SensitiveFilter";
        }
    }
    
    package com.alan.chain.m4.web;
    
    public class Request {
        String requestStr;
    
        public String getRequestStr() {
            return requestStr;
        }
    
        public void setRequestStr(String requestStr) {
            this.requestStr = requestStr;
        }
    
    }
    
    package com.alan.chain.m4.web;
    
    public class Response {
        String responseStr;
    
        public String getResponseStr() {
            return responseStr;
        }
    
        public void setResponseStr(String responseStr) {
            this.responseStr = responseStr;
        }
    
    }
    
    package com.alan.chain.m4.web;
    
    public class Client {
        public static void main(String[] args) {
            String msg = "大家好:),<script>, 敏感,被就业,网络授课没感觉,因为看不见大伙儿";
            Request request = new Request();
            request.setRequestStr(msg);
            
            Response response = new Response();
            response.setResponseStr("response");
            
            FilterChain fc = new FilterChain();
            fc.addFilter(new HTMLFilter())
              .addFilter(new SensitiveFilter());
            
            fc.doFilter(request, response);
            System.out.println(request.getRequestStr());
            System.out.println(response.getResponseStr());
        }
    }
    
    • 学习的捷径:先学脉络,后学细节
    • 期望效果是对Request, Response进行处理,Request是正序,Response是倒序,上面例子中的效果是都是正序。那么如何来解决这个问题呢?请看下面的代码。
    package com.alan.chain.m5.web;
    
    public interface Filter {
        void doFilter(Request request, Response response, FilterChain chain);
    }
    
    package com.alan.chain.m5.web;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class FilterChain implements Filter {
        private List<Filter> filters = new ArrayList<Filter>();
        int index = 0;
        
        public FilterChain addFilter(Filter filter) {
            // 将过滤器加入FilterChain中,将FilterChain返回
            this.filters.add(filter);
            return this;
        }
        
        public void doFilter(Request request, Response response, FilterChain chain) {
            if (index == filters.size())
                return;
            Filter f = filters.get(index ++);
            f.doFilter(request, response, chain);
        }
    }
    
    package com.alan.chain.m5.web;
    
    public class HTMLFilter implements Filter {
        public void doFilter(Request request, Response response, FilterChain chain) {
            request.requestStr = request.requestStr.replace("<", "[")
                                        .replace(">", "]") + "---HTMLFilter";
            chain.doFilter(request, response, chain);
            response.responseStr = response.responseStr + "---HTMLFilter";
        }
    }
    
    package com.alan.chain.m5.web;
    
    public class SensitiveFilter implements Filter {
        public void doFilter(Request request, Response response, FilterChain chain) {
            request.requestStr = request.requestStr.replace("被就业", "就业")
                                        .replace("敏感", "") + "---SensitiveFilter";
            chain.doFilter(request, response, chain);
            response.responseStr = response.responseStr + "---SensitiveFilter";
        }
    }
    
    package com.alan.chain.m5.web;
    
    public class Request {
        String requestStr;
    
        public String getRequestStr() {
            return requestStr;
        }
    
        public void setRequestStr(String requestStr) {
            this.requestStr = requestStr;
        }
    
    }
    
    package com.alan.chain.m5.web;
    
    public class Response {
        String responseStr;
    
        public String getResponseStr() {
            return responseStr;
        }
    
        public void setResponseStr(String responseStr) {
            this.responseStr = responseStr;
        }
    
    }
    
    package com.alan.chain.m5.web;
    
    public class Client {
        public static void main(String[] args) {
            String msg = "大家好:),<script>, 敏感,被就业,网络授课没感觉,因为看不见大伙儿";
            Request request = new Request();
            request.setRequestStr(msg);
            
            Response response = new Response();
            response.setResponseStr("response");
            
            FilterChain fc = new FilterChain();
            fc.addFilter(new HTMLFilter())
              .addFilter(new SensitiveFilter());
            
            fc.doFilter(request, response, fc);
            System.out.println(request.getRequestStr());
            System.out.println(response.getResponseStr());
        }
    }
    
    • 此时,如果有一个Request请求过来了,发现它不合乎条件,不合法,一发现它,停掉后续的处理不处理了。怎么做呢?
    • 学习框架,先应用,然后再学习原理。学习框架,明白其思想。
    • 应用驱动学习。
    • 学习思想,学习设计方面的能力。
    • 站在整体项目上的一个把握。少想一点,多做一点。

    相关文章

      网友评论

          本文标题:设计模式-责任链模式

          本文链接:https://www.haomeiwen.com/subject/jvenxftx.html