<?php
// 每个规范类中都有一个称为 isSatisfiedBy 的方法,
// 方法判断给定的规则是否满足规范从而返回 true 或 false。
// php 技术群:781742505
declare(strict_types=1);
class Item
{
private $price;
public function __construct(float $price)
{
$this->price = $price;
}
public function getPrice(): float
{
return $this->price;
}
}
interface Specification
{
public function isSatisfiedBy(Item $item): bool;
}
class OrSpecification implements Specification
{
private $specifications;
public function __construct(Specification ...$specifications)
{
$this->specifications = $specifications;
}
public function isSatisfiedBy(Item $item): bool
{
foreach ($this->specifications as $specification) {
if ($specification->isSatisfiedBy($item)) {
return true;
}
}
return false;
}
}
class PriceSpecification implements Specification
{
/**
* @var float|null
*/
private $maxPrice;
/**
* @var float|null
*/
private $minPrice;
/**
* @param float $minPrice
* @param float $maxPrice
*/
public function __construct($minPrice, $maxPrice)
{
$this->minPrice = $minPrice;
$this->maxPrice = $maxPrice;
}
public function isSatisfiedBy(Item $item): bool
{
if ($this->maxPrice !== null && $item->getPrice() > $this->maxPrice) {
return false;
}
if ($this->minPrice !== null && $item->getPrice() < $this->minPrice) {
return false;
}
return true;
}
}
class AndSpecification implements Specification
{
/**
* @var Specification[]
*/
private $specifications;
/**
* @param Specification[] ...$specifications
*/
public function __construct(Specification ...$specifications)
{
$this->specifications = $specifications;
}
/**
* if at least one specification is false, return false, else return true.
*/
public function isSatisfiedBy(Item $item): bool
{
foreach ($this->specifications as $specification) {
if (!$specification->isSatisfiedBy($item)) {
return false;
}
}
return true;
}
}
class NotSpecification implements Specification
{
/**
* @var Specification
*/
private $specification;
public function __construct(Specification $specification)
{
$this->specification = $specification;
}
public function isSatisfiedBy(Item $item): bool
{
return !$this->specification->isSatisfiedBy($item);
}
}
$spec1 = new PriceSpecification(50, 99);
$spec2 = new PriceSpecification(101, 200);
$orSpec = new OrSpecification($spec1, $spec2);
$this->assertFalse($orSpec->isSatisfiedBy(new Item(100)));
$this->assertTrue($orSpec->isSatisfiedBy(new Item(51)));
$this->assertTrue($orSpec->isSatisfiedBy(new Item(150)));
$spec1 = new PriceSpecification(50, 100);
$spec2 = new PriceSpecification(80, 200);
$andSpec = new AndSpecification($spec1, $spec2);
$this->assertFalse($andSpec->isSatisfiedBy(new Item(150)));
$this->assertFalse($andSpec->isSatisfiedBy(new Item(1)));
$this->assertFalse($andSpec->isSatisfiedBy(new Item(51)));
$this->assertTrue($andSpec->isSatisfiedBy(new Item(100)));
$spec1 = new PriceSpecification(50, 100);
$notSpec = new NotSpecification($spec1);
$this->assertTrue($notSpec->isSatisfiedBy(new Item(150)));
$this->assertFalse($notSpec->isSatisfiedBy(new Item(50)));
规格模式
最新推荐文章于 2024-01-10 22:36:40 发布