Specification mode of the PHP design pattern series, specification pattern _ PHP Tutorial

Source: Internet
Author: User
The specification and specification modes of the PHP design pattern series. The specification model of the PHP design pattern series. specification pattern 1. the Pattern Definition specification pattern is an extension of the combination pattern, there are many applications in framework development (project-level PHP design pattern series specification pattern and specification pattern)

1. mode definition

The specification mode is an extension of the combination mode. it is widely used in framework development (rarely used in project-level development). here is a brief introduction.
Specification can be considered as an extension of the combination mode. Sometimes some conditions in a project determine the business logic, and these conditions can be extracted and combined in a certain relationship (and, or, or not) to flexibly customize the business logic. In addition, in application scenarios such as query and filtering, you can define multiple conditions and then use a combination of these conditions to process queries or filters, instead of using logical judgment statements for processing, the entire implementation logic can be simplified.

Each condition here is a specification. Multiple specifications/conditions are connected in series to form a combined specification in a logical relationship.

2. UML class diagram


3. Sample code

Item. php

<?phpnamespace 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

<? Phpnamespace DesignPatterns \ Behavioral \ Specification; /***** specification interface */interface SpecificationInterface {/*** determine whether the object meets the specification ** @ 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 logic 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; /*** type abstract class */abstract class AbstractSpecification implements SpecificationInterface {/*** check whether all rules are satisfied for a given Item ** @ 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 composite 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 Specification (AND) */class Plus extends define actspecification {protected $ left; protected $ right; /*** input two types in the constructor ** @ param SpecificationInterface $ left * @ param SpecificationInterface $ right */public function _ construct (SpecificationInterface $ left, specificationInterface $ right) {$ this-> left = $ left; $ this-> right = $ right ;} /*** return the logic and evaluation of the two types ** @ param Item $ item ** @ return bool */public function isSatisfiedBy (Item $ item) {return $ this-> left-> isSatisfiedBy ($ item) & $ this-> right-> isSatisfiedBy ($ item );}}

Either. php

<? Phpnamespace DesignPatterns \ Behavioral \ Specification;/*** logic or Specification */class Either extends actspecification {protected $ left; protected $ right; /*** combination of two types ** @ param SpecificationInterface $ left * @ param SpecificationInterface $ right */public function _ construct (SpecificationInterface $ left, SpecificationInterface $ right) {$ this-> left = $ left; $ this-> right = $ right ;} /*** return the logic or evaluation of the two types ** @ param Item $ item ** @ return bool */public function isSatisfiedBy (Item $ item) {return $ this-> left-> isSatisfiedBy ($ item) | $ this-> right-> isSatisfiedBy ($ item );}}

Not. php

<? Phpnamespace DesignPatterns \ Behavioral \ Specification;/*** logical non-Specification */class Not extends actspecification {protected $ spec; /*** input the specified specification in the constructor ** @ param SpecificationInterface $ spec */public function _ construct (SpecificationInterface $ spec) {$ this-> spec = $ spec ;} /*** return the opposite result of the type ** @ param Item $ item ** @ return bool */public function isSatisfiedBy (Item $ item) {return! $ This-> spec-> isSatisfiedBy ($ item );}}

PriceSpecification. php

<? Phpnamespace DesignPatterns \ Behavioral \ Specification;/*** determine whether the price of a given Item is a Specification between the minimum and maximum values */class PriceSpecification extends AbstractSpecification {protected $ maxPrice; protected $ minPrice; /*** set the maximum value ** @ param int $ maxPrice */public function setMaxPrice ($ maxPrice) {$ this-> maxPrice = $ maxPrice ;} /*** set the minimum value ** @ param int $ minPrice */public function setMinPrice ($ minPrice) {$ this-> minPrice = $ minPrice ;}/*** Is the pricing 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 is used to test the 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 (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-& gt; setMaxPrice (50); $ this-& gt; assertTrue ($ not-& gt; isSatisfiedBy ($ item); $ spec-& gt; 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 ));}}

The above content is the specification model of the PHP design pattern series shared by the helper House. I hope this article will help you.

Articles you may be interested in:
  • Php design mode Composite (combination mode)
  • Php design pattern Template (Template pattern)
  • Php design mode Command (Command mode)
  • Php design mode Singleton (Singleton mode)
  • Php design mode Observer (Observer mode)
  • Php design pattern Strategy (policy pattern)
  • Php design mode Interpreter (Interpreter mode)
  • Php design mode Factory (Factory mode)
  • Php design mode Facade (appearance mode)
  • Php design mode Delegation (Delegation mode)

Specification pattern of http://www.bkjia.com/PHPjc/1091112.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1091112.htmlTechArticlePHP design pattern series, specification pattern 1. Pattern Definition pattern is a kind of extension of combination pattern, used more in framework development (project level opening...

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.