學(xué)習(xí)php設(shè)計模式 php實現(xiàn)門面模式(Facade)
一、意圖
為子系統(tǒng)中的一組接口提供一個一致的界面,F(xiàn)acade模式定義了一個高層次的接口,使得子系統(tǒng)更加容易使用【GOF95】
外部與子系統(tǒng)的通信是通過一個門面(Facade)對象進行。
二、門面模式結(jié)構(gòu)圖
三、門面模式中主要角色
門面(Facade)角色:
此角色將被客戶端調(diào)用
知道哪些子系統(tǒng)負責(zé)處理請求
將用戶的請求指派給適當?shù)淖酉到y(tǒng)
子系統(tǒng)(subsystem)角色:
實現(xiàn)子系統(tǒng)的功能
處理由Facade對象指派的任務(wù)
沒有Facade的相關(guān)信息,可以被客戶端直接調(diào)用
可以同時有一個或多個子系統(tǒng),每個子系統(tǒng)都不是一個單獨的類,而一個類的集合。每個子系統(tǒng)都可以被客戶端直接調(diào)用,或者被門面角色調(diào)用。子系統(tǒng)并知道門面模式的存在,對于子系統(tǒng)而言,門面僅僅是另一個客戶端。
四、門面模式的優(yōu)點
1、它對客戶屏蔽了子系統(tǒng)組件,因而減少了客戶處理的對象的數(shù)目并使得子系統(tǒng)使用起來更加方便
2、實現(xiàn)了子系統(tǒng)與客戶之間的松耦合關(guān)系
3、如果應(yīng)用需要,它并不限制它們使用子系統(tǒng)類。因此可以在系統(tǒng)易用性與能用性之間加以選擇
五、門面模式適用場景
1、為一些復(fù)雜的子系統(tǒng)提供一組接口
2、提高子系統(tǒng)的獨立性
3、在層次化結(jié)構(gòu)中,可以使用門面模式定義系統(tǒng)的每一層的接口
六、門面模式與其它模式
抽象工廠模式(abstract factory模式):Abstract Factory模式可以與Facade模式一起使用以提供一個接口,這一接口可用來以一種子系統(tǒng)獨立的方式創(chuàng)建子系統(tǒng)對象。Abstract Factory模式也可以代替Facade模式隱藏那些與平臺相關(guān)的類
調(diào)停者模式:Mediator模式與Facade模式的相似之處是,它抽象了一些已有類的功能。然而,Mediator目的是對同事之間的任意通訊進行抽象,通常集中不屬于任何單個對象的功能。Mediator的同事對象知道中介者并與它通信,而不是直接與其他同類對象通信。相對而言,F(xiàn)acade模式僅對子系統(tǒng)對象的接口進行抽象,從而使它們更容易使用;它并定義不功能,子系統(tǒng)也不知道facade的存在
單例模式(singleton模式):一般來說,僅需要一個Facade對象,因此Facade對象通常屬于Singleton對象。
七、門面模式PHP示例
<?php class Camera { /** * 打開錄像機 */ public function turnOn() { echo 'Turning on the camera.<br />'; } /** * 關(guān)閉錄像機 */ public function turnOff() { echo 'Turning off the camera.<br />'; } /** * 轉(zhuǎn)到錄像機 * @param <type> $degrees */ public function rotate($degrees) { echo 'rotating the camera by ', $degrees, ' degrees.<br />'; } } class Light { /** * 開燈 */ public function turnOn() { echo 'Turning on the light.<br />'; } /** * 關(guān)燈 */ public function turnOff() { echo 'Turning off the light.<br />'; } /** * 換燈泡 */ public function changeBulb() { echo 'changing the light-bulb.<br />'; } } class Sensor { /** * 啟動感應(yīng)器 */ public function activate() { echo 'Activating the sensor.<br />'; } /** * 關(guān)閉感應(yīng)器 */ public function deactivate() { echo 'Deactivating the sensor.<br />'; } /** * 觸發(fā)感應(yīng)器 */ public function trigger() { echo 'The sensor has been trigged.<br />'; } } class Alarm { /** * 啟動警報器 */ public function activate() { echo 'Activating the alarm.<br />'; } /** * 關(guān)閉警報器 */ public function deactivate() { echo 'Deactivating the alarm.<br />'; } /** * 拉響警報器 */ public function ring() { echo 'Ring the alarm.<br />'; } /** * 停掉警報器 */ public function stopRing() { echo 'Stop the alarm.<br />'; } } /** * 門面類 */ class SecurityFacade { /* 錄像機 */ private $_camera1, $_camera2; /* 燈 */ private $_light1, $_light2, $_light3; /* 感應(yīng)器 */ private $_sensor; /* 警報器 */ private $_alarm; public function __construct() { $this->_camera1 = new Camera(); $this->_camera2 = new Camera(); $this->_light1 = new Light(); $this->_light2 = new Light(); $this->_light3 = new Light(); $this->_sensor = new Sensor(); $this->_alarm = new Alarm(); } public function activate() { $this->_camera1->turnOn(); $this->_camera2->turnOn(); $this->_light1->turnOn(); $this->_light2->turnOn(); $this->_light3->turnOn(); $this->_sensor->activate(); $this->_alarm->activate(); } public function deactivate() { $this->_camera1->turnOff(); $this->_camera2->turnOff(); $this->_light1->turnOff(); $this->_light2->turnOff(); $this->_light3->turnOff(); $this->_sensor->deactivate(); $this->_alarm->deactivate(); } } /** * 客戶端 */ class Client { private static $_security; /** * Main program. */ public static function main() { self::$_security = new SecurityFacade(); self::$_security->activate(); } } Client::main(); ?>
以上就是使用php實現(xiàn)門面模式的代碼,還有一些關(guān)于門面模式的概念區(qū)分,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
WordPress開發(fā)中自定義菜單的相關(guān)PHP函數(shù)使用簡介
這篇文章主要介紹了WordPress開發(fā)中自定義菜單的相關(guān)PHP函數(shù)使用,包括過濾掉自定義菜單中無用的class值的方法,需要的朋友可以參考下2016-01-01php的instanceof和判斷閉包Closure操作示例
這篇文章主要介紹了php的instanceof和判斷閉包Closure操作,結(jié)合實例形式分析了PHP使用instanceof判斷類實例以及判斷閉包Closure相關(guān)操作技巧,需要的朋友可以參考下2020-01-01php通過ksort()函數(shù)給關(guān)聯(lián)數(shù)組按照鍵排序的方法
這篇文章主要介紹了php通過ksort()函數(shù)給關(guān)聯(lián)數(shù)組按照鍵排序的方法,實例分析了php中ksort()函數(shù)的使用技巧,需要的朋友可以參考下2015-03-03php字符串函數(shù)學(xué)習(xí)之strstr()
這篇文章主要介紹了php字符串函數(shù)學(xué)習(xí)之strstr(),本文講解了它的定義和用法、參數(shù)描述、提示和注釋以及多個使用示例,需要的朋友可以參考下2015-03-03PHP4 與 MySQL 數(shù)據(jù)庫操作函數(shù)詳解
PHP4 與 MySQL 數(shù)據(jù)庫操作函數(shù)詳解...2006-12-12