過濾器模式(Filter Pattern)


過濾器模式 過濾器模式(Filter Pattern)或標准模式(Criteria Pattern)是一種設計模式,這種模式允許開發人員使用不同的標准來過濾一組對象,通過邏輯運算以解耦的方式把它們連接起來。這種類型的設計模式屬於結構型模式,它結合多個標准來獲得單一標准。 實現 我們將創建一個 Person 對象、Criteria 接口和實現了該接口的實體類,來過濾 Person 對象的列表。CriteriaPatternDemo,我..


過濾器模式(Filter Pattern)或標准模式(Criteria Pattern)是一種設計模式,這種模式允許開發人員使用不同的標准來過濾一組對象,通過邏輯運算以解耦的方式把它們連接起來。這種類型的設計模式屬於結構型模式,它結合多個標准來獲得單一標准。

 

復制代碼
/**
* github地址:https://github.com/ZQCard/design_pattern
* 過濾器模式(Filter Pattern)或標准模式(Criteria Pattern)是一種設計模式
* 這種模式允許開發人員使用不同的標准來過濾一組對象,通過邏輯運算以解耦的方式把它們連接起來。
* 這種類型的設計模式屬於結構型模式,它結合多個標准來獲得單一標准。
* 例子:篩選男人、女人、單身男人、單身女人
*/
復制代碼

(1)Person.class.php(對象類)

復制代碼
<?php

namespace Filter;

class Person
{
    private $name;
    private $gender;
    private $maritalStatus;

    public function __construct($name, $gender, $maritalStatus)
    {
        $this->name = $name;
        $this->gender = $gender;
        $this->maritalStatus = $maritalStatus;
    }

    public function __get($attributes)
    {
        return $this->$attributes;
    }
}
復制代碼

(2)Criteria.class.php(抽象接口,規范實現類)

復制代碼
<?php

namespace Filter;

interface Criteria
{
    public function meetCriteria($persons);
}
復制代碼

(3)Male.class.php(男性類篩選)

復制代碼
<?php

namespace Filter;

class CriteriaMale implements Criteria
{
    public function meetCriteria($persons)
    {
        $malePerson = [];
        foreach ($persons as $person) {
            if (strtoupper($person->gender) == 'MALE') {
                $malePerson[] = $person;
            }
        }
        return $malePerson;
    }
}
復制代碼

(4)Female.class.php(女性類篩選)

復制代碼
<?php

namespace Filter;

class CriteriaFemale implements Criteria
{
    public function meetCriteria($persons)
    {
        $femalePerson = [];
        foreach ($persons as $person) {
            if (strtoupper($person->gender) == 'FEMALE') {
                $femalePerson[] = $person;
            }
        }
        return $femalePerson;
    }
}
復制代碼

(5)Single.class.php(單身類篩選)

復制代碼
<?php

namespace Filter;

class CriteriaSingle implements Criteria
{
    public function meetCriteria($persons)
    {
        $singlePerson = [];
        foreach ($persons as $person) {
            if (strtoupper($person->maritalStatus) == 'SINGLE') {
                $singlePerson[] = $person;
            }
        }
        return $singlePerson;
    }
}
復制代碼

(6)OrCriteria.class.php(或者條件篩選)

復制代碼
<?php

namespace Filter;

class OrCriteria implements Criteria
{
    private $criteria;
    private $otherCriteria;

    public function __construct(Criteria $criteria, Criteria $otherCriteria)
    {
        $this->criteria = $criteria;
        $this->otherCriteria = $otherCriteria;
    }

    public function meetCriteria($persons)
    {
        $firstCriteriaItems = $this->criteria->meetCriteria($persons);
        $otherCriteriaItems = $this->otherCriteria->meetCriteria($persons);

        foreach ($otherCriteriaItems as $person) {
            if (!in_array($person, $firstCriteriaItems)) {
                $firstCriteriaItems[] = $person;
            }
        }

        return $firstCriteriaItems;
    }
}
復制代碼

(6)AndCriteria.class.php(並且條件篩選)

復制代碼
<?php

namespace Filter;

class AndCriteria implements Criteria
{
    private $criteria;
    private $otherCriteria;

    public function __construct(Criteria $criteria,Criteria $otherCriteria)
    {
        $this->criteria = $criteria;
        $this->otherCriteria = $otherCriteria;
    }

    public function meetCriteria($persons)
    {
        $firstCriteriaPerson = $this->criteria->meetCriteria($persons);
        return $this->otherCriteria->meetCriteria($firstCriteriaPerson);
    }
}
復制代碼

(7)filter.php(客戶端)

復制代碼
<?php
spl_autoload_register(function ($className){
    $className = str_replace('\\','/',$className);
    include $className.".class.php";
});

use Filter\Person;
use Filter\CriteriaMale;
use Filter\CriteriaFemale;
use Filter\CriteriaSingle;
use Filter\AndCriteria;
use Filter\OrCriteria;

$persons = [];
$persons[] = (new Person("Robert","Male", "Single"));
$persons[] = (new Person("John","Male", "Married"));
$persons[] = (new Person("Laura","Female", "Married"));
$persons[] = (new Person("Diana","Female", "Single"));
$persons[] = (new Person("Mike","Male", "Single"));
$persons[] = (new Person("Bobby","Male", "Single"));

$male = new CriteriaMale();
$female = new CriteriaFemale();
$single = new CriteriaSingle();
$singleMale = new AndCriteria($single, $male);
$singleOrFemale = new OrCriteria($single, $female);

//Males:
//Robert John Mike Bobby
echo "Males:";
$maleList = $male->meetCriteria($persons);
foreach ($maleList as $male){
    echo $male->name.'  ';
}
echo '<br/>';

//Females:
//Laura Diana
echo "Females:";
$maleList = $female->meetCriteria($persons);
foreach ($maleList as $male){
    echo $male->name.'  ';
}
echo '<br/>';


//Single Males:
//Robert Mike Bobby
echo "Single Males:";
$singleMaleList = $singleMale->meetCriteria($persons);
foreach ($singleMaleList as $male){
    echo $male->name.'  ';
}
echo '<br/>';


//Single Or Females:
//Robert Diana Mike Bobby Laura
echo "Single Or Females:";
$singleOrFemaleList = $singleOrFemale->meetCriteria($persons);
foreach ($singleOrFemaleList as $person){
    echo $person->name.'  ';
}
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM