门面模式意图与结构
一、意图 门面模式旨在为子系统中的一组接口提供一个统一的访问界面。通过定义一个高层次的接口,该模式降低了系统的使用难度【GOF95】。外部客户端与子系统之间的交互不再直接进行,而是统一通过一个门面(Facade)对象来完成。 二、门面模式结构图

三、门面模式中主要角色 门面(Facade)角色: 这是客户端直接调用的入口点。 熟悉各个子系统的功能分布,清楚哪些子系统负责处理特定请求。 负责接收用户的请求,并将其分派给合适的子系统进行处理。 子系统(subsystem)角色: 承担子系统的核心功能实现。 执行由门面对象分派的具体任务。 不依赖门面存在,允许被客户端直接调用。 子系统可以是一个或多个,每个子系统并非单一类,而是类的集合。子系统对门面的存在一无所知,将其视为普通的客户端。
四、门面模式的优点 1、对客户屏蔽了子系统内部的复杂组件,从而显著减少了客户需要管理的对象数量,使子系统的使用变得更加简便。 2、有效实现了子系统与客户之间的松耦合关系,降低了模块间的依赖性。 3、并不限制应用程序直接使用子系统类。因此,开发者可以根据需要在系统的易用性与功能性之间做出灵活选择。 五、门面模式适用场景 1、当需要为一组复杂的子系统提供简化的接口时。 2、为了提高子系统的独立性和可维护性。 3、在层次化软件架构中,用于定义每一层对外提供的统一接口。 六、门面模式与其它模式 抽象工厂模式(Abstract Factory):两者可结合使用,以提供一种独立于子系统的方式创建对象。此外,抽象工厂也可替代门面模式,用于隐藏平台相关的类。 调停者模式(Mediator):虽然两者都涉及功能抽象,但调停者侧重于抽象同事对象间的任意通信,通常集中不属于单个对象的功能。调停者的同事对象知晓中介者并与其通信,而非直接交互。相比之下,门面模式仅对子系统接口进行抽象以简化使用,不定义新功能,且子系统 unaware 门面的存在。 单例模式(Singleton):由于通常整个系统只需要一个门面实例,因此门面对象常被实现为单例模式。
<?php
class Camera {
/**
* 打开录像机
*/
public function turnOn() {
echo 'Turning on the camera.
';
}
/**
* 关闭录像机
*/
public function turnOff() {
echo 'Turning off the camera.
';
}
/**
* 转到录像机
* @param <type> $degrees
*/
public function rotate($degrees) {
echo 'rotating the camera by ', $degrees, ' degrees.
';
}
}
class Light {
/**
* 开灯
*/
public function turnOn() {
echo 'Turning on the light.
';
}
/**
* 关灯
*/
public function turnOff() {
echo 'Turning off the light.
';
}
/**
* 换灯泡
*/
public function changeBulb() {
echo 'changing the light-bulb.
';
}
}
class Sensor {
/**
* 启动感应器
*/
public function activate() {
echo 'Activating the sensor.
';
}
/**
* 关闭感应器
*/
public function deactivate() {
echo 'Deactivating the sensor.
';
}
/**
* 触发感应器
*/
public function trigger() {
echo 'The sensor has been trigged.
';
}
}
class Alarm {
/**
* 启动警报器
*/
public function activate() {
echo 'Activating the alarm.
';
}
/**
* 关闭警报器
*/
public function deactivate() {
echo 'Deactivating the alarm.
';
}
/**
* 拉响警报器
*/
public function ring() {
echo 'Ring the alarm.
';
}
/**
* 停掉警报器
*/
public function stopRing() {
echo 'Stop the alarm.
';
}
}
/**
* 门面类
*/
class SecurityFacade {
/* 录像机 */
private $_camera1, $_camera2;
/* 灯 */
private $_light1, $_light2, $_light3;
/* 感应器 */
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示例 以上展示了使用PHP实现门面模式的完整代码。此外,文中还对比分析了门面模式与其他相关概念的区别,希望能为读者在学习和设计模式时提供帮助。