菜单

过滤器模式

2018年11月19日 - jQuery

    

    class Regulars
    {
        public $RegularID;
        public $RegularName;
        public $AnalysisConditons;

        public function __construct($RegularID, $RegularName, $AnalysisConditons)
        {
            $this->RegularID = $RegularID;
            $this->RegularName = $RegularName;
            $this->AnalysisConditons = $AnalysisConditons;
        }

        public function getRegularID()
        {
            $this->RegularID;
        }

        public function setRegularID($RegularID)
        {
            $this->RegularID = $RegularID;
        }

        public function getRegularName()
        {
            $this->RegularName;
        }

        public function setRegularName($RegularName)
        {
            $this->RegularName = $RegularName;
        }

        public function getAnalysisConditons()
        {
            $this->AnalysisConditons;
        }

        public function setAnalysisConditons($AnalysisConditons)
        {
            $this->AnalysisConditons = $AnalysisConditons;
        }
    }




    interface IFilter
    {
        public function filter(array $regulars);
    }



    class RegularIDFilter implements IFilter
    {
        public function filter(array $regularsList)
        {
            //过滤规则
            return $regularsList;
        }
    }

    class RegularNameFilter implements IFilter
    {
        public function filter(array $regularsList)
        {
            return $regularsList;
        }
    }

        class RegularCondtionFilter implements IFilter
    {
        public function filter(array $regularsList)
        {
            return $regularsList;
        }
    }

    class AndFilter implements IFilter
    {
        private $filters = [];

        public function AndFilter(array $filters)
        {
            $this->filters = $filters;
        }

        public function Filter(array $regularlist)
        {
            foreach ($filters as $filter) {
                $regularlist = $filter->Filter($regularlist);
            }

            return $regularlist;
        }
    }


    class OrFilter implements IFilter
    {
        private $filters = [];

        public function OrFilter(array $filters)
        {
            $this->filters = $filters;
        }

        public function Filter(array $regularlist)
        {
            $context = [];
            foreach ($filters as $filter) {
                $regularlist = $filter->Filter($regularlist);
                foreach ($regularlist as $regular){
                    $context[] = $regular;
                }
            }
            //去重复返回
            return array_flip(array_flip($context));
        }
    }


    $regulars = [];
    $regulars[] = new Regulars(1, "规则1", "xxxx");
    $regulars[] = new Regulars(1, "规则2", "xxxx");
    $regulars[] = new Regulars(1, "规则3", "xxxx");
    $filterList = [];
    $filterList[] = new RegularIDFilter();
    $filterList[] = new RegularNameFilter();
    $filterList[] = new RegularCondtionFilter();
    $andCriteria = new AndFilter($filterList);
    $andCriteria->Filter($regulars);

  过滤器模式,顾名思义,就是过滤对象用的,对得过滤的目标,进行局部征,或者加某些特定信息,或者去信息都得。如果你想定义多单过滤规则,那么就用定义多独过滤器,一般每个过滤器只兑现平等种规则。然后多单过滤器会连在一起,形成一个过滤器链,想想咱们的servlet的过滤器,其实就是是如此的,下面我就算冲servlet过滤器实现方式,来效仿一下过滤器的模式:

参考文章
http://www.cnblogs.com/huangxincheng/p/6415393.html

  1、首先定义两个待过滤的目标,

  Request:

package com.hd.filter;

public class Request {

    private String requestStr;

    public Request(String requestStr) {
        this.requestStr = requestStr;
    }

    public String getRequestStr() {
        return requestStr;
    }

    public void setRequestStr(String requestStr) {
        this.requestStr = requestStr;
    }

    public void addStr(String str){
        this.requestStr += str;
    }
}

  Response:

package com.hd.filter;

public class Response {

    private String ResponseStr;

    public Response(String responseStr) {
        ResponseStr = responseStr;
    }

    public String getResponseStr() {
        return ResponseStr;
    }

    public void setResponseStr(String responseStr) {
        ResponseStr = responseStr;
    }

    public void addStr(String str){
        this.ResponseStr += str;
    }
}

 

  2、然后要定义一个filter接口:

package com.hd.filter;

public interface Filter {

    void doFilter(Request request, Response response, FilterChain filterChain);
}

  

  3、接着我们再次定义一个过滤器链FilterChain,用来将大半单过滤器有序的链条起来:

package com.hd.filter;

import java.util.ArrayList;
import java.util.List;

public class FilterChain {

    private List<Filter> filterChains = new ArrayList<Filter>();

    public static FilterChain build(){
        return new FilterChain();
    }

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

    public FilterChain removeFilter(Filter filter){
        this.filterChains.remove(filter);
        return this;
    }

}

 

  4、下面就定义具体的filter实现了:

package com.hd.filter;

public class HTMLFilter implements Filter {

    @Override
    public void doFilter(Request request, Response response, FilterChain filterChain) {

        System.out.println("HTMLFilter Request");
        request.addStr("-HTML-");
        filterChain.doFilter(request, response);    //这里是第一个重点的地方,只有这样设计,request才可以按照正序执行过滤,response按照倒序执行过滤
        response.addStr("-HTML-");
        System.out.println("HTMLFilter Response");
    }
}

 

package com.hd.filter;

public class SensitiveFilter implements Filter {

    @Override
    public void doFilter(Request request, Response response, FilterChain filterChain) {

        System.out.println("SensitiveFilter Request");
        request.addStr("-Sensitive-");
        filterChain.doFilter(request, response);
        response.addStr("-Sensitive-");
        System.out.println("SensitiveFilter Response");
    }
}

 

package com.hd.filter;

public class LowerUpcaseFilter implements Filter {

    @Override
    public void doFilter(Request request, Response response, FilterChain filterChain) {

        System.out.println("LowerUpcaseFilter Request");
        request.addStr("-LowerUpcase-");
        filterChain.doFilter(request, response);
        response.addStr("-LowerUpcase-");
        System.out.println("LowerUpcaseFilter Response");
    }
}

 

  5、下面我们尚不一一个重点的触及,就是怎拿有的过滤器串起来,还按顺序为生实施。如果你来通过思考后底说话,不难想到该是在FilterChain的
doFilter 方法里举行文章:

    private int index =0;

    public void doFilter(Request request, Response response){

        if(index < filterChains.size()){
            filterChains.get(index++).doFilter(request, response, this);
        }else{
            return;
        }

    }

  定义一个变量,用来号当前需处理的凡谁过滤器,这是第二只基本点。

  6、最后写下测试代码:

package com.hd.filter;

public class TestFilter {

    public static void main(String[] args){

        FilterChain filterChain = FilterChain.build();
        filterChain.addFilter(new HTMLFilter());
        filterChain.addFilter(new SensitiveFilter());
        filterChain.addFilter(new LowerUpcaseFilter());

        Request request = new Request("request");
        Response response = new Response("response");
        filterChain.doFilter(request, response);

        System.out.println(request.getRequestStr());
        System.out.println(response.getResponseStr());
    }
}

 

  运行结果如下:

HTMLFilter Request
SensitiveFilter Request
LowerUpcaseFilter Request
LowerUpcaseFilter Response
SensitiveFilter Response
HTMLFilter Response
request-HTML--Sensitive--LowerUpcase-
response-LowerUpcase--Sensitive--HTML-

 

  如果您勾勒了递归算法的话,应该会与自发共鸣,感觉过滤器有点像递归算法,一稀有的为生套,然后实施到最终一重合的事后,再一个个本路回。

  过滤器在spring中呢闹要运用,AOP切面的落实方式就是因故到了过滤器模式,后面有空子我会介绍的。

  未完待续。。。

相关文章

标签:

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图