PHP design pattern facade (appearance mode) _php tips

Source: Internet
Author: User
Pattern Definition: Appearance mode (façade pattern): External communication with a subsystem must be carried out through a uniform visual object, providing a consistent interface for a set of interfaces in the subsystem, and the appearance pattern defines a high-level interface that makes the subsystem easier to use. The appearance pattern, also known as the façade mode, is an object-structured pattern.
Pattern structure:

The appearance pattern is to allow client clients to invoke a more complex system in a simple way to accomplish one thing.

SUBSYSTEM:
Copy Code code as follows:

Class Car {
Public Function Start () {
Print_r ("car Start");
}
Public Function Check_stop () {
Print_r ("Brake check normal");
}
Public Function Check_box () {
Print_r ("Check oil tank normal");
}
Public Function Check_console () {
Print_r ("Check if the dashboard is abnormal");
}
}

Facade mode
Class Carfacade {
Public function Catgo (car $carref) {
$carref->check_stop ();
$carref->check_box ();
$carref->check_console ();
$carref->start ();
}
}
The client can simply go to the call.
$car = new car ();
$CAROBJ = new Carfacade ();
$CAROBJ->catgo ($car);

Copy Code code as follows:

<?php
/**
* Sample Appearance mode
*
* Provides a consistent interface for a set of interfaces in a subsystem, defining a high-level interface that makes this subsystem easier to use
*/
Class SubSytem1
{
Public Function Method1 ()
{
echo "Subsystem1 method1<br/>";
}
}

Class SUBSYTEM2
{
Public Function Method2 ()
{
echo "Subsystem2 method2<br/>";
}
}

Class SubSytem3
{
Public Function Method3 ()
{
echo "Subsystem3 method3<br/>";
}
}

Class Façade
{
Private $_object1 = null;
Private $_object2 = null;
Private $_object3 = null;

Public Function __construct ()
{
$this->_object1 = new SubSytem1 ();
$this->_object2 = new SubSytem2 ();
$this->_object3 = new SubSytem3 ();
}

Public Function MethodA () {
echo "Façade methoda<br/>";
$this->_object1->method1 ();
$this->_object2->method2 ();
}

Public Function MethodB () {
echo "Façade methodb<br/>";
$this->_object2->method2 ();
$this->_object3->method3 ();
}
}

Instantiation of
$objFacade = new façade ();

$objFacade->methoda ();
$objFacade->methodb ();

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.