溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在php項(xiàng)目中實(shí)現(xiàn)一個(gè)門面模式

發(fā)布時(shí)間:2021-01-29 16:28:31 來(lái)源:億速云 閱讀:163 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)怎么在php項(xiàng)目中實(shí)現(xiàn)一個(gè)門面模式,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

一、意圖
為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,F(xiàn)acade模式定義了一個(gè)高層次的接口,使得子系統(tǒng)更加容易使用【GOF95】
外部與子系統(tǒng)的通信是通過(guò)一個(gè)門面(Facade)對(duì)象進(jìn)行。
二、門面模式結(jié)構(gòu)圖

怎么在php項(xiàng)目中實(shí)現(xiàn)一個(gè)門面模式

三、門面模式中主要角色
門面(Facade)角色:
此角色將被客戶端調(diào)用
知道哪些子系統(tǒng)負(fù)責(zé)處理請(qǐng)求
將用戶的請(qǐng)求指派給適當(dāng)?shù)淖酉到y(tǒng)

子系統(tǒng)(subsystem)角色:
實(shí)現(xiàn)子系統(tǒng)的功能
處理由Facade對(duì)象指派的任務(wù)
沒(méi)有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)使用起來(lái)更加方便
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è)接口,這一接口可用來(lái)以一種子系統(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ì)而言,F(xiàn)acade模式僅對(duì)子系統(tǒng)對(duì)象的接口進(jìn)行抽象,從而使它們更容易使用;它并定義不功能,子系統(tǒng)也不知道facade的存在
單例模式(singleton模式):一般來(lái)說(shuō),僅需要一個(gè)Facade對(duì)象,因此Facade對(duì)象通常屬于Singleton對(duì)象。
七、門面模式PHP示例

<?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項(xiàng)目中實(shí)現(xiàn)一個(gè)門面模式,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI