淺談PHP設計模式之門面模式Facade
目的
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)關(guān)閉方法。 */ public function turnOff() { $this->os->halt(); $this->bios->powerDown(); } }
OsInterface.php
<?php namespace DesignPatterns\Structural\Facade; /** * 創(chuàng)建操作系統(tǒng)接口類 OsInterface 。 */ interface OsInterface { /** * 聲明關(guān)機方法。 */ 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); /** * 聲明關(guān)機方法。 */ 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)容,更多關(guān)于PHP設計模式之門面模式Facade的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
PHP中mysqli_affected_rows作用行數(shù)返回值分析
這篇文章主要介紹了PHP中mysqli_affected_rows作用行數(shù)返回值,實例分析了普通模式與oop模式的用法,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12PHP與MySQL開發(fā)中頁面亂碼的產(chǎn)生與解決
一般來說,亂碼的出現(xiàn)有2種原因,首先是由于編碼(charset)設置錯誤,導致瀏覽器以錯誤的編碼來解析,從而出現(xiàn)了滿屏亂七八糟的“天書”,其次是文件被以錯誤的編碼打開,然后保存,比如一個文本文件原先是GB2312編碼的,卻以UTF-8編碼打開再保存。要解決上述亂碼問題,首先需要知道開發(fā)中哪些環(huán)節(jié)涉及到了編碼:2008-03-03PHP+jquery實時顯示網(wǎng)站在線人數(shù)的方法
這篇文章主要介紹了PHP+jquery實時顯示網(wǎng)站在線人數(shù)的方法,較為詳細的分析了實時顯示在線人數(shù)的原理與代碼實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01