外觀模式(Facade Pattern):外部與子系統的通信必須通過一個統一的外觀對象進行,為子系統中的一組接口提供一個一緻的界面,外觀模式定義了一個高層接口,這個接口是的這一個子系統更加容易使用。外觀模式又稱門面模式,它是一種對象結構模式。
模式的結構
外觀模式就是讓用戶端以一種簡單的方式來調用比較複雜的系統,來完成一件事情。
/**
*Facade 外觀模式
*/
/**
*錄影機
*/
class Camera
{
/**
*打開錄影機
*/
public function turnOn()
{
echo 'Turning on the camera.<br/>';
}
/**
*關閉錄影機
*/
public function turnOff()
{
echo 'Turning off the camera.<br/>';
}
/**
*轉到錄像機
*@param $degrees
*/
public function rotate($degrees)
{
echo 'Totating the camera by '. $degrees . ' degress.<br/>';
}
}
/**
*燈光
*/
class Light
{
/**
*開燈
*/
public function trunOn()
{
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 trigger()
{
echo 'The alarm has been trigged.<br/>';
}
}
/**
*門面類
*/
class SecurityFacade
{
//錄像機
private $_camera1, $_camera2;
//燈光
private $_light1, $_light2, $_light3;
//感應器
private $_sensor;
//警報器
private $_alarm;
public function __construct()
{
$this->_camera1 = new Camera();
$this->_canera2 = 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();
}
}
//使用
$_security = new SecurityFacade();
$_security->activate();