目的
動態地為類的實體添加功能
例子
-
Zend Framework: Zend_Form_Element 實體的裝飾者
-
Web Service層:REST服務的JSON與XML裝飾器(當然,在此只能使用其中的一種)
UML圖

★官方PHP高級學習交流社群「點擊」管理整理了一些資料,BAT等一線大廠進階知識體系備好(相關學習資料以及筆面試題)以及不限于:分布式架構、高可擴展、高性能、高并發、服務器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階干貨
代碼
- RenderableInterface.php
<?php
namespace DesignPatterns\Structural\Decorator;
/**
* 創建渲染介面,
* 這里的裝飾方法 renderData() 回傳的是字串格式資料,
*/
interface RenderableInterface
{
public function renderData(): string;
}
- Webservice.php
<?php
namespace DesignPatterns\Structural\Decorator;
/**
* 創建 Webservice 服務類實作 RenderableInterface,
* 該類將在后面為裝飾者實作資料的輸入,
*/
class Webservice implements RenderableInterface
{
/**
* @var string
*/
private $data;
/**
* 傳入字串格式資料,
*/
public function __construct(string $data)
{
$this->data = https://www.cnblogs.com/phpyu/p/$data;
}
/**
* 實作 RenderableInterface 渲染介面中的 renderData() 方法,
* 回傳傳入的資料,
*/
public function renderData(): string
{
return $this->data;
}
}
- RendererDecorator.php
<?php
namespace DesignPatterns\Structural\Decorator;
/**
* 裝飾者必須實作渲染介面類 RenderableInterface 契約,這是該設計
* 模式的關鍵點,否則,這將不是一個裝飾者而只是一個自欺欺人的包
* 裝,
*
* 創建抽象類 RendererDecorator (渲染器裝飾者)實作渲染介面,
*/
abstract class RendererDecorator implements RenderableInterface
{
/**
* @var RenderableInterface
* 定義渲染介面變數,
*/
protected $wrapped;
/**
* @param RenderableInterface $renderer
* 傳入渲染介面類物件 $renderer,
*/
public function __construct(RenderableInterface $renderer)
{
$this->wrapped = $renderer;
}
}
- XmlRenderer.php
<?php
namespace DesignPatterns\Structural\Decorator;
/**
* 創建 Xml 修飾者并繼承抽象類 RendererDecorator ,
*/
class XmlRenderer extends RendererDecorator
{
/**
* 對傳入的渲染介面物件進行處理,生成 DOM 資料檔案,
*/
public function renderData(): string
{
$doc = new \DOMDocument();
$data = https://www.cnblogs.com/phpyu/p/$this->wrapped->renderData();
$doc->appendChild($doc->createElement('content', $data));
return $doc->saveXML();
}
}
- JsonRenderer.php
<?php
namespace DesignPatterns\Structural\Decorator;
/**
* 創建 Json 修飾者并繼承抽象類 RendererDecorator ,
*/
class JsonRenderer extends RendererDecorator
{
/**
* 對傳入的渲染介面物件進行處理,生成 JSON 資料,
*/
public function renderData(): string
{
return json_encode($this->wrapped->renderData());
}
}
測驗
- Tests/DecoratorTest.php
<?php
namespace DesignPatterns\Structural\Decorator\Tests;
use DesignPatterns\Structural\Decorator;
use PHPUnit\Framework\TestCase;
/**
* 創建自動化測驗單元 DecoratorTest ,
*/
class DecoratorTest extends TestCase
{
/**
* @var Decorator\Webservice
*/
private $service;
/**
* 傳入字串 'foobar' ,
*/
protected function setUp()
{
$this->service = new Decorator\Webservice('foobar');
}
/**
* 測驗 JSON 裝飾者,
* 這里的 assertEquals 是為了判斷回傳的結果是否符合預期,
*/
public function testJsonDecorator()
{
$service = new Decorator\JsonRenderer($this->service);
$this->assertEquals('"foobar"', $service->renderData());
}
/**
* 測驗 Xml 裝飾者,
*/
public function testXmlDecorator()
{
$service = new Decorator\XmlRenderer($this->service);
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><content>foobar</content>', $service->renderData());
}
}
PHP 互聯網架構師成長之路*「設計模式」終極指南
PHP 互聯網架構師 50K 成長指南+行業問題解決總綱(持續更新)
面試10家公司,識訓9個offer,2020年PHP 面試問題
★如果喜歡我的文章,想與更多資深開發者一起交流學習的話,獲取更多大廠面試相關技術咨詢和指導,歡迎加入我們的群啊,暗號:phpzh(君羊號碼856460874),
2020年最新PHP進階教程,全系列!

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