Specification Specification mode _php example of PHP design pattern series

Source: Internet
Author: User

1. Pattern definition

The specification pattern is an extension of the combination mode, which is used more in the Framework development (project level development is seldom used), and here makes a simple introduction.
The specification pattern (specification) can be considered an extension of the combinatorial pattern. Sometimes certain conditions in a project dictate business logic that can be drawn out and combined in a relationship (with, or, not) to flexibly tailor the business logic. In addition, in the query, filtering and other applications, by predefined multiple conditions, and then use the combination of these conditions to process queries or filtering, rather than using logical judgment statements to deal with, you can simplify the entire implementation logic.

Each condition here is a specification in which multiple specifications/conditions form a modular specification in a logical relationship in a series of ways.

2, UML class diagram


3. Sample Code

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;
/**
* Specification interface
/
interface specificationinterface
{
/**
* To determine whether the object meets specifications
*
* @param Item $ Item
*
* @return bool
/Public Function IsSatisfiedBy (item $item);

/**
* Create a logic and specification (and)
*
* @param specificationinterface $spec/public
function plus ( Specificationinterface $spec);
/**
* Create a logical OR specification (OR)
*
* @param specificationinterface $spec
/Public function either ( Specificationinterface $spec);

/**
* Create a logical non spec (not)/public
function not ();

abstractspecification.php

<?php
namespace Designpatterns\behavioral\specification;

/**
* Specification Abstract class * *
abstractspecification implements Specificationinterface
{
/**
* Check whether the given item satisfies all rules
* *
@param item $item
* *
@return bool/
Abstract Public function IsSatisfiedBy (Item $item);
/**
* Create a new logic and specification (and) *
*
@param specificationinterface $spec *
*
@return Specificationinterface
*/public
function plus (specificationinterface $spec)
{return
new Plus ( $this, $spec);
}
/**
* Create a new logical OR combination specification (OR) *
*
@param specificationinterface $spec
* *
@return Specificationinterface
*
/Public function either (Specificationinterface $spec)
{return
new Either ($this, $spec);
}
/**
* Create a new logical non spec (not)
*
* @return Specificationinterface */public
function not
()
{ Return
new Not ($this);
}

plus.php

<?php
namespace Designpatterns\behavioral\specification;
/**
* Logic and Specification (and) */
class Plus extends abstractspecification
{
protected $left;
protected $right;

/**
* Two specifications are passed in the constructor *
*
@param specificationinterface $left
* @param specificationinterface $right
*
/Public Function __construct (specificationinterface $left, Specificationinterface $right)
{
$this->left = $left;
$this->right = $right;
}
/**
* Returns the logic and evaluation of two specifications * *
@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;

/**
* Logic or Specification */
class either extends Abstractspecification
{

protected $left;
protected $right;
/** *
combination of two specifications *
* @param specificationinterface $left
* @param specificationinterface $right
*
/Public Function __construct (specificationinterface $left, Specificationinterface $right)
{
$this->left = $left;
$this->right = $right;
}
/**
* Returns the logic or evaluation of two specifications * *
@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;

/**
* Logic non spec
/class not extends Abstractspecification
{
protected $spec;
/**
* Pass the specified specification in the constructor * *
@param specificationinterface $spec
/Public Function __construct ( Specificationinterface $spec)
{
$this->spec = $spec;
}
/**
* Returns the opposite result of specification * *
@param Item $item
* *
@return
bool
/Public function IsSatisfiedBy (Item $item)
{return
! $this->spec->issatisfiedby ($item);
}

pricespecification.php

<?php
namespace Designpatterns\behavioral\specification;

/**
* To determine whether the price of a given item is between the minimum and the maximum value */
class Pricespecification extends Abstractspecification
{
protected $maxPrice;
protected $minPrice;
/**
* Set Maximum
*
* @param int $maxPrice
/public
function Setmaxprice ($maxPrice)
{
$this->maxprice = $maxPrice;
}
/**
* Set minimum value
* *
@param int $minPrice
/Public Function Setminprice ($minPrice)
{
$this->minprice = $minPrice;
}
/**
* Determines whether a given Item is priced between minimum and maximum * *
@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. Test code

tests/specificationtest.php

<?php namespace Designpatterns\behavioral\specification\tests;
Use designpatterns\behavioral\specification\pricespecification;
Use Designpatterns\behavioral\specification\item; /** * specificationtest for Test spec mode/class Specificationtest extends \phpunit_framework_testcase {public Function Testsimp Lespecification () {$item = new Item; $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)); The Public Function testnotspecification () {$item = new Item; $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)); The Public Function testplusspecification () {$spec 1 = new pricespecification (); $spec 2 = new Pricespecification (); $plus =
$spec 1->plus ($spec 2);
$item = new Item (100);
$this->asserttrue ($plus->issatisfiedby ($item));
$spec 1->setmaxprice (150);
$spec 2->setminprice (50);
$this->asserttrue ($plus->issatisfiedby ($item));
$spec 1->setmaxprice (150);
$spec 2->setminprice (101);
$this->assertfalse ($plus->issatisfiedby ($item));
$spec 1->setmaxprice (99);
$spec 2->setminprice (50);
$this->assertfalse ($plus->issatisfiedby ($item)); The Public Function testeitherspecification () {$spec 1 = new pricespecification (); $spec 2 = new Pricespecification (); $eithe
r = $spec 1->either ($spec 2); $item = new Item (100);
$this->asserttrue ($either->issatisfiedby ($item));
$spec 1->setmaxprice (150);
$spec 2->setmaxprice (150);
$this->asserttrue ($either->issatisfiedby ($item));
$spec 1->setmaxprice (150);
$spec 2->setmaxprice (0);
$this->asserttrue ($either->issatisfiedby ($item));
$spec 1->setmaxprice (0);
$spec 2->setmaxprice (150);
$this->asserttrue ($either->issatisfiedby ($item));
$spec 1->setmaxprice (99);
$spec 2->setmaxprice (99);
$this->assertfalse ($either->issatisfiedby ($item)); }
}

Above is the cloud Habitat Community small set to share the PHP design Pattern Series of specification specification mode, I hope this article can help you to share.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.