《PHP實例:PHP 設計模式系列之 specification規格模式》要點:
本文介紹了PHP實例:PHP 設計模式系列之 specification規格模式,希望對您有用。如果有疑問,可以聯系我們。
PHP應用1、模式定義
PHP應用規格模式是組合模式的一種擴展,在框架性開發中使用較多(項目級開發很少使用),這里做一個簡單的介紹.
規格模式(Specification)可以認為是組合模式的一種擴展.有時項目中某些條件決定了業務邏輯,這些條件就可以抽離出來以某種關系(與、或、非)進行組合,從而靈活地對業務邏輯進行定制.另外,在查詢、過濾等應用場合中,通過預定義多個條件,然后使用這些條件的組合來處理查詢或過濾,而不是使用邏輯判斷語句來處理,可以簡化整個實現邏輯.
PHP應用這里的每個條件就是一個規格,多個規格/條件通過串聯的方式以某種邏輯關系形成一個組合式的規格.
PHP應用2、UML類圖
PHP應用
PHP應用3、示例代碼
PHP應用Item.php
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;
}
}
PHP應用SpecificationInterface.php
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();
}
PHP應用AbstractSpecification.php
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);
}
}
PHP應用Plus.php
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);
}
}
PHP應用Either.php
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);
}
}
PHP應用Not.php
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);
}
}
PHP應用PriceSpecification.php
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;
}
}
PHP應用4、測試代碼
PHP應用Tests/SpecificationTest.php
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應用以上內容是維易PHP小編給大家分享的PHP 設計模式系列之 specification規格模式,希望本文分享能夠贊助大家.
維易PHP培訓學院每天發布《PHP實例:PHP 設計模式系列之 specification規格模式》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/7747.html