Merge remote-tracking branch 'memsb/master'

This commit is contained in:
Dominik Liebler
2013-12-06 10:14:52 +01:00
9 changed files with 384 additions and 0 deletions

View File

@ -41,6 +41,7 @@ The patterns can be structured in roughly three different categories. Please cli
* [Mediator](Mediator) [:notebook:](http://en.wikipedia.org/wiki/Mediator_pattern) * [Mediator](Mediator) [:notebook:](http://en.wikipedia.org/wiki/Mediator_pattern)
* [NullObject](NullObject) [:notebook:](http://en.wikipedia.org/wiki/Null_Object_pattern) * [NullObject](NullObject) [:notebook:](http://en.wikipedia.org/wiki/Null_Object_pattern)
* [Observer](Observer) [:notebook:](http://en.wikipedia.org/wiki/Observer_pattern) * [Observer](Observer) [:notebook:](http://en.wikipedia.org/wiki/Observer_pattern)
* [Specification](Specification) [:notebook:](http://en.wikipedia.org/wiki/Specification_pattern)
* [State](State) [:notebook:](http://en.wikipedia.org/wiki/State_pattern) * [State](State) [:notebook:](http://en.wikipedia.org/wiki/State_pattern)
* [Strategy](Strategy) [:notebook:](http://en.wikipedia.org/wiki/Strategy_pattern) * [Strategy](Strategy) [:notebook:](http://en.wikipedia.org/wiki/Strategy_pattern)
* [TemplateMethod](TemplateMethod) [:notebook:](http://en.wikipedia.org/wiki/Template_method_pattern) * [TemplateMethod](TemplateMethod) [:notebook:](http://en.wikipedia.org/wiki/Template_method_pattern)

View File

@ -0,0 +1,53 @@
<?php
namespace DesignPatterns\Specification;
/**
* An abstract specification allows the creation of wrapped specifications
*/
abstract class AbstractSpecification implements SpecificationInterface
{
/**
* Checks if given item meets all criteria
*
* @param Item $item
*
* @return bool
*/
public function isSatisfiedBy(Item $item)
{
}
/**
* Creates a new logical AND specification
*
* @param SpecificationInterface $spec
*
* @return SpecificationInterface
*/
public function plus(SpecificationInterface $spec)
{
return new Plus($this, $spec);
}
/**
* Creates a new logical OR composite specification
*
* @param SpecificationInterface $spec
*
* @return SpecificationInterface
*/
public function either(SpecificationInterface $spec)
{
return new Either($this, $spec);
}
/**
* Creates a new logical NOT specification
*
* @return SpecificationInterface
*/
public function not()
{
return new Not($this);
}
}

37
Specification/Either.php Executable file
View File

@ -0,0 +1,37 @@
<?php
namespace DesignPatterns\Specification;
/**
* A logical AND specification
*/
class Either extends AbstractSpecification
{
protected $left;
protected $right;
/**
* A composite wrapper of two specifications
*
* @param SpecificationInterface $left
* @param SpecificationInterface $right
*/
public function __construct(SpecificationInterface $left, SpecificationInterface $right)
{
$this->left = $left;
$this->right = $right;
}
/**
* Returns the evaluation of both wrapped specifications as a logical AND
*
* @param Item $item
*
* @return bool
*/
public function isSatisfiedBy(Item $item)
{
return $this->left->isSatisfiedBy($item) || $this->right->isSatisfiedBy($item);
}
}

31
Specification/Item.php Normal file
View File

@ -0,0 +1,31 @@
<?php
namespace DesignPatterns\Specification;
/**
* An trivial item
*/
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;
}
}

34
Specification/Not.php Executable file
View File

@ -0,0 +1,34 @@
<?php
namespace DesignPatterns\Specification;
/**
* A logical Not specification
*/
class Not extends AbstractSpecification
{
protected $spec;
/**
* Creates a new specification wrapping another
*
* @param SpecificationInterface $spec
*/
public function __construct(SpecificationInterface $spec)
{
$this->spec = $spec;
}
/**
* Returns the negated result of the wrapped specification
*
* @param Item $item
*
* @return bool
*/
public function isSatisfiedBy(Item $item)
{
return !$this->spec->isSatisfiedBy($item);
}
}

37
Specification/Plus.php Executable file
View File

@ -0,0 +1,37 @@
<?php
namespace DesignPatterns\Specification;
/**
* A logical AND specification
*/
class Plus extends AbstractSpecification
{
protected $left;
protected $right;
/**
* Creation of a locical AND of two specifications
*
* @param SpecificationInterface $left
* @param SpecificationInterface $right
*/
public function __construct(SpecificationInterface $left, SpecificationInterface $right)
{
$this->left = $left;
$this->right = $right;
}
/**
* Checks if the composite AND of specifications passes
*
* @param Item $item
*
* @return bool
*/
public function isSatisfiedBy(Item $item)
{
return $this->left->isSatisfiedBy($item) && $this->right->isSatisfiedBy($item);
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace DesignPatterns\Specification;
/**
* A specification to check an Item is priced between min and max
*/
class PriceSpecification extends AbstractSpecification
{
protected $maxPrice;
protected $minPrice;
/**
* Sets the optional maximum price
*
* @param int $maxPrice
*/
public function setMaxPrice($maxPrice)
{
$this->maxPrice = $maxPrice;
}
/**
* Sets the optional minimum price
*
* @param int $minPrice
*/
public function setMinPrice($minPrice)
{
$this->minPrice = $minPrice;
}
/**
* Checks if Item price falls between bounds
*
* @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;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace DesignPatterns\Specification;
/**
* An interface for a specification
*/
interface SpecificationInterface
{
/**
* A boolean evaluation indicating if the object meets the specification
*
* @param Item $item
*
* @return bool
*/
public function isSatisfiedBy(Item $item);
/**
* Creates a logical AND specification
*
* @param SpecificationInterface $spec
*/
public function plus(SpecificationInterface $spec);
/**
* Creates a logical OR specification
*
* @param SpecificationInterface $spec
*/
public function either(SpecificationInterface $spec);
/**
* Creates a logical not specification
*/
public function not();
}

View File

@ -0,0 +1,103 @@
<?php
namespace DesignPatterns\Tests\Specification;
use DesignPatterns\Specification\PriceSpecification;
use DesignPatterns\Specification\Item;
/**
* SpecificationTest tests the specification pattern
*/
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));
}
}