欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP 設(shè)計模式系列之 specification規(guī)格模式

 更新時間:2016年01月10日 09:41:39   投稿:mrr  
規(guī)格模式是組合模式的一種擴展,在框架性開發(fā)中使用較多(項目級開發(fā)很少使用),通過本文給大家介紹PHP 設(shè)計模式系列之 specification規(guī)格模式,對specification模式相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧

1、模式定義

規(guī)格模式是組合模式的一種擴展,在框架性開發(fā)中使用較多(項目級開發(fā)很少使用),這里做一個簡單的介紹。
規(guī)格模式(Specification)可以認為是組合模式的一種擴展。有時項目中某些條件決定了業(yè)務(wù)邏輯,這些條件就可以抽離出來以某種關(guān)系(與、或、非)進行組合,從而靈活地對業(yè)務(wù)邏輯進行定制。另外,在查詢、過濾等應(yīng)用場合中,通過預(yù)定義多個條件,然后使用這些條件的組合來處理查詢或過濾,而不是使用邏輯判斷語句來處理,可以簡化整個實現(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的價格是否介于最小值和最大值之間的規(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的定價是否在最小值和最大值之間
*
* @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è)計模式系列之 specification規(guī)格模式,希望本文分享能夠幫助大家。

相關(guān)文章

  • PHP通過API獲取手機號碼歸屬地

    PHP通過API獲取手機號碼歸屬地

    本API支持1、可輸入11位手機號查詢歸屬地如:13858861234,2、可輸入7位號段查詢歸屬地如:1335586,3、支持號段 13、14、15、17、18,有需要的小伙伴可以參考下。
    2015-05-05
  • PHP 對接美團大眾點評團購券(門票)的開發(fā)步驟

    PHP 對接美團大眾點評團購券(門票)的開發(fā)步驟

    這篇文章主要介紹了PHP 對接美團大眾點評團購券(門票)的開發(fā)步驟,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • PHP語言對接抖音快手小紅書視頻/圖片去水印API接口源碼

    PHP語言對接抖音快手小紅書視頻/圖片去水印API接口源碼

    這篇文章主要介紹了PHP語言對接抖音快手小紅書視頻/圖片去水印API接口源碼,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • PHP中new static()與new self()的比較

    PHP中new static()與new self()的比較

    在寫代碼時發(fā)現(xiàn) new static(),覺得實例化的地方不是應(yīng)該是 new self()嗎?怎么回事?通過查閱相關(guān)資料才知道具體情況,下面小編整理下方便日后查找
    2016-08-08
  • php判斷數(shù)組是否為空的實例方法

    php判斷數(shù)組是否為空的實例方法

    在本篇文章里小編給大家分享的是關(guān)于php判斷數(shù)組是否為空的實例方法,需要的朋友們可以參考下。
    2020-05-05
  • PHP遞歸刪除目錄幾個代碼實例

    PHP遞歸刪除目錄幾個代碼實例

    刪除一個空目錄用rmdir() 函數(shù)即可搞定。但是要刪除一個非空目錄,則無法進行快速的刪除,必須先將目錄中文件刪除,但是目錄里可能還會有子目錄,因此我們需要進行遞歸刪除
    2014-04-04
  • 淺談php中變量的數(shù)據(jù)類型判斷函數(shù)

    淺談php中變量的數(shù)據(jù)類型判斷函數(shù)

    下面小編就為大家?guī)硪黄獪\談php中變量的數(shù)據(jù)類型判斷函數(shù)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • ThinkPHP實現(xiàn)簡單登陸功能

    ThinkPHP實現(xiàn)簡單登陸功能

    這篇文章主要為大家詳細介紹了ThinkPHP實現(xiàn)簡單登陸功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • ThinkPHP3.2.3框架實現(xiàn)的空模塊、空控制器、空操作,跳轉(zhuǎn)到錯誤404頁面圖文詳解

    ThinkPHP3.2.3框架實現(xiàn)的空模塊、空控制器、空操作,跳轉(zhuǎn)到錯誤404頁面圖文詳解

    這篇文章主要介紹了ThinkPHP3.2.3框架實現(xiàn)的空模塊、空控制器、空操作,跳轉(zhuǎn)到錯誤404頁面,結(jié)合圖文形式詳細分析了thinkPHP3.2.3框架空模塊、空控制器、空操作,跳轉(zhuǎn)到錯誤404頁面具體操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2019-04-04
  • 基于CI(CodeIgniter)框架實現(xiàn)購物車功能的方法

    基于CI(CodeIgniter)框架實現(xiàn)購物車功能的方法

    這篇文章主要介紹了基于CI(CodeIgniter)框架實現(xiàn)購物車功能的方法,結(jié)合實例形式分析了CodeIgniter框架購物車功能類的定義及數(shù)據(jù)庫建立相關(guān)sql命令,需要的朋友可以參考下
    2018-04-04

最新評論