淺談PHP設(shè)計(jì)模式之門面模式Facade
Facade通過(guò)嵌入多個(gè)(當(dāng)然,有時(shí)只有一個(gè))接口來(lái)解耦訪客與子系統(tǒng),同時(shí)也為了降低復(fù)雜度。
Facade 不會(huì)禁止你訪問子系統(tǒng) 你可以(應(yīng)該)為一個(gè)子系統(tǒng)提供多個(gè) Facade因此一個(gè)好的 Facade 里面不會(huì)有 new 。如果每個(gè)方法里都要構(gòu)造多個(gè)對(duì)象,那么它就不是 Facade,而是生成器或者[抽象|靜態(tài)|簡(jiǎn)單] 工廠 [方法]。
優(yōu)秀的 Facade 不會(huì)有 new,并且構(gòu)造函數(shù)參數(shù)是接口類型的。如果你需要?jiǎng)?chuàng)建一個(gè)新實(shí)例,則在參數(shù)中傳入一個(gè)工廠對(duì)象。
UMLFacade.php
<?phpnamespace DesignPatternsStructuralFacade;class Facade{ /** * @var OsInterface * 定義操作系統(tǒng)接口變量。 */ private $os; /** * @var BiosInterface * 定義基礎(chǔ)輸入輸出系統(tǒng)接口變量。 */ private $bios; /** * @param BiosInterface $bios * @param OsInterface $os * 傳入基礎(chǔ)輸入輸出系統(tǒng)接口對(duì)象 $bios 。 * 傳入操作系統(tǒng)接口對(duì)象 $os 。 */ public function __construct(BiosInterface $bios, OsInterface $os) {$this->bios = $bios;$this->os = $os; } /** * 構(gòu)建基礎(chǔ)輸入輸出系統(tǒng)執(zhí)行啟動(dòng)方法。 */ 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
<?phpnamespace DesignPatternsStructuralFacade;/*** 創(chuàng)建操作系統(tǒng)接口類 OsInterface 。*/interface OsInterface{ /** * 聲明關(guān)機(jī)方法。 */ public function halt(); /** * 聲明獲取名稱方法,返回字符串格式數(shù)據(jù)。 */ public function getName(): string;}
BiosInterface.php
<?phpnamespace DesignPatternsStructuralFacade;/*** 創(chuàng)建基礎(chǔ)輸入輸出系統(tǒng)接口類 BiosInterface 。*/interface BiosInterface{ /** * 聲明執(zhí)行方法。 */ public function execute(); /** * 聲明等待密碼輸入方法 */ public function waitForKeyPress(); /** * 聲明登錄方法。 */ public function launch(OsInterface $os); /** * 聲明關(guān)機(jī)方法。 */ public function powerDown();}測(cè)試
Tests/FacadeTest.php
<?phpnamespace DesignPatternsStructuralFacadeTests;use DesignPatternsStructuralFacadeFacade;use DesignPatternsStructuralFacadeOsInterface;use PHPUnitFrameworkTestCase;/*** 創(chuàng)建自動(dòng)化測(cè)試單元 FacadeTest 。*/class FacadeTest extends TestCase{ public function testComputerOn() {/** @var OsInterface|PHPUnit_Framework_MockObject_MockObject $os */$os = $this->createMock(’DesignPatternsStructuralFacadeOsInterface’);$os->method(’getName’) ->will($this->returnValue(’Linux’));$bios = $this->getMockBuilder(’DesignPatternsStructuralFacadeBiosInterface’) ->setMethods([’launch’, ’execute’, ’waitForKeyPress’]) ->disableAutoload() ->getMock();$bios->expects($this->once()) ->method(’launch’) ->with($os);$facade = new Facade($bios, $os);// 門面接口很簡(jiǎn)單。$facade->turnOn();// 但你也可以訪問底層組件。$this->assertEquals(’Linux’, $os->getName()); }}
以上就是淺談PHP設(shè)計(jì)模式之門面模式Facade的詳細(xì)內(nèi)容,更多關(guān)于PHP設(shè)計(jì)模式之門面模式Facade的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python判斷字符串是否為合法標(biāo)示符操作2. 如何學(xué)習(xí)html的各種標(biāo)簽3. 三道java新手入門面試題,通往自由的道路--JVM4. Python接口測(cè)試文件上傳實(shí)例解析5. vue項(xiàng)目登錄成功拿到令牌跳轉(zhuǎn)失敗401無(wú)登錄信息的解決6. python 將html轉(zhuǎn)換為pdf的幾種方法7. 一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解8. 使用AJAX異步通信技術(shù)實(shí)現(xiàn)搜索聯(lián)想和自動(dòng)補(bǔ)全示例9. Python字符串及文本模式方法詳解10. ASP.NET 2.0頁(yè)面框架的幾處變化
