PHP 设计模式系列之 specification规格模式,specification模式_PHP教程
PHP 设计模式系列之 specification规格模式,specification模式
1、模式定义
规格模式是组合模式的一种扩展,在框架性开发中使用较多(项目级开发很少使用),这里做一个简单的介绍。
规格模式(Specification)可以认为是组合模式的一种扩展。有时项目中某些条件决定了业务逻辑,这些条件就可以抽离出来以某种关系(与、或、非)进行组合,从而灵活地对业务逻辑进行定制。另外,在查询、过滤等应用场合中,通过预定义多个条件,然后使用这些条件的组合来处理查询或过滤,而不是使用逻辑判断语句来处理,可以简化整个实现逻辑。
这里的每个条件就是一个规格,多个规格/条件通过串联的方式以某种逻辑关系形成一个组合式的规格。
2、UML类图
3、示例代码
Item.php
<?php namespace DesignPatterns\Behavioral\Specification; class Item { protected $price; /** * An item must have a price * * @param int $price */ public function __construct($price) { $this->price = $price; } /** * Get the items price * * @return int */ public function getPrice() { return $this->price; } }
SpecificationInterface.php
<?php namespace DesignPatterns\Behavioral\Specification; /** * 规格接口 */ interface SpecificationInterface { /** * 判断对象是否满足规格 * * @param Item $item * * @return bool */ public function isSatisfiedBy(Item $item); /** * 创建一个逻辑与规格(AND) * * @param SpecificationInterface $spec */ public function plus(SpecificationInterface $spec); /** * 创建一个逻辑或规格(OR) * * @param SpecificationInterface $spec */ public function either(SpecificationInterface $spec); /** * 创建一个逻辑非规格(NOT) */ public function not(); }
AbstractSpecification.php
<?php namespace DesignPatterns\Behavioral\Specification; /** * 规格抽象类 */ abstract class AbstractSpecification implements SpecificationInterface { /** * 检查给定Item是否满足所有规则 * * @param Item $item * * @return bool */ abstract public function isSatisfiedBy(Item $item); /** * 创建一个新的逻辑与规格(AND) * * @param SpecificationInterface $spec * * @return SpecificationInterface */ public function plus(SpecificationInterface $spec) { return new Plus($this, $spec); } /** * 创建一个新的逻辑或组合规格(OR) * * @param SpecificationInterface $spec * * @return SpecificationInterface */ public function either(SpecificationInterface $spec) { return new Either($this, $spec); } /** * 创建一个新的逻辑非规格(NOT) * * @return SpecificationInterface */ public function not() { return new Not($this); } }
Plus.php
<?php namespace DesignPatterns\Behavioral\Specification; /** * 逻辑与规格(AND) */ class Plus extends AbstractSpecification { protected $left; protected $right; /** * 在构造函数中传入两种规格 * * @param SpecificationInterface $left * @param SpecificationInterface $right */ public function __construct(SpecificationInterface $left, SpecificationInterface $right) { $this->left = $left; $this->right = $right; } /** * 返回两种规格的逻辑与评估 * * @param Item $item * * @return bool */ public function isSatisfiedBy(Item $item) { return $this->left->isSatisfiedBy($item) && $this->right->isSatisfiedBy($item); } }
Either.php
<?php namespace DesignPatterns\Behavioral\Specification; /** * 逻辑或规格 */ class Either extends AbstractSpecification { protected $left; protected $right; /** * 两种规格的组合 * * @param SpecificationInterface $left * @param SpecificationInterface $right */ public function __construct(SpecificationInterface $left, SpecificationInterface $right) { $this->left = $left; $this->right = $right; } /** * 返回两种规格的逻辑或评估 * * @param Item $item * * @return bool */ public function isSatisfiedBy(Item $item) { return $this->left->isSatisfiedBy($item) || $this->right->isSatisfiedBy($item); } }
Not.php
<?php namespace DesignPatterns\Behavioral\Specification; /** * 逻辑非规格 */ class Not extends AbstractSpecification { protected $spec; /** * 在构造函数中传入指定规格 * * @param SpecificationInterface $spec */ public function __construct(SpecificationInterface $spec) { $this->spec = $spec; } /** * 返回规格的相反结果 * * @param Item $item * * @return bool */ public function isSatisfiedBy(Item $item) { return !$this->spec->isSatisfiedBy($item); } }
PriceSpecification.php
<?php namespace DesignPatterns\Behavioral\Specification; /** * 判断给定Item的价格是否介于最小值和最大值之间的规格 */ class PriceSpecification extends AbstractSpecification { protected $maxPrice; protected $minPrice; /** * 设置最大值 * * @param int $maxPrice */ public function setMaxPrice($maxPrice) { $this->maxPrice = $maxPrice; } /** * 设置最小值 * * @param int $minPrice */ public function setMinPrice($minPrice) { $this->minPrice = $minPrice; } /** * 判断给定Item的定价是否在最小值和最大值之间 * * @param Item $item * * @return bool */ public function isSatisfiedBy(Item $item) { if (!empty($this->maxPrice) && $item->getPrice() > $this->maxPrice) { return false; } if (!empty($this->minPrice) && $item->getPrice() < $this->minPrice) { return false; } return true; } }
4、测试代码
Tests/SpecificationTest.php
<?php namespace DesignPatterns\Behavioral\Specification\Tests; use DesignPatterns\Behavioral\Specification\PriceSpecification; use DesignPatterns\Behavioral\Specification\Item; /** * SpecificationTest 用于测试规格模式 */ class SpecificationTest extends \PHPUnit_Framework_TestCase { public function testSimpleSpecification() { $item = new Item(100); $spec = new PriceSpecification(); $this->assertTrue($spec->isSatisfiedBy($item)); $spec->setMaxPrice(50); $this->assertFalse($spec->isSatisfiedBy($item)); $spec->setMaxPrice(150); $this->assertTrue($spec->isSatisfiedBy($item)); $spec->setMinPrice(101); $this->assertFalse($spec->isSatisfiedBy($item)); $spec->setMinPrice(100); $this->assertTrue($spec->isSatisfiedBy($item)); } public function testNotSpecification() { $item = new Item(100); $spec = new PriceSpecification(); $not = $spec->not(); $this->assertFalse($not->isSatisfiedBy($item)); $spec->setMaxPrice(50); $this->assertTrue($not->isSatisfiedBy($item)); $spec->setMaxPrice(150); $this->assertFalse($not->isSatisfiedBy($item)); $spec->setMinPrice(101); $this->assertTrue($not->isSatisfiedBy($item)); $spec->setMinPrice(100); $this->assertFalse($not->isSatisfiedBy($item)); } public function testPlusSpecification() { $spec1 = new PriceSpecification(); $spec2 = new PriceSpecification(); $plus = $spec1->plus($spec2); $item = new Item(100); $this->assertTrue($plus->isSatisfiedBy($item)); $spec1->setMaxPrice(150); $spec2->setMinPrice(50); $this->assertTrue($plus->isSatisfiedBy($item)); $spec1->setMaxPrice(150); $spec2->setMinPrice(101); $this->assertFalse($plus->isSatisfiedBy($item)); $spec1->setMaxPrice(99); $spec2->setMinPrice(50); $this->assertFalse($plus->isSatisfiedBy($item)); } public function testEitherSpecification() { $spec1 = new PriceSpecification(); $spec2 = new PriceSpecification(); $either = $spec1->either($spec2); $item = new Item(100); $this->assertTrue($either->isSatisfiedBy($item)); $spec1->setMaxPrice(150); $spec2->setMaxPrice(150); $this->assertTrue($either->isSatisfiedBy($item)); $spec1->setMaxPrice(150); $spec2->setMaxPrice(0); $this->assertTrue($either->isSatisfiedBy($item)); $spec1->setMaxPrice(0); $spec2->setMaxPrice(150); $this->assertTrue($either->isSatisfiedBy($item)); $spec1->setMaxPrice(99); $spec2->setMaxPrice(99); $this->assertFalse($either->isSatisfiedBy($item)); } }
以上内容是帮客之家小编给大家分享的PHP 设计模式系列之 specification规格模式,希望本文分享能够帮助大家。
您可能感兴趣的文章:
- php设计模式 Composite (组合模式)
- php设计模式 Template (模板模式)
- php设计模式 Command(命令模式)
- php设计模式 Singleton(单例模式)
- php设计模式 Observer(观察者模式)
- php设计模式 Strategy(策略模式)
- php设计模式 Interpreter(解释器模式)
- php设计模式 Factory(工厂模式)
- php设计模式 Facade(外观模式)
- php设计模式 Delegation(委托模式)

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

