宝哥软件园

PHP设计模式系列之规格规格模式

编辑:宝哥软件园 来源:互联网 时间:2021-09-18

1、模式定义

规格模式是组合模式的一种扩展,在框架性开发中使用较多(项目级开发很少使用),这里做一个简单的介绍。规格模式(规格)可以认为是组合模式的一种扩展。有时项目中某些条件决定了业务逻辑,这些条件就可以抽离出来以某种关系(与、或、非)进行组合,从而灵活地对业务逻辑进行定制。另外,在查询、过滤等应用场合中,通过预定义多个条件,然后使用这些条件的组合来处理查询或过滤,而不是使用逻辑判断语句来处理,可以简化整个实现逻辑。

这里的每个条件就是一个规格,多个规格/条件通过串联的方式以某种逻辑关系形成一个组合式的规格。

2、统一建模语言类图

3、示例代码

Item.php

?phpnamespace设计模式行为规范;类别项目{受保护的$价格;/***项目必须有价格* * @ param int $ price */public function _ _ construct($ price){ $ this-price=$ price;}/***获取物品价格* * @返回int*/public函数getPrice(){ return $ this-price;}}SpecificationInterface.php

?phpnamespace设计模式行为规范;/*** 规格接口*/接口规范接口{/***判断对象是否满足规格* * @参数项目$项目* * @返回bool*/public函数由(项目$ Item)认证;/*** 创建一个逻辑与规格(AND)* * @ param规范接口$ spec */public function plus(规范接口$ spec);/*** 创建一个逻辑或规格(或)* * @ param规范接口$ spec */公共函数任一(规范接口$ spec);/*** 创建一个逻辑非规格(不是)*/公共函数NOT();}抽象规范。服务器端编程语言(Professional Hypertext Preprocessor的缩写)

?phpnamespace设计模式行为规范;/*** 规格抽象类*/抽象类抽象规范实现规范接口{/***检查给定项目是否满足所有规则* * @参数项目$项目* * @返回bool */抽象公共函数由(项目$ Item)认证;/*** 创建一个新的逻辑与规格(与)* * @参数规格接口$规格* * @返回规范接口*/公共函数Plus(规范接口$ spec){ 0返回new Plus($本,$ spec);}/*** 创建一个新的逻辑或组合规格(或)* * @ param规范接口$ spec * * @返回规范接口*/公共函数任一(规格接口$ spec){ 0返回new One(这款,$ spec);}/*** 创建一个新的逻辑非规格(不是)* * @返回规范接口*/公共函数NOT(){ 0返回新NOT($ this);}}Plus.php

?phpnamespace设计模式行为规范;/*** 逻辑与规格(AND)*/class Plus扩展了抽象规范{受保护的$ left受保护的$ right/***在构造函数中传入两种规格* * @ param规范接口$ left * @ param规范接口$ right */public function _ _ construct(规范接口$ left,规范接口$ right){ $ this-left=$ left;$ this-right=$ right;}/*** 返回两种规格的逻辑与评估* * @参数项目$项目* * @返回bool*/public函数由(Item $ Item){ return $ this-left-issed by($ Item)$ this-right-issed by($ Item);} }任一。服务器端编程语言(Professional Hypertext Preprocessor的缩写)

?phpnamespace设计模式行为规范;/*** 逻辑或规格*/class要么扩展抽象规范{受保护的$ left受保护的$ right/***两种规格的组合* * @ param规范接口$ left * @ param规范接口$ right */public function _ _ construct(规范接口$ left,规范接口$ right){ $ this-left=$ left;$ this-right=$ right;}/*** 返回两种规格的逻辑或评估* * @参数项目$项目* * @返回bool*/public函数由(Item $ Item){ return $ this-left-issedby($ Item)| $ this-right-issedby($ Item);} }不是。服务器端编程语言(Professional Hypertext Preprocessor的缩写)

?phpnamespace设计模式行为规范;/*** 逻辑非规格*/class Not扩展抽象规范{受保护的$ spec/***在构造函数中传入指定规格* * @ param规范接口$ spec */public function _ _ construct(规范接口$ spec){ $ this-spec=$ spec;}/*** 返回规格的相反结果* * @参数项目$项目* * @返回bool*/public函数由(项目$ Item){ return!$ this-spec-isSatisfiedBy($ item);}}PriceSpecification.php

