Learn PHP Design patterns PHP Implementation façade mode (façade) _php tips

Source: Internet
Author: User
Tags learn php php class

I. Intention
Provides a consistent interface for a set of interfaces in a subsystem, and the façade pattern defines a high-level interface that makes it easier for subsystems to use "GOF95"
External communication with subsystems is done through a façade (façade) object.
Second, the façade pattern structure chart

Third, the main role in the façade model
façade (façade) role:
This role will be invoked by the client
Know which subsystems are responsible for processing requests
Assigning a user's request to the appropriate subsystem

Subsystem (SUBSYSTEM) role:
Realize the function of subsystem
Work with tasks assigned by a façade object
There is no relevant information about the façade, which can be called directly by the client
You can have one or more subsystems at the same time, each subsystem is not a separate class, and a collection of classes. Each subsystem can be called directly by the client or by a façade role. subsystem and know that the façade mode exists, for the subsystem, the façade is just another client.
Four, the advantages of the façade model
1, it shields the customer subsystem components, thus reducing the number of objects processed by customers and make the subsystem more convenient to use
2. Realize the loose coupling relationship between subsystem and customer
3, if the application needs, it does not limit their use of subsystem classes. Therefore, it is possible to choose between system ease of use and usability
V. The scene of the façade mode application
1, for some complex subsystems to provide a set of interfaces
2, improve the independence of the subsystem
3, in the hierarchical structure, you can use the façade mode to define the interface of each layer of the system
vi. façade mode and other modes
Abstract Factory mode (in abstract Factory mode):
The Abstract factory pattern can be used in conjunction with the façade pattern to provide an interface that can be used to create subsystem objects in a subsystem-independent way. The Abstract factory pattern can also be used to hide platform-related classes instead of the façade pattern
Mediator Mode:The similarity between the mediator pattern and the façade pattern is that it abstracts the functionality of some existing classes. However, the purpose of mediator is to abstract any communication between colleagues, often concentrating on functions that do not belong to any single object. Mediator's colleague object knows the mediator and communicates with it, rather than communicating directly with other homogeneous objects. In contrast, the façade pattern abstracts only the interfaces of subsystem objects, making them easier to use; it defines non-functional, and subsystems do not know the presence of a façade.
single case mode (singleton mode):In general, only a façade object is required, so a façade object usually belongs to a singleton object.
Seven, façade mode PHP sample

<?php class Camera {/** * Open VCR/Public function Turnon () {echo ' turning on the camera.<br/> ';
 /** * Close the VCR/Public function turnoff () {echo ' turning off the camera.<br/> '; /** * Go to VCR * @param <type> $degrees * * Public Function rotate ($degrees) {echo ' Rotating the camera b
 Y ', $degrees, ' degrees.<br/> ';
 } class Light {/** * turn on light/Public Function turnon () {echo ' turning on the light.<br/> ';
 /** * Turn off the light/public function turnoff () {echo ' turning off the light.<br/> ';
 /** * Change bulb/Public Function changebulb () {echo ' Changing the light-bulb.<br/> ';
 } class Sensor {/** * start sensor/Public function activate () {echo ' activating the sensor.<br/> ';
 /** * Close sensor/Public function deactivate () {echo ' deactivating the sensor.<br/> '; /** * Trigger sensor/Public Function trigger () {echo ' The sensor has been TRIgged.<br/> ';
 } class Alarm {/** * launch alarm/Public Function activate () {echo ' activating the alarm.<br/> ';
 /** * Close Alarm/Public Function deactivate () {echo ' deactivating the alarm.<br/> ';
 /** * Sound alarm/Public Function rings () {echo ' Ring the alarm.<br/> ';
 /** * Stop the alarm/Public Function stopring () {echo ' Stop the alarm.<br/> ';
 }/** * Façade class */class Securityfacade {/* VCR/private $_camera1, $_camera2;
 * * Lights/private $_light1, $_light2, $_LIGHT3;
 * * Sensor/private $_sensor;
 * * Alarm/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 ();
  }/** * Client/class Client {private static $_security;
  /** * Main program.
  */public static function main () {self::$_security = new Securityfacade ();
 Self::$_security->activate (); } client::main ();?>

The above is the use of PHP to implement the façade mode of code, there are some of the concept of the façade model to differentiate, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.