过滤器模式
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。
故名思意,就是将原始数据经过不同规则的筛选得到想要的数据。
角色:1)待过滤数据类;2)过滤器;3)逻辑过滤器;4)使用者
最近在找工作,这真是一个活生生被过滤的例子啊。于是以找工作,和公司各种过滤条件为例,详见代码(php版本):
1 <?php 2 /* 3 * 过滤器方法模式示例 4 */ 5 6 preg_match("/cli/i", php_sapi_name()) ? define(‘LINE_BREAK‘, PHP_EOL) : define(‘LINE_BREAK‘, "<br/>"); 7 8 /* 9 * 角色:待过滤数据类 10 * 找工作者 11 */ 12 class JobSeeker 13 { 14 private $_name; 15 private $_age; 16 private $_sex; 17 private $_working_years; 18 private $_skills; 19 20 public function __construct($name,$age,$sex,$working_years,$skills) { 21 $this->_name = $name; 22 $this->_age = $age; 23 $this->_sex = $sex; 24 $this->_working_years = $working_years; 25 $this->_skills = $skills; 26 } 27 28 public function getName(){ 29 return $this->_name; 30 } 31 public function getAge(){ 32 return $this->_age; 33 } 34 public function getSex(){ 35 return $this->_sex; 36 } 37 public function getWorkingYears(){ 38 return $this->_working_years; 39 } 40 public function getSkills(){ 41 return $this->_skills; 42 } 43 } 44 45 46 /* 47 *用来过滤的接口类 48 */ 49 interface Criteria{ 50 public function meetCriteria( $job_seekers); 51 } 52 53 /* 54 * 角色:过滤器 55 * 只需要男性的过滤类 56 */ 57 58 class CriteriaMale implements Criteria{ 59 public function meetCriteria( $job_seekers) { 60 if(empty($job_seekers)) 61 return null; 62 $job_seekers_criteria_male = array(); 63 foreach($job_seekers as $job_seeker){ 64 if($job_seeker->getSex() == "male") 65 $job_seekers_criteria_male[] = $job_seeker; 66 } 67 return $job_seekers_criteria_male; 68 } 69 } 70 71 /* 72 * 角色:过滤器 73 * 年龄区间的过滤器 74 */ 75 class CriteriaAge implements Criteria{ 76 private $_min = 18; 77 private $_max; 78 public function __construct($min,$max) { 79 $this->_min = $min; 80 $this->_max = $max; 81 } 82 public function meetCriteria( $job_seekers){ 83 $job_seekers_criteria_age = array(); 84 foreach($job_seekers as $job_seek){ 85 $age = $job_seek->getAge(); 86 if(is_int($age) && $age >= $this->_min && $age <= $this->_max){ 87 $job_seekers_criteria_age[] = $job_seek; 88 } 89 } 90 return $job_seekers_criteria_age; 91 } 92 } 93 94 /* 95 * 角色:逻辑过滤器 96 * AND过滤器 97 */ 98 class CriteriaAnd implements Criteria{ 99 private $_criteria; 100 private $_another_criteria; 101 102 public function __construct($criteria,$another_criteria) { 103 $this->_criteria = $criteria; 104 $this->_another_criteria = $another_criteria; 105 } 106 public function meetCriteria( $job_seekers) { 107 $firstCriteriaPersons = $this->_criteria->meetCriteria($job_seekers); 108 return $this->_another_criteria->meetCriteria($firstCriteriaPersons); 109 } 110 } 111 112 /* 113 * 打印筛选后人资料,与设计模式无关 114 */ 115 function printPerson($job_seekers){ 116 if(empty($job_seekers)){ 117 echo "".LINE_BREAK;return; 118 } 119 foreach($job_seekers as $job_seeker){ 120 printf("Person : [ Name : %s , Age : %s , Sex : %s , Working Years : %s , Skills : %s ]".LINE_BREAK,$job_seeker->getName(),$job_seeker->getAge(),$job_seeker->getSex(),$job_seeker->getWorkingYears(),$job_seeker->getSkills()); 121 } 122 123 } 124 /* 125 * 角色:使用者 126 * 实际中就是HR。。。 127 */ 128 $job_seekers = array(); 129 $job_seekers[] = new JobSeeker("xiao wang", 31, "male", 8, "php"); 130 $job_seekers[] = new JobSeeker("xiao li", 25, "male", 2, "java"); 131 $job_seekers[] = new JobSeeker("xiao zhan", 26, "female", 3, "test"); 132 $job_seekers[] = new JobSeeker("xiao huang", 28, "male", 4, "python"); 133 $job_seekers[] = new JobSeeker("xiao huang", 22, "male", 0, "c"); 134 135 $male = new CriteriaMale(); 136 $age_under_30 = new CriteriaAge(18,30); 137 $male_and_age_under_30 = new CriteriaAnd($male,$age_under_30); 138 139 echo "Males:".LINE_BREAK; 140 printPerson($male->meetCriteria($job_seekers)); 141 echo "Age Under 30:".LINE_BREAK; 142 printPerson($age_under_30->meetCriteria($job_seekers)); 143 echo "Males And Age Under 30:".LINE_BREAK; 144 printPerson($male_and_age_under_30->meetCriteria($job_seekers)); 145 /* 146 Males: 147 Person : [ Name : xiao wang , Age : 31 , Sex : male , Working Years : 8 , Skills : php ] 148 Person : [ Name : xiao li , Age : 25 , Sex : male , Working Years : 2 , Skills : java ] 149 Person : [ Name : xiao huang , Age : 28 , Sex : male , Working Years : 4 , Skills : python ] 150 Person : [ Name : xiao huang , Age : 22 , Sex : male , Working Years : 0 , Skills : c ] 151 Age Under 30: 152 Person : [ Name : xiao li , Age : 25 , Sex : male , Working Years : 2 , Skills : java ] 153 Person : [ Name : xiao zhan , Age : 26 , Sex : female , Working Years : 3 , Skills : test ] 154 Person : [ Name : xiao huang , Age : 28 , Sex : male , Working Years : 4 , Skills : python ] 155 Person : [ Name : xiao huang , Age : 22 , Sex : male , Working Years : 0 , Skills : c ] 156 Males And Age Under 30: 157 Person : [ Name : xiao li , Age : 25 , Sex : male , Working Years : 2 , Skills : java ] 158 Person : [ Name : xiao huang , Age : 28 , Sex : male , Working Years : 4 , Skills : python ] 159 Person : [ Name : xiao huang , Age : 22 , Sex : male , Working Years : 0 , Skills : c ] 160 */
第一个是过滤只要男性,第二个是过滤只要30岁以下的,第三个过滤是只要男性和30岁以下的。
参考资料:
原文地址:https://www.cnblogs.com/Andres/p/8999791.html
时间: 2024-10-12 07:46:27