?phpnamespace设计模式行为规范;/*** 判断给定项目的价格是否介于最小值和最大值之间的规格*/类价格规格扩展了抽象规格{受保护的$ maxPrice受保护的$ minPrice/***设置最大值** @param int $maxPrice*/public函数setMaxPrice($ MaxPrice){ $ this-MaxPrice=$ MaxPrice;}/*** 设置最小值** @param int $minPrice*/public函数setMinPrice($ minPrice){ $ this-minPrice=$ minPrice;}/*** 判断给定项目的定价是否在最小值和最大值之间* * @参数项目$项目* * @返回bool*/public函数由(项目$ Item){ if(!空($ this-MaxPrice)$ item-GetPrice()$ this-MaxPrice){ return false;}if(!空($ this-minPrice)$ item-getPrice()$ this-minPrice){ return false;}返回true}}4、测试代码

测试/规范测试。服务器端编程语言(Professional Hypertext Preprocessor的缩写)

?phpnamespace设计模式行为规范测试;使用设计模式行为规格价格规格;使用设计模式行为规范项目;/***规格测试用于测试规格模式*/类规格测试扩展 PHPUnit _ Framework _ TestCase {公共函数testSimpleSpecification(){ $ Item=new Item(100);$spec=新价格规格();$ this-assert TRUe($ spec-Issa tified by($ item));$ spec-SetMaxPrice(50);$ this-assertFalse($ spec-Issa tified by($ item));$ spec-SetMaxPrice(150);$ this-assert TRUe($ spec-Issa tified by($ item));$ spec-SetMinPrice(101);$ this-assertFalse($ spec-Issa tified by($ item));$ spec-SetMinPrice(100);$ this-assert TRUe($ spec-Issa tified by($ item));}公共函数testNotSpecification(){ $ Item=new Item(100);$spec=新价格规格();$ not=$ spec-not();$ this-assertFalse($ not-Issa tified by($ item));$ spec-SetMaxPrice(50);$ this-assert TRUe($ not-Issa tified by($ item));$ spec-SetMaxPrice(150);$ this-assertFalse($ not-Issa tified by($ item));$ spec-SetMinPrice(101);$ this-assert TRUe($ not-Issa tified by($ item));$ spec-SetMinPrice(100);$ this-assertFalse($ not-Issa tified by($ item));}公共函数testplussspecification(){ $ spec 1=新价格规格();$spec2=新价格规格();$ plus=$ spec 1-plus($ spec 2);$item=新项目(100);$ this-assertTrue($ plus-Issa tified by($ item));$ spec 1-SetMaxPrice(150);$ spec 2-SetMinPrice(50);$ this-assertTrue($ plus-Issa tified by($ item));$ spec 1-SetMaxPrice(150);$ spec 2-SetMinPrice(101);$ this-assertFalse($ plus-Issa tified by($ item));$ spec 1-SetMaxPrice(99);$ spec 2-SetMinPrice(50);$ this-assertFalse($ plus-Issa tified by($ item));} public function test thessspecification(){ $ spec 1=新价格规格();$spec2=新价格规格();$要么=$ spec 1-要么($ spec 2);$item=新项目(100);$ this-assertTrue($ or-Issa tified by($ item));$ spec 1-SetMaxPrice(150);$ spec 2-SetMaxPrice(150);$ this-assertTrue($ or-Issa tified by($ item));$ spec 1-SetMaxPrice(150);$ spec 2-SetMaxPrice(0);$ this-assertTrue($ or-Issa tified by($ item));$ spec 1-SetMaxPrice(0);$ spec 2-SetMaxPrice(150);$ this-assertTrue($ or-Issa tified by($ item));$ spec 1-SetMaxPrice(99);$ spec 2-SetMaxPrice(99);$ this-assertFalse($ or-Issa tified by($ item));}}以上内容是我们小编给大家分享的服务器端编程语言(专业超文本预处理器的缩写)设计模式系列之规格规格模式,希望本文分享能够帮助大家。

更多资讯
游戏推荐
更多+