PHP 設(shè)計(jì)模式系列之 specification規(guī)格模式
1、模式定義
規(guī)格模式是組合模式的一種擴(kuò)展,在框架性開(kāi)發(fā)中使用較多(項(xiàng)目級(jí)開(kāi)發(fā)很少使用),這里做一個(gè)簡(jiǎn)單的介紹。
規(guī)格模式(Specification)可以認(rèn)為是組合模式的一種擴(kuò)展。有時(shí)項(xiàng)目中某些條件決定了業(yè)務(wù)邏輯,這些條件就可以抽離出來(lái)以某種關(guān)系(與、或、非)進(jìn)行組合,從而靈活地對(duì)業(yè)務(wù)邏輯進(jìn)行定制。另外,在查詢(xún)、過(guò)濾等應(yīng)用場(chǎng)合中,通過(guò)預(yù)定義多個(gè)條件,然后使用這些條件的組合來(lái)處理查詢(xún)或過(guò)濾,而不是使用邏輯判斷語(yǔ)句來(lái)處理,可以簡(jiǎn)化整個(gè)實(shí)現(xiàn)邏輯。
這里的每個(gè)條件就是一個(gè)規(guī)格,多個(gè)規(guī)格/條件通過(guò)串聯(lián)的方式以某種邏輯關(guān)系形成一個(gè)組合式的規(guī)格。
2、UML類(lèi)圖
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; /** * 規(guī)格接口 */ interface SpecificationInterface { /** * 判斷對(duì)象是否滿(mǎn)足規(guī)格 * * @param Item $item * * @return bool */ public function isSatisfiedBy(Item $item); /** * 創(chuàng)建一個(gè)邏輯與規(guī)格(AND) * * @param SpecificationInterface $spec */ public function plus(SpecificationInterface $spec); /** * 創(chuàng)建一個(gè)邏輯或規(guī)格(OR) * * @param SpecificationInterface $spec */ public function either(SpecificationInterface $spec); /** * 創(chuàng)建一個(gè)邏輯非規(guī)格(NOT) */ public function not(); }
AbstractSpecification.php
<?php namespace DesignPatterns\Behavioral\Specification; /** * 規(guī)格抽象類(lèi) */ abstract class AbstractSpecification implements SpecificationInterface { /** * 檢查給定Item是否滿(mǎn)足所有規(guī)則 * * @param Item $item * * @return bool */ abstract public function isSatisfiedBy(Item $item); /** * 創(chuàng)建一個(gè)新的邏輯與規(guī)格(AND) * * @param SpecificationInterface $spec * * @return SpecificationInterface */ public function plus(SpecificationInterface $spec) { return new Plus($this, $spec); } /** * 創(chuàng)建一個(gè)新的邏輯或組合規(guī)格(OR) * * @param SpecificationInterface $spec * * @return SpecificationInterface */ public function either(SpecificationInterface $spec) { return new Either($this, $spec); } /** * 創(chuàng)建一個(gè)新的邏輯非規(guī)格(NOT) * * @return SpecificationInterface */ public function not() { return new Not($this); } }
Plus.php
<?php namespace DesignPatterns\Behavioral\Specification; /** * 邏輯與規(guī)格(AND) */ class Plus extends AbstractSpecification { protected $left; protected $right; /** * 在構(gòu)造函數(shù)中傳入兩種規(guī)格 * * @param SpecificationInterface $left * @param SpecificationInterface $right */ public function __construct(SpecificationInterface $left, SpecificationInterface $right) { $this->left = $left; $this->right = $right; } /** * 返回兩種規(guī)格的邏輯與評(píng)估 * * @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; /** * 邏輯或規(guī)格 */ class Either extends AbstractSpecification { protected $left; protected $right; /** * 兩種規(guī)格的組合 * * @param SpecificationInterface $left * @param SpecificationInterface $right */ public function __construct(SpecificationInterface $left, SpecificationInterface $right) { $this->left = $left; $this->right = $right; } /** * 返回兩種規(guī)格的邏輯或評(píng)估 * * @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; /** * 邏輯非規(guī)格 */ class Not extends AbstractSpecification { protected $spec; /** * 在構(gòu)造函數(shù)中傳入指定規(guī)格 * * @param SpecificationInterface $spec */ public function __construct(SpecificationInterface $spec) { $this->spec = $spec; } /** * 返回規(guī)格的相反結(jié)果 * * @param Item $item * * @return bool */ public function isSatisfiedBy(Item $item) { return !$this->spec->isSatisfiedBy($item); } }
PriceSpecification.php
<?php namespace DesignPatterns\Behavioral\Specification; /** * 判斷給定Item的價(jià)格是否介于最小值和最大值之間的規(guī)格 */ class PriceSpecification extends AbstractSpecification { protected $maxPrice; protected $minPrice; /** * 設(shè)置最大值 * * @param int $maxPrice */ public function setMaxPrice($maxPrice) { $this->maxPrice = $maxPrice; } /** * 設(shè)置最小值 * * @param int $minPrice */ public function setMinPrice($minPrice) { $this->minPrice = $minPrice; } /** * 判斷給定Item的定價(jià)是否在最小值和最大值之間 * * @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、測(cè)試代碼
Tests/SpecificationTest.php
<?php namespace DesignPatterns\Behavioral\Specification\Tests; use DesignPatterns\Behavioral\Specification\PriceSpecification; use DesignPatterns\Behavioral\Specification\Item; /** * SpecificationTest 用于測(cè)試規(guī)格模式 */ 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)); } }
以上內(nèi)容是腳本之家小編給大家分享的PHP 設(shè)計(jì)模式系列之 specification規(guī)格模式,希望本文分享能夠幫助大家。
- php設(shè)計(jì)模式之簡(jiǎn)單工廠(chǎng)模式詳解
- PHP中“簡(jiǎn)單工廠(chǎng)模式”實(shí)例代碼講解
- PHP設(shè)計(jì)模式之簡(jiǎn)單投訴頁(yè)面實(shí)例
- PHP設(shè)計(jì)模式之觀(guān)察者模式實(shí)例
- php設(shè)計(jì)模式之委托模式
- PHP常用設(shè)計(jì)模式之委托設(shè)計(jì)模式
- 學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)備忘錄模式(Memento)
- 學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)觀(guān)察者模式(Observer)
- 學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)模板方法模式
- 實(shí)例講解PHP設(shè)計(jì)模式編程中的簡(jiǎn)單工廠(chǎng)模式
相關(guān)文章
PHP通過(guò)API獲取手機(jī)號(hào)碼歸屬地
本API支持1、可輸入11位手機(jī)號(hào)查詢(xún)歸屬地如:13858861234,2、可輸入7位號(hào)段查詢(xún)歸屬地如:1335586,3、支持號(hào)段 13、14、15、17、18,有需要的小伙伴可以參考下。2015-05-05PHP 對(duì)接美團(tuán)大眾點(diǎn)評(píng)團(tuán)購(gòu)券(門(mén)票)的開(kāi)發(fā)步驟
這篇文章主要介紹了PHP 對(duì)接美團(tuán)大眾點(diǎn)評(píng)團(tuán)購(gòu)券(門(mén)票)的開(kāi)發(fā)步驟,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04PHP語(yǔ)言對(duì)接抖音快手小紅書(shū)視頻/圖片去水印API接口源碼
這篇文章主要介紹了PHP語(yǔ)言對(duì)接抖音快手小紅書(shū)視頻/圖片去水印API接口源碼,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08PHP中new static()與new self()的比較
在寫(xiě)代碼時(shí)發(fā)現(xiàn) new static(),覺(jué)得實(shí)例化的地方不是應(yīng)該是 new self()嗎?怎么回事?通過(guò)查閱相關(guān)資料才知道具體情況,下面小編整理下方便日后查找2016-08-08淺談php中變量的數(shù)據(jù)類(lèi)型判斷函數(shù)
下面小編就為大家?guī)?lái)一篇淺談php中變量的數(shù)據(jù)類(lèi)型判斷函數(shù)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03ThinkPHP實(shí)現(xiàn)簡(jiǎn)單登陸功能
這篇文章主要為大家詳細(xì)介紹了ThinkPHP實(shí)現(xiàn)簡(jiǎn)單登陸功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04ThinkPHP3.2.3框架實(shí)現(xiàn)的空模塊、空控制器、空操作,跳轉(zhuǎn)到錯(cuò)誤404頁(yè)面圖文詳解
這篇文章主要介紹了ThinkPHP3.2.3框架實(shí)現(xiàn)的空模塊、空控制器、空操作,跳轉(zhuǎn)到錯(cuò)誤404頁(yè)面,結(jié)合圖文形式詳細(xì)分析了thinkPHP3.2.3框架空模塊、空控制器、空操作,跳轉(zhuǎn)到錯(cuò)誤404頁(yè)面具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04基于CI(CodeIgniter)框架實(shí)現(xiàn)購(gòu)物車(chē)功能的方法
這篇文章主要介紹了基于CI(CodeIgniter)框架實(shí)現(xiàn)購(gòu)物車(chē)功能的方法,結(jié)合實(shí)例形式分析了CodeIgniter框架購(gòu)物車(chē)功能類(lèi)的定義及數(shù)據(jù)庫(kù)建立相關(guān)sql命令,需要的朋友可以參考下2018-04-04