微信勿扰模式什么意思如今,随着智能手机的普及和移动互联网的迅猛发展,社交媒体平台已经成为人们日常生活中不可或缺的一部分。而微信作为国内最流行的社交媒体平台之一,几乎每个人都有一个微信账号。我们可以通过微信与朋友、家人、同事进行实时沟通,分享生活中的点滴,了解彼此的近况。然而,在这个时代,我们也不可避免地面临着信息过载和隐私泄露的问题,特别是对于那些需要专注或

长期以来,iOS设备一直能够使用“健康”应用程序跟踪您的睡眠模式等。但是,当您在睡觉时被通知打扰时,这不是很烦人吗?这些通知可能无关紧要,因此在此过程中会扰乱您的睡眠模式。虽然免打扰模式是避免睡觉时分心的好方法,但它可能会导致您错过夜间收到的重要电话和消息。值得庆幸的是,这就是睡眠模式的用武之地。让我们了解更多关于它以及如何在iPhone上使用它的信息。睡眠模式在iPhone上有什么作用睡眠模式是iOS中专用的专注模式,会根据你在“健康”App中的睡眠定时自动激活。它可以帮助您设置闹钟,然后可以

即使在“请勿打扰”模式下接听电话也可能是一种非常烦人的体验。顾名思义,请勿打扰模式可关闭来自邮件、消息等的所有来电通知和警报。您可以按照这些解决方案集进行修复。修复1–启用对焦模式在手机上启用对焦模式。步骤1–从顶部向下滑动以访问控制中心。步骤2–接下来,在手机上启用“对焦模式”。专注模式可在手机上启用“请勿打扰”模式。它不会让您的手机上出现任何来电提醒。修复2–更改对焦模式设置如果对焦模式设置中存在一些问题,则应进行修复。步骤1–打开您的iPhone设置窗口。步骤2–接下来,打开“对焦”模式设

