溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP 中的specification規(guī)格模式是什么

發(fā)布時(shí)間:2020-12-16 14:33:35 來源:億速云 閱讀:190 作者:Leah 欄目:開發(fā)技術(shù)

PHP 中的specification規(guī)格模式是什么?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

1、模式定義

規(guī)格模式是組合模式的一種擴(kuò)展,在框架性開發(fā)中使用較多(項(xiàng)目級開發(fā)很少使用),這里做一個(gè)簡單的介紹。
規(guī)格模式(Specification)可以認(rèn)為是組合模式的一種擴(kuò)展。有時(shí)項(xiàng)目中某些條件決定了業(yè)務(wù)邏輯,這些條件就可以抽離出來以某種關(guān)系(與、或、非)進(jìn)行組合,從而靈活地對業(yè)務(wù)邏輯進(jìn)行定制。另外,在查詢、過濾等應(yīng)用場合中,通過預(yù)定義多個(gè)條件,然后使用這些條件的組合來處理查詢或過濾,而不是使用邏輯判斷語句來處理,可以簡化整個(gè)實(shí)現(xiàn)邏輯。

這里的每個(gè)條件就是一個(gè)規(guī)格,多個(gè)規(guī)格/條件通過串聯(lián)的方式以某種邏輯關(guān)系形成一個(gè)組合式的規(guī)格。

2、UML類圖

PHP 中的specification規(guī)格模式是什么

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)建一個(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ī)格抽象類
*/
abstract class AbstractSpecification implements SpecificationInterface
{
/**
* 檢查給定Item是否滿足所有規(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ī)格的邏輯與評估
*
* @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 中的specification規(guī)格模式是什么的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI