Introduction to Facade (appearance mode) of PHP design pattern

怪我咯
Release: 2023-03-13 19:54:01
Original
1322 people have browsed it

Provide a consistent interface for a set of interfaces in the subsystem and define a high-level interface to make this subsystem easier to use

Pattern definition: Facade Pattern(Facade Pattern): External communication with a subsystem must be carried out through a unified facadeObject, for the subsystem A set of interfaces provides a consistent interface, and the facade pattern defines a high-level interface that makes this subsystem easier to use. Appearance mode is also called facade mode, which is an object structure mode.
Mode structure:

The appearance mode is to allow the client to call a more complex system in a simple way to complete one thing .

Appearance mode, also called facade mode. It is mostly used as an intermediate layer between multiple subsystems. Users directly request work through the Facade object, eliminating the need for users to call multiple subsystems.

A common example of appearance mode is that we bought many stocks, but time is limited. Tracking is complicated and we made a mess. So, we simply bought stock funds. The stock fund is like the Facade object of the appearance model, and the subsystem is each stock invested by the stock fund.

The code is as follows:

class car { 
public function start() { 
print_r("车子启动"); 
} 
public function check_stop() { 
print_r("刹车检查正常"); 
} 
public function check_box() { 
print_r("检查油箱正常"); 
} 
public function check_console() { 
print_r("检查仪表盘是否异常"); 
} 
} 

//facade模式 
class carfacade { 
public function catgo(car $carref){ 
$carref->check_stop(); 
$carref->check_box(); 
$carref->check_console(); 
$carref->start(); 
} 
} 
//客户端可以简单的去调用。 
$car = new car(); 
$carObj = new carfacade(); 
$carObj->catgo($car);
Copy after login

The code is as follows:

<?php 
/** 
* 外观模式 示例 
* 
* 为子系统中的一组接口提供一个一致的界面,定义一个高层接口,使得这一子系统更加的容易使用 
*/ 
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 Facade 
{ 
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 "Facade MethodA<br/>"; 
$this->_object1->Method1(); 
$this->_object2->Method2(); 
} 

public function MethodB() { 
echo "Facade MethodB<br/>"; 
$this->_object2->Method2(); 
$this->_object3->Method3(); 
} 
} 

// 实例化 
$objFacade = new Facade(); 

$objFacade->MethodA(); 
$objFacade->MethodB();
Copy after login

The above is the detailed content of Introduction to Facade (appearance mode) of PHP design pattern. For more information, please follow other related articles on the PHP Chinese website!

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