epc+o模式就是指设计、采购等等为一体的总承包框架,它是在epc里面引申出来的一些运营环节;即在建设期内时,总承包商除了要去承担传统意义上的设计任务以外,还要去包揽在运营期内的所有维护任务。该模式可以极大程度提高许多项目的运营效率,也可以迅速降低运营成本。

在iPhone15Pro和iPhone15ProMax型号上,Apple推出了一个物理可编程的动作按钮,取代了音量按钮上方的传统响铃/静音开关。可以对操作按钮进行编程以执行几种不同的功能,但是在静音和响铃模式之间切换的能力并没有消失。默认情况下,长按一次操作按钮将使设备静音,按钮的触觉反馈将发出三个脉冲。两款iPhone15Pro机型在状态栏中的时间旁边都会显示一个划掉的铃铛符号,表示静音/静音模式已激活,并且它将一直保持到您再次长按“操作”按钮取消设备静音。如果您倾向于将iPhone置于静音模

S模式下的窗口旨在通过仅允许从Microsoft应用商店安装应用来提供增强的安全性和性能。虽然此功能有助于防止恶意软件和确保安全的计算环境,但它可能会限制想要从MicrosoftStore以外的源安装应用程序的用户。如果您发现自己处于这种情况并不断问自己如何在Windows10/11中切换出S模式,那么您来对地方了,因为我们将引导您完成如何使用两种不同的方法在Windows10/11中切换出S模式的步骤,确保您可以享受从您选择的任何地方安装应用程序的自由。了解如何在Windows中切换出S模式将

待机模式即将通过iOS17进入iPhone,本指南旨在向您展示如何在iPhone上使用此功能。待机模式是一项突破性功能,可将iPhone转变为动态、始终开启的智能显示屏。当您的iPhone在充电过程中水平侧放时,它会激活待机模式。此模式精美地展示了大量有用的小部件,包括但不限于当前时间、当地天气更新、您喜欢的照片的幻灯片,甚至是音乐播放控件。此模式的一个显着优点是它能够显示通知,允许用户查看和参与通知,而无需完全唤醒他们的iPhone。如何使用待机模式要使待机模式正常运行,iPhone必须运行i

记事本++暗模式v8.0没有参数,Notepad++是最有用的文本编辑器。在Windows10上运行的每个应用程序都支持暗模式。您可以命名Web浏览器,例如Chrome、Firefox和MicrosoftEdge。如果您在记事本++上工作,默认的白色背景可能会伤害您的眼睛。开发人员已将暗模式添加到版本8的Notepad++中,这是打开它的方法。为Windows11/10启用记事本++暗模式启动记事本++单击“设置”>“首选项”>“暗模式”选择“启用深色模式”重新启动记
