設計模式學習系列——過濾器模式
阿新 • • 發佈:2018-05-07
one another color iam sed 得到 per span $max
過濾器模式
過濾器模式(Filter Pattern)或標準模式(Criteria Pattern)是一種設計模式,這種模式允許開發人員使用不同的標準來過濾一組對象,通過邏輯運算以解耦的方式把它們連接起來。這種類型的設計模式屬於結構型模式,它結合多個標準來獲得單一標準。
故名思意,就是將原始數據經過不同規則的篩選得到想要的數據。
角色:1)待過濾數據類;2)過濾器;3)邏輯過濾器;4)使用者
最近在找工作,這真是一個活生生被過濾的例子啊。於是以找工作,和公司各種過濾條件為例,詳見代碼(php版本):
1 <?php 2 /* 3 * 過濾器方法模式示例View Code4 */ 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歲以下的。
參考資料:
設計模式學習系列——過濾器模式