php 规格,PHP 设计模式系列之 specification规格模式_PHP

本文详细介绍了PHP中的规格模式,包括如何使用它来实现逻辑与、逻辑或和逻辑非操作。通过`PriceSpecification`类展示了如何限定商品价格范围,并通过`Plus`、`Either`和`Not`类组合不同规格。测试代码验证了这些操作的正确性,展示了规格模式在条件判断和业务逻辑中的应用。
摘要由CSDN通过智能技术生成

Plus.php

left = $left;

$this->right = $right;

}

/**

* 返回两种规格的逻辑与评估

*

* @param Item $item

*

* @return bool

*/

public function isSatisfiedBy(Item $item)

{

return $this->left->isSatisfiedBy($item) && $this->right->isSatisfiedBy($item);

}

}

Either.php

left = $left;

$this->right = $right;

}

/**

* 返回两种规格的逻辑或评估

*

* @param Item $item

*

* @return bool

*/

public function isSatisfiedBy(Item $item)

{

return $this->left->isSatisfiedBy($item) || $this->right->isSatisfiedBy($item);

}

}

Not.php

spec = $spec;

}

/**

* 返回规格的相反结果

*

* @param Item $item

*

* @return bool

*/

public function isSatisfiedBy(Item $item)

{

return !$this->spec->isSatisfiedBy($item);

}

}

PriceSpecification.php

maxPrice = $maxPrice;

}

/**

* 设置最小值

*

* @param int $minPrice

*/

public function setMinPrice($minPrice)

{

$this->minPrice = $minPrice;

}

/**

* 判断给定Item的定价是否在最小值和最大值之间

*

* @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、测试代码

Tests/SpecificationTest.php

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规格模式,希望本文分享能够帮助大家。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值