Learn PHP design patterns PHP implements facade mode (Facade)_php skills

WBOY
Release: 2016-05-16 20:03:36
Original
831 people have browsed it

1. Intention
To provide a consistent interface for a set of interfaces in a subsystem, the Facade pattern defines a high-level interface, making the subsystem easier to use [GOF95]
Communication between the outside and the subsystem is through a facade object.
2. Facade mode structure diagram

3. Main characters in facade mode
Facade role:
This role will be called by the client
Know which subsystems are responsible for handling requests
Assign user requests to the appropriate subsystem

Subsystem role:
Implement the functions of the subsystem
Process tasks assigned by the Facade object
There is no Facade related information and can be called directly by the client
There can be one or more subsystems at the same time, and each subsystem is not a separate class, but a collection of classes. Each subsystem can be called directly by the client, or by the facade role. The subsystem is not aware of the existence of the facade pattern. For the subsystem, the facade is just another client.
4. Advantages of the facade model
1. It shields subsystem components from customers, thus reducing the number of objects processed by customers and making the subsystem more convenient to use
2. Achieved a loose coupling relationship between subsystems and customers
3. It does not restrict applications from using subsystem classes if they need it. So you can choose between system ease of use and usability
5. Applicable Scenarios of Facade Mode
1. Provide a set of interfaces for some complex subsystems
2. Improve the independence of subsystems
3. In the hierarchical structure, the facade pattern can be used to define the interface of each layer of the system
6. Facade mode and other modes
Abstract Factory Pattern:
The Abstract Factory pattern can be used with the Facade pattern to provide an interface that can be used to create subsystem objects in a subsystem-independent manner. Abstract Factory mode can also replace Facade mode to hide those platform-related classes
Mediator pattern: The similarity between the Mediator pattern and the Facade pattern is that it abstracts the functions of some existing classes. However, the purpose of Mediator is to abstract arbitrary communication between colleagues, usually focusing on functionality that does not belong to any single object. Mediator's colleague objects are aware of the mediator and communicate with it, rather than communicating directly with other objects of the same type. Relatively speaking, the Facade pattern only abstracts the interfaces of subsystem objects to make them easier to use; it does not define functions, and the subsystem does not know the existence of the facade
Singleton mode: Generally speaking, only one Facade object is required, so Facade objects usually belong to Singleton objects.
7. Facade Mode PHP Example

<&#63;php
class Camera {
 
 /**
  * 打开录像机
  */
 public function turnOn() {
  echo 'Turning on the camera.<br />';
 }
 
 /**
  * 关闭录像机
  */
 public function turnOff() {
  echo 'Turning off the camera.<br />';
 }
 
 /**
  * 转到录像机
  * @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 />';
 }
 
 /**
  * 关灯
  */
 public function turnOff() {
  echo 'Turning off the light.<br />';
 }
 
 /**
  * 换灯泡
  */
 public function changeBulb() {
  echo 'changing the light-bulb.<br />';
 }
}
 
class Sensor {
 
 /**
  * 启动感应器
  */
 public function activate() {
  echo 'Activating the sensor.<br />';
 }
 
 /**
  * 关闭感应器
  */
 public function deactivate() {
  echo 'Deactivating the sensor.<br />';
 }
 
 /**
  * 触发感应器
  */
 public function trigger() {
  echo 'The sensor has been trigged.<br />';
 }
}
 
class Alarm {
 
 /**
  * 启动警报器
  */
 public function activate() {
  echo 'Activating the alarm.<br />';
 }
 
 /**
  * 关闭警报器
  */
 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;
 
 /* 感应器 */
 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();
&#63;>
Copy after login

The above is the code for using PHP to implement the facade mode. There are also some conceptual distinctions about the facade mode. I hope it will be helpful to everyone's learning.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!