婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 淺談PHP設計模式之門面模式Facade

淺談PHP設計模式之門面模式Facade

熱門標簽:福州人工外呼系統(tǒng)哪家強 新河科技智能外呼系統(tǒng)怎么樣 地圖標注平臺怎么給錢注冊 安裝電銷外呼系統(tǒng) 常州地圖標注服務商 百度商鋪地圖標注 注冊400電話申請 衡水外呼系統(tǒng)平臺 釘釘打卡地圖標注

目的

Facade通過嵌入多個(當然,有時只有一個)接口來解耦訪客與子系統(tǒng),同時也為了降低復雜度。

  • Facade 不會禁止你訪問子系統(tǒng)
  • 你可以(應該)為一個子系統(tǒng)提供多個 Facade

因此一個好的 Facade 里面不會有 new 。如果每個方法里都要構(gòu)造多個對象,那么它就不是 Facade,而是生成器或者[抽象|靜態(tài)|簡單] 工廠 [方法]。

優(yōu)秀的 Facade 不會有 new,并且構(gòu)造函數(shù)參數(shù)是接口類型的。如果你需要創(chuàng)建一個新實例,則在參數(shù)中傳入一個工廠對象。

UML

代碼

Facade.php

?php

namespace DesignPatterns\Structural\Facade;

class Facade
{
    /**
    * @var OsInterface
    * 定義操作系統(tǒng)接口變量。
    */
    private $os;

    /**
    * @var BiosInterface
    * 定義基礎輸入輸出系統(tǒng)接口變量。
    */
    private $bios;

    /**
    * @param BiosInterface $bios
    * @param OsInterface $os
    * 傳入基礎輸入輸出系統(tǒng)接口對象 $bios 。
    * 傳入操作系統(tǒng)接口對象 $os 。
    */
    public function __construct(BiosInterface $bios, OsInterface $os)
    {
        $this->bios = $bios;
        $this->os = $os;
    }

    /**
    * 構(gòu)建基礎輸入輸出系統(tǒng)執(zhí)行啟動方法。
    */
    public function turnOn()
    {
        $this->bios->execute();
        $this->bios->waitForKeyPress();
        $this->bios->launch($this->os);
    }

    /**
    * 構(gòu)建系統(tǒng)關閉方法。
    */
    public function turnOff()
    {
        $this->os->halt();
        $this->bios->powerDown();
    }
}

OsInterface.php

?php

namespace DesignPatterns\Structural\Facade;

/**
* 創(chuàng)建操作系統(tǒng)接口類 OsInterface 。
*/
interface OsInterface
{
    /**
    * 聲明關機方法。
    */
    public function halt();

    /** 
    * 聲明獲取名稱方法,返回字符串格式數(shù)據(jù)。
    */
    public function getName(): string;
}

BiosInterface.php

?php

namespace DesignPatterns\Structural\Facade;

/**
* 創(chuàng)建基礎輸入輸出系統(tǒng)接口類 BiosInterface 。
*/
interface  BiosInterface
{
    /**
    * 聲明執(zhí)行方法。
    */
    public function execute();

    /**
    * 聲明等待密碼輸入方法
    */
    public function waitForKeyPress();

    /**
    * 聲明登錄方法。
    */
    public function launch(OsInterface $os);

    /**
    * 聲明關機方法。
    */
    public function powerDown();
}

測試

Tests/FacadeTest.php

?php

namespace DesignPatterns\Structural\Facade\Tests;

use DesignPatterns\Structural\Facade\Facade;
use DesignPatterns\Structural\Facade\OsInterface;
use PHPUnit\Framework\TestCase;

/**
* 創(chuàng)建自動化測試單元 FacadeTest 。
*/
class FacadeTest extends TestCase
{
    public function testComputerOn()
    {
        /** @var OsInterface|\PHPUnit_Framework_MockObject_MockObject $os */
        $os = $this->createMock('DesignPatterns\Structural\Facade\OsInterface');

        $os->method('getName')
            ->will($this->returnValue('Linux'));

        $bios = $this->getMockBuilder('DesignPatterns\Structural\Facade\BiosInterface')
            ->setMethods(['launch', 'execute', 'waitForKeyPress'])
            ->disableAutoload()
            ->getMock();

        $bios->expects($this->once())
            ->method('launch')
            ->with($os);

        $facade = new Facade($bios, $os);

        // 門面接口很簡單。
        $facade->turnOn();

        // 但你也可以訪問底層組件。
        $this->assertEquals('Linux', $os->getName());
    }
}

以上就是淺談PHP設計模式之門面模式Facade的詳細內(nèi)容,更多關于PHP設計模式之門面模式Facade的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • PHP設計模式(觀察者模式)
  • 淺談PHP設計模式之對象池模式Pool
  • 詳解PHP設計模式之依賴注入模式
  • PHP設計模式之迭代器模式的使用
  • 詳解PHP八大設計模式
  • PHP設計模式之原型模式示例詳解
  • PHP設計模式之命令模式示例詳解
  • PHP八大設計模式案例詳解

標簽:柳州 遼陽 鶴崗 白城 六安 唐山 克拉瑪依 鷹潭

巨人網(wǎng)絡通訊聲明:本文標題《淺談PHP設計模式之門面模式Facade》,本文關鍵詞  淺談,PHP,設計模式,之,門面,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關。
  • 相關文章
  • 下面列出與本文章《淺談PHP設計模式之門面模式Facade》相關的同類信息!
  • 本頁收集關于淺談PHP設計模式之門面模式Facade的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 淮南市| 句容市| 昌宁县| 岑溪市| 淮安市| 江口县| 秭归县| 新河县| 桐梓县| 万年县| 永德县| 柳江县| 天祝| 通江县| 莒南县| 阿尔山市| 台中县| 五家渠市| 凌海市| 太白县| 仪陇县| 宕昌县| 宜黄县| 连城县| 临沂市| 海安县| 二连浩特市| 阿拉善左旗| 南澳县| 林州市| 新干县| 宾阳县| 普兰店市| 勐海县| 黄冈市| 北海市| 鄂尔多斯市| 禹城市| 呼玛县| 鹤山市| 漳平市|