目錄
PHP 设计模式系列之 specification规格模式,specification模式
您可能感兴趣的文章:
首頁 後端開發 php教程 PHP 设计模式系列之 specification规格模式,specification模式_PHP教程

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

Jul 12, 2016 am 09:00 AM
模式

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

1、模式定义

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

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

2、UML类图


3、示例代码

Item.php

<&#63;php
namespace DesignPatterns\Behavioral\Specification;
class Item
{
protected $price;

/**
* An item must have a price
*
* @param int $price
*/
public function __construct($price)
{
$this->price = $price;
}
/**
* Get the items price
*
* @return int
*/
public function getPrice()
{
return $this->price;
}
}
登入後複製

SpecificationInterface.php

<&#63;php
namespace DesignPatterns\Behavioral\Specification;
/**
* 规格接口
*/
interface SpecificationInterface
{
/**
* 判断对象是否满足规格
*
* @param Item $item
*
* @return bool
*/
public function isSatisfiedBy(Item $item);

/**
* 创建一个逻辑与规格(AND)
*
* @param SpecificationInterface $spec
*/
public function plus(SpecificationInterface $spec);
/**
* 创建一个逻辑或规格(OR)
*
* @param SpecificationInterface $spec
*/
public function either(SpecificationInterface $spec);

/**
* 创建一个逻辑非规格(NOT)
*/
public function not();
}
登入後複製

AbstractSpecification.php

<&#63;php
namespace DesignPatterns\Behavioral\Specification;

/**
* 规格抽象类
*/
abstract class AbstractSpecification implements SpecificationInterface
{
/**
* 检查给定Item是否满足所有规则
*
* @param Item $item
*
* @return bool
*/
abstract public function isSatisfiedBy(Item $item);
/**
* 创建一个新的逻辑与规格(AND)
*
* @param SpecificationInterface $spec
*
* @return SpecificationInterface
*/
public function plus(SpecificationInterface $spec)
{
return new Plus($this, $spec);
}
/**
* 创建一个新的逻辑或组合规格(OR)
*
* @param SpecificationInterface $spec
*
* @return SpecificationInterface
*/
public function either(SpecificationInterface $spec)
{
return new Either($this, $spec);
}
/**
* 创建一个新的逻辑非规格(NOT)
*
* @return SpecificationInterface
*/
public function not()
{
return new Not($this);
}
}
登入後複製

Plus.php

