目录
PHP 设计模式系列之 specification规格模式,specification模式
您可能感兴趣的文章:
首页 后端开发 php教程 PHP 设计模式系列之 specification规格模式,specification模式_PHP教程

PHP 设计模式系列之 specification规格模式,specification模式_PHP教程

Jul 12, 2016 am 09:00 AM
模式

PHP 设计模式系列之 specification规格模式,specification模式

1、模式定义

规格模式是组合模式的一种扩展,在框架性开发中使用较多(项目级开发很少使用),这里做一个简单的介绍。
规格模式(Specification)可以认为是组合模式的一种扩展。有时项目中某些条件决定了业务逻辑,这些条件就可以抽离出来以某种关系(与、或、非)进行组合,从而灵活地对业务逻辑进行定制。另外,在查询、过滤等应用场合中,通过预定义多个条件,然后使用这些条件的组合来处理查询或过滤,而不是使用逻辑判断语句来处理,可以简化整个实现逻辑。

这里的每个条件就是一个规格,多个规格/条件通过串联的方式以某种逻辑关系形成一个组合式的规格。

2、UML类图


3、示例代码

Item.php

<&#63;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

<&#63;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

<&#63;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

<&#63;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

<&#63;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

<&#63;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

<&#63;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

<&#63;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(委托模式)

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1091112.htmlTechArticlePHP 设计模式系列之 specification规格模式,specification模式 1、模式定义 规格模式是组合模式的一种扩展,在框架性开发中使用较多(项目级开...
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

微信的免打扰模式有什么作用 微信的免打扰模式有什么作用 Feb 23, 2024 pm 10:48 PM

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

iPhone上的睡眠模式有何用途? iPhone上的睡眠模式有何用途? Nov 04, 2023 am 11:13 AM

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

请勿打扰模式在iPhone中不起作用:修复 请勿打扰模式在iPhone中不起作用:修复 Apr 24, 2024 pm 04:50 PM

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

epc+o模式是什么意思 epc+o模式是什么意思 Nov 09, 2022 am 10:54 AM

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

iPhone 15 Pro:如何摆脱状态栏中的静音模式符号 iPhone 15 Pro:如何摆脱状态栏中的静音模式符号 Sep 24, 2023 pm 10:01 PM

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

在 Windows 10/11 上如何离开 S 模式 在 Windows 10/11 上如何离开 S 模式 Aug 03, 2023 pm 08:17 PM

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

iOS 17中的待机模式使用指南 iOS 17中的待机模式使用指南 Aug 22, 2023 pm 04:01 PM

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

如何启用'记事本++深色模式”和'记事本++深色主题”? 如何启用'记事本++深色模式”和'记事本++深色主题”? Oct 27, 2023 pm 11:17 PM

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

See all articles