PHP 設(shè)計(jì)模式系列之 specification規(guī)格模式
1、模式定義
規(guī)格模式是組合模式的一種擴(kuò)展,在框架性開發(fā)中使用較多(項(xiàng)目級開發(fā)很少使用),這里做一個簡單的介紹。
規(guī)格模式(Specification)可以認(rèn)為是組合模式的一種擴(kuò)展。有時項(xiàng)目中某些條件決定了業(yè)務(wù)邏輯,這些條件就可以抽離出來以某種關(guān)系(與、或、非)進(jìn)行組合,從而靈活地對業(yè)務(wù)邏輯進(jìn)行定制。另外,在查詢、過濾等應(yīng)用場合中,通過預(yù)定義多個條件,然后使用這些條件的組合來處理查詢或過濾,而不是使用邏輯判斷語句來處理,可以簡化整個實(shí)現(xiàn)邏輯。
這里的每個條件就是一個規(guī)格,多個規(guī)格/條件通過串聯(lián)的方式以某種邏輯關(guān)系形成一個組合式的規(guī)格。
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; /** * 規(guī)格接口 */ interface SpecificationInterface { /** * 判斷對象是否滿足規(guī)格 * * @param Item $item * * @return bool */ public function isSatisfiedBy(Item $item); /** * 創(chuàng)建一個邏輯與規(guī)格(AND) * * @param SpecificationInterface $spec */ public function plus(SpecificationInterface $spec); /** * 創(chuàng)建一個邏輯或規(guī)格(OR) * * @param SpecificationInterface $spec */ public function either(SpecificationInterface $spec); /** * 創(chuàng)建一個邏輯非規(guī)格(NOT) */ public function not(); }
AbstractSpecification.php
<?php namespace DesignPatterns\Behavioral\Specification; /** * 規(guī)格抽象類 */ abstract class AbstractSpecification implements SpecificationInterface { /** * 檢查給定Item是否滿足所有規(guī)則 * * @param Item $item * * @return bool */ abstract public function isSatisfiedBy(Item $item); /** * 創(chuàng)建一個新的邏輯與規(guī)格(AND) * * @param SpecificationInterface $spec * * @return SpecificationInterface */ public function plus(SpecificationInterface $spec) { return new Plus($this, $spec); } /** * 創(chuàng)建一個新的邏輯或組合規(guī)格(OR) * * @param SpecificationInterface $spec * * @return SpecificationInterface */ public function either(SpecificationInterface $spec) { return new Either($this, $spec); } /** * 創(chuàng)建一個新的邏輯非規(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ī)格的邏輯與評估 * * @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ī)格的邏輯或評估 * * @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、測試代碼
Tests/SpecificationTest.php
<?php namespace DesignPatterns\Behavioral\Specification\Tests; use DesignPatterns\Behavioral\Specification\PriceSpecification; use DesignPatterns\Behavioral\Specification\Item; /** * SpecificationTest 用于測試規(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ì)模式之簡單工廠模式詳解
- PHP中“簡單工廠模式”實(shí)例代碼講解
- PHP設(shè)計(jì)模式之簡單投訴頁面實(shí)例
- PHP設(shè)計(jì)模式之觀察者模式實(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)觀察者模式(Observer)
- 學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)模板方法模式
- 實(shí)例講解PHP設(shè)計(jì)模式編程中的簡單工廠模式
相關(guān)文章
PHP 對接美團(tuán)大眾點(diǎn)評團(tuán)購券(門票)的開發(fā)步驟
這篇文章主要介紹了PHP 對接美團(tuán)大眾點(diǎn)評團(tuán)購券(門票)的開發(fā)步驟,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04PHP中new static()與new self()的比較
在寫代碼時發(fā)現(xiàn) new static(),覺得實(shí)例化的地方不是應(yīng)該是 new self()嗎?怎么回事?通過查閱相關(guān)資料才知道具體情況,下面小編整理下方便日后查找2016-08-08淺談php中變量的數(shù)據(jù)類型判斷函數(shù)
下面小編就為大家?guī)硪黄獪\談php中變量的數(shù)據(jù)類型判斷函數(shù)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03ThinkPHP3.2.3框架實(shí)現(xiàn)的空模塊、空控制器、空操作,跳轉(zhuǎn)到錯誤404頁面圖文詳解
這篇文章主要介紹了ThinkPHP3.2.3框架實(shí)現(xiàn)的空模塊、空控制器、空操作,跳轉(zhuǎn)到錯誤404頁面,結(jié)合圖文形式詳細(xì)分析了thinkPHP3.2.3框架空模塊、空控制器、空操作,跳轉(zhuǎn)到錯誤404頁面具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04基于CI(CodeIgniter)框架實(shí)現(xiàn)購物車功能的方法
這篇文章主要介紹了基于CI(CodeIgniter)框架實(shí)現(xiàn)購物車功能的方法,結(jié)合實(shí)例形式分析了CodeIgniter框架購物車功能類的定義及數(shù)據(jù)庫建立相關(guān)sql命令,需要的朋友可以參考下2018-04-04