国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP 設計模式系列之 specification規格模式

2020-03-22 19:20:51
字體:
來源:轉載
供稿:網友
規格模式是組合模式的一種擴展,在框架性開發中使用較多(項目級開發很少使用),這里做一個簡單的介紹。
規格模式(Specification)可以認為是組合模式的一種擴展。有時項目中某些條件決定了業務邏輯,這些條件就可以抽離出來以某種關系(與、或、非)進行組合,從而靈活地對業務邏輯進行定制。另外,在查詢、過濾等應用場合中,通過預定義多個條件,然后使用這些條件的組合來處理查詢或過濾,而不是使用邏輯判斷語句來處理,可以簡化整個實現邏輯。這里的每個條件就是一個規格,多個規格/條件通過串聯的方式以某種邏輯關系形成一個組合式的規格。2、UML類圖
3、示例代碼Item.phpnamespace DesignPatterns/Behavioral/Specification;html' target='_blank'>class Itemprotected $price;* An item must have a price* @param int $pricepublic function __construct($price)$this- price = $price;* Get the items price* @return intpublic function getPrice()return $this- price;}SpecificationInterface.phpnamespace DesignPatterns/Behavioral/Specification;* 規格接口interface SpecificationInterface* 判斷對象是否滿足規格* @param Item $item* @return boolpublic function isSatisfiedBy(Item $item);* 創建一個邏輯與規格(AND)* @param SpecificationInterface $specpublic function plus(SpecificationInterface $spec);* 創建一個邏輯或規格(OR)* @param SpecificationInterface $specpublic function either(SpecificationInterface $spec);* 創建一個邏輯非規格(NOT)public function not();}AbstractSpecification.phpnamespace DesignPatterns/Behavioral/Specification;* 規格抽象類abstract class AbstractSpecification implements SpecificationInterface* 檢查給定Item是否滿足所有規則* @param Item $item* @return boolabstract public function isSatisfiedBy(Item $item);* 創建一個新的邏輯與規格(AND)* @param SpecificationInterface $spec* @return SpecificationInterfacepublic function plus(SpecificationInterface $spec)return new Plus($this, $spec);* 創建一個新的邏輯或組合規格(OR)* @param SpecificationInterface $spec* @return SpecificationInterfacepublic function either(SpecificationInterface $spec)return new Either($this, $spec);* 創建一個新的邏輯非規格(NOT)* @return SpecificationInterfacepublic function not()return new Not($this);}Plus.phpnamespace DesignPatterns/Behavioral/Specification;* 邏輯與規格(AND)class Plus extends AbstractSpecificationprotected $left;protected $right;* 在構造函數中傳入兩種規格* @param SpecificationInterface $left* @param SpecificationInterface $rightpublic function __construct(SpecificationInterface $left, SpecificationInterface $right)$this- left = $left;$this- right = $right;* 返回兩種規格的邏輯與評估* @param Item $item* @return boolpublic function isSatisfiedBy(Item $item)return $this- left- isSatisfiedBy($item) && $this- right- isSatisfiedBy($item);}Either.phpnamespace DesignPatterns/Behavioral/Specification;* 邏輯或規格class Either extends AbstractSpecificationprotected $left;protected $right;* 兩種規格的組合* @param SpecificationInterface $left* @param SpecificationInterface $rightpublic function __construct(SpecificationInterface $left, SpecificationInterface $right)$this- left = $left;$this- right = $right;* 返回兩種規格的邏輯或評估* @param Item $item* @return boolpublic function isSatisfiedBy(Item $item)return $this- left- isSatisfiedBy($item) || $this- right- isSatisfiedBy($item);}Not.phpnamespace DesignPatterns/Behavioral/Specification;* 邏輯非規格class Not extends AbstractSpecificationprotected $spec;* 在構造函數中傳入指定規格* @param SpecificationInterface $specpublic function __construct(SpecificationInterface $spec)$this- spec = $spec;* 返回規格的相反結果* @param Item $item* @return boolpublic function isSatisfiedBy(Item $item)return !$this- spec- isSatisfiedBy($item);}PriceSpecification.phpnamespace DesignPatterns/Behavioral/Specification;* 判斷給定Item的價格是否介于最小值和最大值之間的規格class PriceSpecification extends AbstractSpecificationprotected $maxPrice;protected $minPrice;* 設置最大值* @param int $maxPricepublic function setMaxPrice($maxPrice)$this- maxPrice = $maxPrice;* 設置最小值* @param int $minPricepublic function setMinPrice($minPrice)$this- minPrice = $minPrice;* 判斷給定Item的定價是否在最小值和最大值之間* @param Item $item* @return boolpublic 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、測試代碼Tests/SpecificationTest.phpnamespace DesignPatterns/Behavioral/Specification/Tests;use DesignPatterns/Behavioral/Specification/PriceSpecification;use DesignPatterns/Behavioral/Specification/Item;* SpecificationTest 用于測試規格模式class SpecificationTest extends /PHPUnit_Framework_TestCasepublic 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));}以上內容是phpstudy小編給大家分享的PHP 設計模式系列之 specification規格模式,希望本文分享能夠幫助大家。PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽中县| 方山县| 庆城县| 多伦县| 丰镇市| 调兵山市| 收藏| 宜宾市| 夏邑县| 祁连县| 嵊州市| 顺平县| 社旗县| 襄樊市| 赫章县| 合山市| 武陟县| 扎囊县| 龙州县| 河津市| 绥德县| 利辛县| 额尔古纳市| 贡觉县| 黄冈市| 富蕴县| 灌南县| 郸城县| 竹北市| 上高县| 都匀市| 桐乡市| 来凤县| 德州市| 泊头市| 甘孜县| 柘荣县| 江城| 桑植县| 潼关县| 杨浦区|