设计模式之过滤器模式(php实现)
/**
* 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.' ';
}
设计模式之过滤器模式(php实现)的更多相关文章
- 设计模式之过滤器模式——Java语言描述
过滤器模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来 实现 创建一个Person对象.Criteria 接口和实现了该接口的实体类,来过滤 Person 对象的列 ...
- Java设计模式应用——过滤器模式
storm引擎计算出一批中间告警结果,会发送一条kafka消息给告警入库服务,告警入库服务接收到kafka消息后读取中间告警文件,经过一系列处理后把最终告警存入mysql中. 实际上,中间告警结果可能 ...
- 设计模式系列之过滤器模式(Chriteria Pattern)
过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来.这种类 ...
- [07]Go设计模式:过滤器模式(FilterPattern)
目录 过滤器模式 一.简介 二.代码 三.参考链接 过滤器模式 一.简介 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使 ...
- 【设计模式 - 7】之过滤器模式(Filter)
1 模式简介 过滤器模式(Filter)也叫标准模式(Criteria),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来. 2 实例 需求 ...
- 设计模式のFilterPattern(过滤器模式)----结构模式
一.产生背景 我们有一堆“人”的对象,我们应该怎么选择出其中的男性.女性或者其他类型的呢?这时候我们可以用过滤器模式 二.通常做法 我们将创建一个 Person 对象.Criteria 接口和实现了该 ...
- Python进阶:设计模式之迭代器模式
在软件开发领域中,人们经常会用到这一个概念——“设计模式”(design pattern),它是一种针对软件设计的共性问题而提出的解决方案.在一本圣经级的书籍<设计模式:可复用面向对象软件的基础 ...
- Java进阶篇设计模式之六 ----- 组合模式和过滤器模式
前言 在上一篇中我们学习了结构型模式的外观模式和装饰器模式.本篇则来学习下组合模式和过滤器模式. 组合模式 简介 组合模式是用于把一组相似的对象当作一个单一的对象.组合模式依据树形结构来组合对象,用来 ...
- 过滤器模式(Filter Pattern)
过滤器模式 一.什么是过滤器模式 过滤器模式(Filter Pattern),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来.这种类型的设计模式属于结构型 ...
随机推荐
- PHP文件操作函数及文件指针理解
知识点: 一.fopen(),文件打开函数,读写参数有: 1.R : 只读,指针在文件开头 2.r+:读写,指针同上 3.W :只写,写入前会删除文件内容,然后指针回到文件开头,文件不存在则创建 4 ...
- Spring2集成iBatis2
从数据库中查询一条记录,演示Spring与iBatis的集成 1 编写sqlmaps与Domain对象 <?xml version="1.0" encoding=" ...
- 2016年NK冬季训练赛 民间题解
A题 水题,考察对行的读入和处理,注意使用long long #include <iostream> #include <cstring> #include <cstdi ...
- oracle怎么查看一个表或一个索引占用多少空间
很多时候我们想知道一个表或一个索引占用多少M的空间,以下脚本就是满足这个要求的,记住替换其中的内容. SELECT owner, segment_name, SUM(bytes)/1024/1024 ...
- 双系统Ubuntu 无 启用wifi选项
安装好双系统进入ubuntu(14.04)后发现只能用有线连接,不能用wifi.网络连接里无启用wifi选项. 1.查询网卡型号,发现是BCM43132 命令: lspci | grep -i n ...
- centos 安装使用smb
http://blog.csdn.net/edu_enth/article/details/52964295
- usb驱动---What is the difference between /dev/ttyUSB and /dev/ttyACM【转】
转自:http://blog.csdn.net/ppp2006/article/details/25654733 https://www.rfc1149.net/blog/2013/03/05/wha ...
- linux缺页异常处理--内核空间【转】
转自:http://blog.csdn.net/vanbreaker/article/details/7867720 版权声明:本文为博主原创文章,未经博主允许不得转载. 缺页异常被触发通常有两种情况 ...
- 150.Evaluate Reverse Polish Notation---逆波兰式求值
题目链接 题目大意:计算逆波兰表达式的值. 法一:stack,用stack存数,遇到操作符,则运算.代码如下(耗时12ms): public int evalRPN(String[] tokens) ...
- com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support/annotation/ColorRes.class
保存信息如上: 我在添加一个支持库的时候遇的问题,这个库com.yanzhenjie:album:1.0.5 这是由于v4包重复导致的,在网上我也找过多种解决方案 用了这种,方式 configur ...