<&#63;php
namespace DesignPatterns\Behavioral\Specification;
/**
* 逻辑与规格(AND)
*/
class Plus extends AbstractSpecification
{
protected $left;
protected $right;

/**
* 在构造函数中传入两种规格
*
* @param SpecificationInterface $left
* @param SpecificationInterface $right
*/
public function __construct(SpecificationInterface $left, SpecificationInterface $right)
{
$this->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

<&#63;php
namespace DesignPatterns\Behavioral\Specification;

/**
* 逻辑或规格
*/
class Either extends AbstractSpecification
{

protected $left;
protected $right;
/**
* 两种规格的组合
*
* @param SpecificationInterface $left
* @param SpecificationInterface $right
*/
public function __construct(SpecificationInterface $left, SpecificationInterface $right)
{
$this->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

<&#63;php
namespace DesignPatterns\Behavioral\Specification;

/**
* 逻辑非规格
*/
class Not extends AbstractSpecification
{
protected $spec;
/**
* 在构造函数中传入指定规格
*
* @param SpecificationInterface $spec
*/
public function __construct(SpecificationInterface $spec)
{
$this->spec = $spec;
}
/**
* 返回规格的相反结果
*
* @param Item $item
*
* @return bool
*/
public function isSatisfiedBy(Item $item)
{
return !$this->spec->isSatisfiedBy($item);
}
}
登入後複製

PriceSpecification.php

<&#63;php
namespace DesignPatterns\Behavioral\Specification;

/**
* 判断给定Item的价格是否介于最小值和最大值之间的规格
*/
class PriceSpecification extends AbstractSpecification
{
protected $maxPrice;
protected $minPrice;
/**
* 设置最大值
*
* @param int $maxPrice
*/
public function setMaxPrice($maxPrice)
{
$this->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

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

您可能感兴趣的文章:

  • php设计模式 Composite (组合模式)
  • php设计模式 Template (模板模式)
  • php设计模式 Command(命令模式)
  • php设计模式 Singleton(单例模式)
  • php设计模式 Observer(观察者模式)
  • php设计模式 Strategy(策略模式)
  • php设计模式 Interpreter(解释器模式)
  • php设计模式 Factory(工厂模式)
  • php设计模式 Facade(外观模式)
  • php设计模式 Delegation(委托模式)

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1091112.htmlTechArticlePHP 设计模式系列之 specification规格模式,specification模式 1、模式定义 规格模式是组合模式的一种扩展,在框架性开发中使用较多(项目级开...
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1319
25
PHP教程
1269
29
C# 教程
1248
24
微信的免打擾模式有什麼作用 微信的免打擾模式有什麼作用 Feb 23, 2024 pm 10:48 PM

微信勿擾模式什麼意思如今,隨著智慧型手機的普及和行動網路的快速發展,社群媒體平台已成為人們日常生活中不可或缺的一部分。而微信作為國內最受歡迎的社群媒體平台之一,幾乎每個人都有一個微信帳號。我們可以透過微信與朋友、家人、同事進行即時溝通,分享生活中的點滴,了解彼此的近況。然而,在這個時代,我們也不可避免地面臨資訊過載和隱私洩漏的問題,特別是對於那些需要專注或

iPhone上的睡眠模式有何用途? iPhone上的睡眠模式有何用途? Nov 04, 2023 am 11:13 AM

長期以來,iOS設備一直能夠使用「健康」應用程式追蹤您的睡眠模式等。但是,當您在睡覺時被通知打擾時,這不是很煩人嗎?這些通知可能無關緊要,因此在此過程中會擾亂您的睡眠模式。雖然免打擾模式是避免睡覺時分心的好方法,但它可能會導致您錯過夜間收到的重要電話和訊息。值得慶幸的是,這就是睡眠模式的用武之地。讓我們了解更多關於它以及如何在iPhone上使用它的資訊。睡眠模式在iPhone上有什麼作用睡眠模式是iOS中專用的專注模式,會根據你在「健康」App中的睡眠定時自動啟動。它可以幫助您設定鬧鐘,然後可以

請勿打擾模式在iPhone中不起作用:修復 請勿打擾模式在iPhone中不起作用:修復 Apr 24, 2024 pm 04:50 PM

即使在「請勿打擾」模式下接聽電話也可能是一種非常煩人的體驗。顧名思義,請勿打擾模式可關閉來自郵件、訊息等的所有來電通知和警報。您可以按照這些解決方案集進行修復。修復1–啟用對焦模式在手機上啟用對焦模式。步驟1–從頂部向下滑動以存取控制中心。步驟2–接下來,在手機上啟用「對焦模式」。專注模式可在手機上啟用「請勿打擾」模式。它不會讓您的手機上出現任何來電提醒。修復2–更改對焦模式設定如果對焦模式設定中存在一些問題,則應進行修復。步驟1–打開您的iPhone設定視窗。步驟2–接下來,開啟「對焦」模式設

epc+o模式是什麼意思 epc+o模式是什麼意思 Nov 09, 2022 am 10:54 AM

epc+o模式是指設計、採購等等為一體的總承包框架,它是在epc裡面引申出來的一些運營環節;即在建設期內時,總承包商除了要去承擔傳統意義上的設計任務以外,還要去包攬在營運期間內的所有維護任務。此模式可以大幅提高許多專案的營運效率,也可以迅速降低營運成本。

iPhone 15 Pro:如何擺脫狀態列中的靜音模式符號 iPhone 15 Pro:如何擺脫狀態列中的靜音模式符號 Sep 24, 2023 pm 10:01 PM

在iPhone15Pro和iPhone15ProMax型號上,Apple推出了一個實體可編程的動作按鈕,取代了音量按鈕上方的傳統響鈴/靜音開關。可以對操作按鈕進行編程以執行幾種不同的功能,但是在靜音和響鈴模式之間切換的能力並沒有消失。預設情況下,長按一次操作按鈕將使裝置靜音,按鈕的觸覺回饋將發出三個脈衝。兩款iPhone15Pro機型在狀態欄中的時間旁邊都會顯示一個劃掉的鈴鐺符號,表示靜音/靜音模式已激活,並且它將一直保持到您再次長按“操作”按鈕取消設備靜音。如果您傾向於將iPhone置於靜音模

iOS 17中的待機模式使用指南 iOS 17中的待機模式使用指南 Aug 22, 2023 pm 04:01 PM

待機模式即將透過iOS17進入iPhone,本指南旨在向您展示如何在iPhone上使用此功能。待機模式是一項突破性功能,可將iPhone轉換為動態、始終開啟的智慧顯示器。當您的iPhone在充電過程中水平側放時,它會啟動待機模式。此模式精美地展示了大量有用的小部件,包括但不限於當前時間、當地天氣更新、您喜歡的照片的幻燈片,甚至是音樂播放控制。此模式的一個顯著優點是它能夠顯示通知,允許用戶查看和參與通知,而無需完全喚醒他們的iPhone。如何使用待機模式要使待機模式正常運行,iPhone必須運行i

如何啟用「記事本++深色模式」和「記事本++深色主題」? 如何啟用「記事本++深色模式」和「記事本++深色主題」? Oct 27, 2023 pm 11:17 PM

記事本++暗模式v8.0沒有參數,Notepad++是最有用的文字編輯器。在Windows10上執行的每個應用程式都支援暗模式。您可以命名網頁瀏覽器,例如Chrome、Firefox和MicrosoftEdge。如果您在記事本++上工作,預設的白色背景可能會傷害您的眼睛。開發人員已將暗模式加入到版本8的Notepad++中,這是開啟它的方法。為Windows11/10啟用記事本++暗模式啟動記事本++點選「設定」&gt;「首選項」&gt;「暗模式」選擇「啟用深色模式」重新啟動記

在 Windows 10/11 上如何離開 S 模式 在 Windows 10/11 上如何離開 S 模式 Aug 03, 2023 pm 08:17 PM

S模式下的視窗旨在透過僅允許從Microsoft應用商店安裝應用程式來提供增強的安全性和效能。雖然此功能有助於防止惡意軟體和確保安全的運算環境,但它可能會限制想要從MicrosoftStore以外的來源安裝應用程式的使用者。如果您發現自己處於這種情況並不斷問自己如何在Windows10/11中切換出S模式,那麼您來對地方了,因為我們將引導您完成如何使用兩種不同的方法在Windows10/11中切換出S模式的步驟,確保您可以享受從您選擇的任何地方安裝應用程式的自由。了解如何在Windows中切換出S模式將

See all articles