天天看點

PHP設計模式之組合模式(Composite) 代碼執行個體大全(12)

目的

一組對象與該對象的單個執行個體的處理方式一緻。

示例

  • form類的執行個體包含多個子元素,而它也像單個子元素那樣響應 render() 請求,當調用 render() 方法時,它會曆遍所有的子元素,調用 render() 方法
  • Zend_Config: 一個配置選項樹,每個選項自身就是一個 Zend_Config 對象

UML圖

PHP設計模式之組合模式(Composite) 代碼執行個體大全(12)

★官方PHP進階學習交流社群「點選」管理整理了一些資料,BAT等一線大廠進階知識體系備好(相關學習資料以及筆面試題)以及不限于:分布式架構、高可擴充、高性能、高并發、伺服器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點進階進階幹貨

代碼

  • RenderableInterface.php
<?php

namespace DesignPatterns\Structural\Composite;

interface RenderableInterface
{
    public function render(): string;
}
           
  • Form.php
<?php

namespace DesignPatterns\Structural\Composite;

/**
 * 該組合内的節點必須派生于該元件契約。為了建構成一個元件樹,
 * 此為強制性操作。
 */
class Form implements RenderableInterface
{
    /**
     * @var RenderableInterface[]
     */
    private $elements;

    /**
     * 周遊所有元素,并對他們調用 render() 方法,然後傳回表單的完整
     * 的解析表達。
     *
     * 從外部上看,我們不會看到周遊過程,該表單的操作過程與單一對
     * 象執行個體一樣
     *
     * @return string
     */
    public function render(): string
    {
        $formCode = '<form>';

        foreach ($this->elements as $element) {
            $formCode .= $element->render();
        }

        $formCode .= '</form>';

        return $formCode;
    }

    /**
     * @param RenderableInterface $element
     */
    public function addElement(RenderableInterface $element)
    {
        $this->elements[] = $element;
    }
}
           
  • InputElement.php
<?php

namespace DesignPatterns\Structural\Composite;

class InputElement implements RenderableInterface
{
    public function render(): string
    {
        return '<input type="text" />';
    }
}
           
  • TextElement.php
<?php

namespace DesignPatterns\Structural\Composite;

class TextElement implements RenderableInterface
{
    /**
     * @var string
     */
    private $text;

    public function __construct(string $text)
    {
        $this->text = $text;
    }

    public function render(): string
    {
        return $this->text;
    }
}
           

測試

  • Tests/CompositeTest.php
<?php

namespace DesignPatterns\Structural\Composite\Tests;

use DesignPatterns\Structural\Composite;
use PHPUnit\Framework\TestCase;

class CompositeTest extends TestCase
{
    public function testRender()
    {
        $form = new Composite\Form();
        $form->addElement(new Composite\TextElement('Email:'));
        $form->addElement(new Composite\InputElement());
        $embed = new Composite\Form();
        $embed->addElement(new Composite\TextElement('Password:'));
        $embed->addElement(new Composite\InputElement());
        $form->addElement($embed);

        // 此代碼僅作示例。在實際場景中,現在的網頁浏覽器根本不支援
        // 多表單嵌套,牢記該點非常重要

        $this->assertEquals(
            '<form>Email:<input type="text" /><form>Password:<input type="text" /></form></form>',
            $form->render()
        );
    }
}
           

PHP 網際網路架構師成長之路*「設計模式」終極指南

PHP 網際網路架構師 50K 成長指南+行業問題解決總綱(持續更新)

面試10家公司,收獲9個offer,2020年PHP 面試問題

★如果喜歡我的文章,想與更多資深開發者一起交流學習的話,擷取更多大廠面試相關技術咨詢和指導,歡迎加入我們的群啊,暗号:phpzh(群号碼856460874)。

2020年最新PHP進階教程,全系列!

PHP設計模式之組合模式(Composite) 代碼執行個體大全(12)
内容不錯的話希望大家支援鼓勵下點個贊/喜歡,歡迎一起來交流;另外如果有什麼問題 建議 想看的内容可以在評論提出