Learning php design patterns php Facade)

Source: Internet
Author: User
This article mainly introduces the facade mode in the php design mode and uses php to implement the facade mode. if you are interested, refer to it. I. intention
Provides a consistent interface for a group of interfaces in the subsystem. the Facade mode defines a high-level interface, making it easier for the subsystem to use GOF95]
External Communication with subsystems is performed through a Facade object.
II. facade mode structure

3. main roles in the facade mode
Facade role:
This role will be called by the client
Know which subsystems are responsible for processing requests
Assign user requests to appropriate subsystems

Subsystem role:
Implement sub-system functions
Process tasks assigned by the Facade object
Without Facade information, it can be directly called by the client.
There can be one or more subsystems at the same time. 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 also knows the existence of the facade mode. for the subsystem, the facade is just another client.
IV. advantages of the facade model
1. it shields sub-system components from the customer, thus reducing the number of objects processed by the customer and making the sub-system more convenient to use.
2. loose coupling between subsystems and customers
3. if the application needs it, it does not limit the use of sub-system classes. Therefore, you can choose between ease of use and availability of the system.
V. applicable scenarios of the facade model
1. provide a set of interfaces for some complex subsystems
2. improve the independence of subsystems
3. in a hierarchical structure, you can use the facade mode to define the interfaces of each layer of the system.
VI. facade mode and other modes
Abstract factory mode (abstract factory mode ):
Abstract Factory mode can be used together with Facade mode to provide an interface, which can be used to create subsystem objects in a seed system independent way. Abstract Factory mode can also replace Facade mode to hide platform-related classes.
Mediator mode:Similar to the Facade mode, the Mediator mode abstracts some existing class functions. However, Mediator aims to abstract any communication between colleagues, and generally does not belong to any single object. A Mediator colleague knows the intermediary and communicates with it, instead of directly communicating with other similar objects. Relatively speaking, the Facade mode only abstracts the interfaces of subsystem objects to make them easier to use. it does not define functionality, and the subsystem does not know the existence of the facade.
Singleton mode ):Generally, only one Facade object is required, so the Facade object is usually a Singleton object.
VII. facade mode PHP example

<? Phpclass Camera {/*** enable video recorder */public function turnOn () {echo 'turning on the camera.
';}/*** Disable video recorder */public function turnOff () {echo 'turning off the camera.
';}/*** Switch to video recorder * @ param $ Degrees */public function rotate ($ degrees) {echo 'rotating the camera by', $ degrees, 'degrees.
';}} Class Light {/*** turn on the light */public function turnOn () {echo 'turning on the Light.
';}/*** Turn off the light */public function turnOff () {echo 'turning off the light.
';}/*** Change light bulb */public function changeBulb () {echo 'changing the light-bulb.
';}} Class Sensor {/*** start sensor */public function activate () {echo 'Activating the Sensor.
';}/*** Turn off sensor */public function deactivate () {echo 'deactivating the sensor.
';}/*** Trigger sensor */public function trigger () {echo' The sensor has been trigged.
';}} Class Alarm {/*** start alarm */public function activate () {echo 'Activating the Alarm.
';}/*** Close the alarm */public function deactivate () {echo 'deactivating the alarm.
';}/*** Audible alarm */public function ring () {echo 'ring the alarm.
';}/*** Stop the alarm */public function stopRing () {echo 'stop the alarm.
';}}/*** Door class */class SecurityFacade {/* video recorder */private $ _ camera1, $ _ camera2;/* lamp */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 (); $ t His-> _ 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-> _ ligh T3-> 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 code for implementing the facade mode using php, and there are some concepts about the facade mode, 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.