设计模式-责任链模式

2018-03-01  本文已影响0人  MarkShen

话题引入

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);
    }
}
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);
    }
}
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());
    }
}
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());
    }
}
上一篇下一篇

猜你喜欢

热点阅读