设计模式之过滤器模式(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对象在内存中的分配
对像在PHP 里面和整型.浮点型一样,也是一种数据类,都是存储不同类型数据用的, 在运行的时候都要加载到内存中去用,那么对象在内存里面是怎么体现的呢?内存从逻辑上 说大体上是分为4 段,栈空间段.堆空 ...
- 【Luogu】P2490黑白棋(博弈DP)
题目链接 题解链接 #include<cstdio> #include<cstring> #include<algorithm> #include<cstdl ...
- 2017 多校3 hdu 6061 RXD and functions
2017 多校3 hdu 6061 RXD and functions(FFT) 题意: 给一个函数\(f(x)=\sum_{i=0}^{n}c_i \cdot x^{i}\) 求\(g(x) = f ...
- hihocoder 1457 后缀自动机四·重复旋律7 求不同子串的和
描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 神奇的是小Hi发现了一部名字叫<十进制进行曲大全>的作品集,顾名思义,这部作品集里有许多作品 ...
- Windows1小时后关机命令
shutdown -s -t 3600 1.注销当前用户 shutdown - l 该命令只能注销本机用户,对远程计算机不适用. 2.关闭本地计算机 shutdown - s 3.重启本地计算机 sh ...
- Topcoder SRM 603 div1题解
昨天刚打了一场codeforces...困死了...不过赶在睡前终于做完了- 话说这好像是我第一次做250-500-1000的标配耶--- Easy(250pts): 题目大意:有一棵树,一共n个节点 ...
- 封装一下webform的公用方法:对于软件我把这些全封装在pagebase里面,这样所有的页面只调用一句 Init()即可,其他的全在页面上配置
/// <summary> /// 绑定新闻列表,带分页与查询 /// </summary> /// <param n ...
- glRotatef 转动方向
http://blog.sina.com.cn/s/blog_3c6889fe0100qko6.html glRotatef(GLfloat angle,GLfloat x,GLfloat y,GLf ...
- VMWare虚拟机如何与主机共享文件夹(最容易看懂的讲解)附图~
http://wenku.baidu.com/view/54ab9e19227916888486d776.html 新建好虚拟机并安装好系统后,在编辑虚拟机设置--选项进行以下设置: 点添加 选择你要 ...
- Kubernetes UI配置
#配置,在控制节点上操作#这里的镜像在谷歌上面需要FQ下载#######################################生成windows证书,将生成的证书IE.p12导入到IE个人证 ...