亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > 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教程

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
成人www视频在线观看| 亚洲成色777777女色窝| 欧美国产精品人人做人人爱| 国产成人一区三区| 日韩中文字幕网址| 亚洲专区中文字幕| 亚洲va国产va天堂va久久| 91九色单男在线观看| 日韩中文字幕av| 国产成人精品久久二区二区91| 久久久av亚洲男天堂| 久久久久五月天| 俺也去精品视频在线观看| 国产成人精品免高潮费视频| 国产91网红主播在线观看| 91成人在线视频| 国a精品视频大全| 97久久精品人搡人人玩| 亚洲性69xxxbbb| 日韩精品欧美国产精品忘忧草| 国产精品久久久久久久久免费| 国产99久久精品一区二区| 欧美一区二区.| 欧美国产日韩在线| 在线视频日本亚洲性| 亚洲天堂免费视频| 亚洲精品久久久一区二区三区| 亚洲欧美精品伊人久久| 亚洲电影在线看| 91精品国产色综合久久不卡98| 国产xxx69麻豆国语对白| 国产精品大片wwwwww| 日韩免费观看高清| 国产精品成人久久久久| 亚洲第一二三四五区| 欧美黑人狂野猛交老妇| 久久香蕉国产线看观看av| 精品久久久一区| 日本精品一区二区三区在线播放视频| 精品国产一区二区三区久久| 日韩av电影中文字幕| 国产精品久久一区| 亚洲变态欧美另类捆绑| 国产一区二区三区四区福利| 日韩高清av在线| 久久国产精品久久久久| 亚洲男人av在线| 欧美电影免费在线观看| 久青草国产97香蕉在线视频| 日韩美女激情视频| 91精品国产综合久久香蕉的用户体验| 国产精品扒开腿做| 日韩欧美福利视频| 国产91av在线| 亚洲xxxx做受欧美| 91国产视频在线播放| 国产精品中文在线| 午夜精品www| 日韩在线一区二区三区免费视频| 欧美亚洲视频在线观看| 在线a欧美视频| 欧美日韩久久久久| 51ⅴ精品国产91久久久久久| 日韩精品久久久久久久玫瑰园| 国产午夜精品视频免费不卡69堂| 亚洲欧洲av一区二区| 成人a在线观看| 久久精品男人天堂| 中文欧美在线视频| 黄网站色欧美视频| 国产成人精品一区| 国产精品自产拍高潮在线观看| 两个人的视频www国产精品| 一本色道久久综合亚洲精品小说| 精品亚洲va在线va天堂资源站| 欧美日韩在线影院| 欧美黄色免费网站| 亚洲天堂日韩电影| 成人啪啪免费看| 欧美精品一区二区三区国产精品| 欧亚精品中文字幕| 日韩欧美亚洲成人| 4438全国亚洲精品在线观看视频| 成人欧美一区二区三区黑人孕妇| 久久久av电影| 成人h猎奇视频网站| 一区二区av在线| 国产精品欧美日韩| 精品小视频在线| 欧美精品在线极品| 欧美大码xxxx| 欧美猛交ⅹxxx乱大交视频| 97在线视频免费观看| 久久资源免费视频| 国产成人小视频在线观看| 国产精品自拍小视频| 俺去了亚洲欧美日韩| 青青青国产精品一区二区| 久久精品中文字幕电影| 91久久久亚洲精品| 欧美日韩裸体免费视频| 久久久久国色av免费观看性色| 伊人久久综合97精品| 亚洲欧美日韩爽爽影院| 91av在线播放| www.欧美三级电影.com| 国产精品午夜国产小视频| 亚洲国产欧美日韩精品| 日韩在线观看视频免费| 久久国产精品影视| 亚洲久久久久久久久久久| 91热精品视频| 日本欧美一二三区| 日韩欧美一区二区三区| 国产成人亚洲综合91精品| 久久精品男人天堂| 欧美性69xxxx肥| 亚洲精品不卡在线| 久久久久久一区二区三区| 91社影院在线观看| 欧美一区二区三区四区在线| 国产精品久久电影观看| 91中文精品字幕在线视频| 91久久在线播放| 精品久久中文字幕久久av| 亚洲欧美国产日韩中文字幕| 美女黄色丝袜一区| 91久久在线播放| 最近2019年好看中文字幕视频| 久久久久免费精品国产| 亚洲欧洲日产国码av系列天堂| 亚洲成在人线av| 色婷婷久久av| 亚洲欧美另类国产| 欧美激情videos| 成人午夜在线观看| 福利视频导航一区| 成人欧美在线视频| 91久久精品在线| 91亚洲va在线va天堂va国| 日韩在线视频免费观看| 亚洲跨种族黑人xxx| 亚洲成人xxx| 麻豆国产精品va在线观看不卡| 亚洲999一在线观看www| 久久视频精品在线| 夜夜躁日日躁狠狠久久88av| 亚洲国产精品嫩草影院久久| 国产精品久久久久久av下载红粉| 中文精品99久久国产香蕉| 爽爽爽爽爽爽爽成人免费观看| 91成人免费观看网站| 米奇精品一区二区三区在线观看| 欧美区在线播放| 久久久久久欧美| 国产精品久久久久久久电影| 国产精品九九久久久久久久| 欧美精品videos性欧美| 亚洲综合中文字幕在线| 成人免费福利在线| 97激碰免费视频| 奇米四色中文综合久久| 欧美激情日韩图片| 最近免费中文字幕视频2019|