《PHP編程:學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)門面模式(Facade)》要點(diǎn):
本文介紹了PHP編程:學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)門面模式(Facade),希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP應(yīng)用一、意圖
為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,Facade模式定義了一個(gè)高層次的接口,使得子系統(tǒng)更加容易使用【GOF95】
外部與子系統(tǒng)的通信是通過一個(gè)門面(Facade)對(duì)象進(jìn)行.
二、門面模式結(jié)構(gòu)圖
PHP應(yīng)用
PHP應(yīng)用三、門面模式中主要角色
門面(Facade)角色:
此角色將被客戶端調(diào)用
知道哪些子系統(tǒng)負(fù)責(zé)處理哀求
將用戶的哀求指派給適當(dāng)?shù)淖酉到y(tǒng)
PHP應(yīng)用子系統(tǒng)(subsystem)角色:
實(shí)現(xiàn)子系統(tǒng)的功能
處理由Facade對(duì)象指派的任務(wù)
沒有Facade的相關(guān)信息,可以被客戶端直接調(diào)用
可以同時(shí)有一個(gè)或多個(gè)子系統(tǒng),每個(gè)子系統(tǒng)都不是一個(gè)單獨(dú)的類,而一個(gè)類的集合.每個(gè)子系統(tǒng)都可以被客戶端直接調(diào)用,或者被門面角色調(diào)用.子系統(tǒng)并知道門面模式的存在,對(duì)于子系統(tǒng)而言,門面僅僅是另一個(gè)客戶端.
四、門面模式的優(yōu)點(diǎn)
1、它對(duì)客戶屏蔽了子系統(tǒng)組件,因而減少了客戶處理的對(duì)象的數(shù)目并使得子系統(tǒng)使用起來更加方便
2、實(shí)現(xiàn)了子系統(tǒng)與客戶之間的松耦合關(guān)系
3、如果應(yīng)用需要,它并不限制它們使用子系統(tǒng)類.因此可以在系統(tǒng)易用性與能用性之間加以選擇
五、門面模式適用場(chǎng)景
1、為一些復(fù)雜的子系統(tǒng)提供一組接口
2、提高子系統(tǒng)的獨(dú)立性
3、在層次化結(jié)構(gòu)中,可以使用門面模式定義系統(tǒng)的每一層的接口
六、門面模式與其它模式
抽象工廠模式(abstract factory模式):Abstract Factory模式可以與Facade模式一起使用以提供一個(gè)接口,這一接口可用來以一種子系統(tǒng)獨(dú)立的方式創(chuàng)建子系統(tǒng)對(duì)象.Abstract Factory模式也可以代替Facade模式暗藏那些與平臺(tái)相關(guān)的類
調(diào)停者模式:Mediator模式與Facade模式的相似之處是,它抽象了一些已有類的功能.然而,Mediator目的是對(duì)同事之間的任意通訊進(jìn)行抽象,通常集中不屬于任何單個(gè)對(duì)象的功能.Mediator的同事對(duì)象知道中介者并與它通信,而不是直接與其他同類對(duì)象通信.相對(duì)而言,Facade模式僅對(duì)子系統(tǒng)對(duì)象的接口進(jìn)行抽象,從而使它們更容易使用;它并定義不功能,子系統(tǒng)也不知道facade的存在
單例模式(singleton模式):一般來說,僅需要一個(gè)Facade對(duì)象,因此Facade對(duì)象通常屬于Singleton對(duì)象.
七、門面模式PHP示例
PHP應(yīng)用
<?php
class Camera {
/**
* 打開錄像機(jī)
*/
public function turnOn() {
echo 'Turning on the camera.<br />';
}
/**
* 關(guān)閉錄像機(jī)
*/
public function turnOff() {
echo 'Turning off the camera.<br />';
}
/**
* 轉(zhuǎn)到錄像機(jī)
* @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 {
/**
* 啟動(dòng)感應(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 {
/**
* 啟動(dòng)警報(bào)器
*/
public function activate() {
echo 'Activating the alarm.<br />';
}
/**
* 關(guān)閉警報(bào)器
*/
public function deactivate() {
echo 'Deactivating the alarm.<br />';
}
/**
* 拉響警報(bào)器
*/
public function ring() {
echo 'Ring the alarm.<br />';
}
/**
* 停掉警報(bào)器
*/
public function stopRing() {
echo 'Stop the alarm.<br />';
}
}
/**
* 門面類
*/
class SecurityFacade {
/* 錄像機(jī) */
private $_camera1, $_camera2;
/* 燈 */
private $_light1, $_light2, $_light3;
/* 感應(yīng)器 */
private $_sensor;
/* 警報(bào)器 */
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應(yīng)用以上就是使用php實(shí)現(xiàn)門面模式的代碼,還有一些關(guān)于門面模式的概念區(qū)分,希望對(duì)大家的學(xué)習(xí)有所贊助.
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP編程:學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)門面模式(Facade)》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/8301.html