美文网首页
过滤器模式

过滤器模式

作者: 散装咖啡 | 来源:发表于2017-06-11 18:12 被阅读9次
    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);

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

相关文章

网友评论

      本文标题:过滤器模式

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