无为清净楼资源网 Design By www.qnjia.com
1、模式定义
规格模式是组合模式的一种扩展,在框架性开发中使用较多(项目级开发很少使用),这里做一个简单的介绍。
规格模式(Specification)可以认为是组合模式的一种扩展。有时项目中某些条件决定了业务逻辑,这些条件就可以抽离出来以某种关系(与、或、非)进行组合,从而灵活地对业务逻辑进行定制。另外,在查询、过滤等应用场合中,通过预定义多个条件,然后使用这些条件的组合来处理查询或过滤,而不是使用逻辑判断语句来处理,可以简化整个实现逻辑。
这里的每个条件就是一个规格,多个规格/条件通过串联的方式以某种逻辑关系形成一个组合式的规格。
2、UML类图
3、示例代码
Item.php
<"htmlcode"><"htmlcode"><"htmlcode"><"htmlcode"><"htmlcode"><"htmlcode"><"htmlcode"><?php namespace DesignPatterns\Behavioral\Specification\Tests; use DesignPatterns\Behavioral\Specification\PriceSpecification; use DesignPatterns\Behavioral\Specification\Item; /** * SpecificationTest 用于测试规格模式 */ 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->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)); } }以上内容是小编给大家分享的PHP 设计模式系列之 specification规格模式,希望本文分享能够帮助大家。
无为清净楼资源网 Design By www.qnjia.com广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com无为清净楼资源网 Design By www.qnjia.com暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年09月24日
2024年09月24日
- 国风大师纯音系列《古筝仙子 常静演奏 十指弹一》1CD[FLAC][919MB]
- DasKapital-OneMustHaveChaosInsidetoGiveBirthtoaDancingStar(2024)[24-44.1]WAV
- 王菲.1999-《只爱陌生人》日本东芝版[低速原抓WAV+CUE]
- 张小英1987-金奖金曲第一集[新加坡版][WAV+CUE]
- 鹿晗.2015-重启【海蝶】【FLAC分轨】
- 张芸京.2012-小女孩【金牌大风】【WAV+CUE】
- 余天.2002-雄狮魅力金曲2CD(南方金点系列)【南方】【WAV+CUE】
- 银霞.1982-《结·SAYYESMYBOY》台湾复刻版[WAV+CUE]
- 梁汉文1993-火热动感[引进版][WAV+CUE]
- 梁汉文2008-EDMONDHITS48[香港]3CD[WAV+CUE]
- 国风大师纯音系列《童丽-[渭城曲]》1CD[WAV分轨][593.5MB]
- 国风大师纯音系列《许菱子《烧筝2》古筝曲集》1CD[WAV分轨][1.3GB]
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】