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
网友评论