PHP Design Mode Series Specification specification mode, specification mode _php tutorial

Source: Internet
Author: User

PHP Design Mode Series Specification specification mode, specification mode


1. Pattern definition

Specification mode is an extension of the combined pattern, which is used more in framework development (project-level development is seldom used), and a simple introduction is made here.
The specification mode (specification) can be considered as an extension of the combined pattern. Sometimes the business logic is determined by certain conditions in the project that can be drawn out to be combined in a relationship (with, or, or not) to flexibly customize the business logic. In addition, in applications such as query, filter, and so on, it is possible to simplify the whole implementation logic by predefined multiple conditions and then using the combination of these conditions to process queries or filters instead of using logical judgment statements.

Each of these conditions is a specification, and 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

<?phpnamespace designpatterns\behavioral\specification;class item{protected $price;/*** an Item must has 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

<?phpnamespace designpatterns\behavioral\specification;/*** Specification Interface */interface specificationinterface{/*** Determine if the object meets specifications * * @param item $item * * @return bool*/public function IsSatisfiedBy (Item $item);/*** Create a logic and specification (and) * * @param Spe Cificationinterface $spec */public function Plus (specificationinterface $spec)/*** Create a logical or specification (OR) * * @param Specificationinterface $spec */public function either (Specificationinterface $spec);/*** Create a logical non-specification (not) */public function not ();}

abstractspecification.php

<?phpnamespace designpatterns\behavioral\specification;/*** Specification Abstract classes */abstract class Abstractspecification Implements specificationinterface{/*** checks whether the given item satisfies all rules * * @param Item $item * * @return bool*/abstract Public function IsSatisfiedBy (Item $item);/*** creates 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-specification (not) * * @return Specificationinterface*/public Function not () {return new not ($this);}}

plus.php

<?phpnamespace designpatterns\behavioral\specification;/*** Logic and Specifications (and) */class Plus extends Abstractspecification {protected $left;p rotected $right;/*** passed in the constructor in two sizes * * @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 both specifications * * @param item $item * * @return bool*/public function IsSatisfiedBy (Item $item) {return $this->left-& Gt;issatisfiedby ($item) && $this->right->issatisfiedby ($item);}

either.php

<?phpnamespace designpatterns\behavioral\specification;/*** logic or specification */class either extends abstractspecification{ Protected $left;p rotected $right/*** two combinations of 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 both specifications * * @param item $item * * @return bool*/public function IsSatisfiedBy (Item $item) {return $this->left-& Gt;issatisfiedby ($item) | | $this->right->issatisfiedby ($item);}}

not.php

<?phpnamespace designpatterns\behavioral\specification;/*** Logic Non-specification */class not extends abstractspecification{ protected $spec;/*** Pass in the constructor with the specified specification * * @param specificationinterface $spec */public function __construct ( Specificationinterface $spec) {$this->spec = $spec;} /*** returns the opposite result of the specification * * @param item $item * * @return bool*/public function IsSatisfiedBy (item $item) {return! $this->spec-> IsSatisfiedBy ($item);}}

pricespecification.php

<?phpnamespace designpatterns\behavioral\specification;/*** Determines whether the price of a given item is between the minimum and maximum values */class Pricespecification extends abstractspecification{protected $maxPrice;p rotected $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 the pricing for a given Item is between the minimum and maximum values * * @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

<?phpnamespace Designpatterns\behavioral\specification\tests;use designpatterns\behavioral\specification\ Pricespecification;use designpatterns\behavioral\specification\item;/*** specificationtest for test specification mode */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, $this->asserttrue ($spec- >issatisfiedby ($item)); $spec->setminprice (101); $this->assertfalse ($spec->issatisfiedby ($item)); $ Spec->setminprice, $this->asserttrue ($spec->issatisfiedby ($item));} Public Function testnotspecification () {$item = new Item, $spec = new Pricespecification (); $not = $spec->not (); $ This->assertfalse ($not->issatisfiedby ($item)), $spec->setmaxprice, $this->asserttrue ($not IsSatisfiedBy ($item)); $spec->sEtmaxprice, $this->assertfalse ($not->issatisfiedby ($item)), $spec->setminprice (101); $this Asserttrue ($not->issatisfiedby ($item)), $spec->setminprice, $this->assertfalse ($not, IsSatisfiedBy ($item));} Public Function testplusspecification () {$spec 1 = new pricespecification (); $spec 2 = new Pricespecification (); $plus = $ Spec1->plus ($spec 2); $item = new Item, $this->asserttrue ($plus->issatisfiedby ($item)); $spec 1-> Setmaxprice, $spec 2->setminprice, $this->asserttrue ($plus->issatisfiedby ($item)); $spec 1-> Setmaxprice, $spec 2->setminprice (101), $this->assertfalse ($plus->issatisfiedby ($item)); $spec 1-> Setmaxprice, $spec 2->setminprice, $this->assertfalse ($plus->issatisfiedby ($item));} Public Function testeitherspecification () {$spec 1 = new pricespecification (); $spec 2 = new Pricespecification (); $either = $spec 1->either ($spec 2); $item = new Item, $this->asserttrue ($either->issatisfiedby ($item)); $spec 1->setmaxprice, $spec 2->setmaxprice, $this->asserttrue ($either->issatisfiedby ($item)); Spec1->setmaxprice, $spec 2->setmaxprice (0), $this->asserttrue ($either->issatisfiedby ($item)); Spec1->setmaxprice (0), $spec 2->setmaxprice, $this->asserttrue ($either->issatisfiedby ($item)); Spec1->setmaxprice, $spec 2->setmaxprice, $this->assertfalse ($either->issatisfiedby ($item));}}

The above content is to help customers to share the PHP Design Model series of specification specifications model, I hope this article to share can help you.

Articles you may be interested in:

    • PHP design mode Composite (combined mode)
    • PHP design mode template (templates mode)
    • PHP design mode Command (commands mode)
    • PHP design mode Singleton (singleton mode)
    • PHP design mode OBSERVER (Observer mode)
    • PHP design mode strategy (policy mode)
    • PHP design mode Interpreter (interpreter mode)
    • PHP design mode Factory (Factory mode)
    • PHP design mode facade (appearance mode)
    • PHP design mode Delegation (delegate mode)

http://www.bkjia.com/PHPjc/1091112.html www.bkjia.com true http://www.bkjia.com/PHPjc/1091112.html techarticle PHP Design Mode Series Specification specification mode, specification mode 1, mode definition specification mode is an extension of the combination mode, in the framework of the development of the use of more (project-Level Open ...

  • 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.