目的
為了封裝呼叫和解耦,
我們有一個呼叫程式和一個接收器, 這種模式使用「命令列」將方法呼叫委托給接收器并且呈現相同的「執行」方法, 因此,呼叫程式只知道呼叫「執行」去處理客戶端的命令,接收器會從呼叫程式中分離出來,
這個模式的另一面是取消方法的 execute (),也就是 undo () ,命令列也可以通過最小量的復制粘貼和依賴組合(不是繼承)被聚合,從而組合成更復雜的命令集,
例子
文本編輯器:所有事件都是可以被解除、堆放,保存的命令,
Symfony2:SF2 命令可以從 CLI 運行,它的建立只需考慮到命令列模式,
大型 CLI 工具使用子程式來分發不同的任務并將它們封裝在「模型」中,每個模塊都可以通過命令列模式實作(例如:vagrant),
UML圖

★官方PHP高級學習交流社群「點擊」管理整理了一些資料,BAT等一線大廠進階知識體系備好(相關學習資料以及筆面試題)以及不限于:分布式架構、高可擴展、高性能、高并發、服務器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階干貨
代碼
- CommandInterface.php
<?php
namespace DesignPatterns\Behavioral\Command;
interface CommandInterface
{
/**
* 這是在命令列模式中很重要的方法,
* 這個接收者會被載入構造器
*/
public function execute();
}
- HelloCommand.php
<?php
namespace DesignPatterns\Behavioral\Command;
/**
* 這個具體命令,在接收器上呼叫 "print" ,
* 但是外部呼叫者只知道,這個是否可以執行,
*/
class HelloCommand implements CommandInterface
{
/**
* @var Receiver
*/
private $output;
/**
* 每個具體的命令都來自于不同的接收者,
* 這個可以是一個或者多個接收者,但是引數里必須是可以被執行的命令,
*
* @param Receiver $console
*/
public function __construct(Receiver $console)
{
$this->output = $console;
}
/**
* 執行和輸出 "Hello World".
*/
public function execute()
{
// 有時候,這里沒有接收者,并且這個命令執行所有作業,
$this->output->write('Hello World');
}
}
- Receiver.php
<?php
namespace DesignPatterns\Behavioral\Command;
/**
* 接收方是特定的服務,有自己的 contract ,只能是具體的實體,
*/
class Receiver
{
/**
* @var bool
*/
private $enableDate = false;
/**
* @var string[]
*/
private $output = [];
/**
* @param string $str
*/
public function write(string $str)
{
if ($this->enableDate) {
$str .= ' ['.date('Y-m-d').']';
}
$this->output[] = $str;
}
public function getOutput(): string
{
return join("\n", $this->output);
}
/**
* 可以顯示訊息的時間
*/
public function enableDate()
{
$this->enableDate = true;
}
/**
* 禁止顯示訊息的時間
*/
public function disableDate()
{
$this->enableDate = false;
}
}
- Invoker.php
<?php
namespace DesignPatterns\Behavioral\Command;
/**
*呼叫者使用這種命令,
* 比例 : 一個在 SF2 中的應用
*/
class Invoker
{
/**
* @var CommandInterface
*/
private $command;
/**
* 在這種呼叫者中,我們發現,訂閱命令也是這種方法
* 還包括:堆疊、串列、集合等等
*
* @param CommandInterface $cmd
*/
public function setCommand(CommandInterface $cmd)
{
$this->command = $cmd;
}
/**
* 執行這個命令;
* 呼叫者也是用這個命令,
*/
public function run()
{
$this->command->execute();
}
}
測驗
- Tests/CommandTest.php
<?php
namespace DesignPatterns\Behavioral\Command\Tests;
use DesignPatterns\Behavioral\Command\HelloCommand;
use DesignPatterns\Behavioral\Command\Invoker;
use DesignPatterns\Behavioral\Command\Receiver;
use PHPUnit\Framework\TestCase;
class CommandTest extends TestCase
{
public function testInvocation()
{
$invoker = new Invoker();
$receiver = new Receiver();
$invoker->setCommand(new HelloCommand($receiver));
$invoker->run();
$this->assertEquals('Hello World', $receiver->getOutput());
}
}
PHP 互聯網架構師成長之路*「設計模式」終極指南
PHP 互聯網架構師 50K 成長指南+行業問題解決總綱(持續更新)
面試10家公司,識訓9個offer,2020年PHP 面試問題
★如果喜歡我的文章,想與更多資深開發者一起交流學習的話,獲取更多大廠面試相關技術咨詢和指導,歡迎加入我們的群啊,暗號:phpzh(君羊號碼856460874),
2020年最新PHP進階教程,全系列!

內容不錯的話希望大家支持鼓勵下點個贊/喜歡,歡迎一起來交流;另外如果有什么問題 建議 想看的內容可以在評論提出
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/82306.html
標籤:其他
