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

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

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

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ī)格模式,希望本文分享能夠幫助大家。

相關(guān)文章

最新評(píng)論