Home >Backend Development >PHP Tutorial >The role of appearance mode

The role of appearance mode

藏色散人
藏色散人forward
2019-04-16 10:00:456853browse

Appearance mode (facade mode)

Appearance mode refers to the packaging of appearance so that the application can only see the appearance object, but not the appearance object. to specific detail objects, which will undoubtedly reduce the complexity of the application and improve the maintainability of the program.

Advantages of the facade pattern

1. It shields the subsystem components from the customer, thereby reducing the number of objects processed by the customer and making the subsystem more convenient to use.

2. Achieves a loose coupling relationship between subsystems and customers

3. If applications need it, it does not restrict them from using subsystem classes. Therefore, you can choose between system ease of use and usability

Applicable scenarios of facade mode

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 pattern to define the interface of each layer of the system

For example, when we develop a website There are the following functions

We can implement and package the three functions of closing and opening the website, closing and opening the blog, and closing and opening the registration.

<?php
//关闭和开启网站 
class webSet{
public function start(){
echo &#39;开启网站......&#39;;
}
public function stop(){
echo &#39;关闭网站......&#39;;
}
}
//关闭开启博客 
class blogSet{
public function start(){
echo &#39;开启博客......&#39;;
}
public function stop(){
echo &#39;关闭博客......&#39;;
}
}
//关闭开启注册
class registerSet{
public function start(){
echo &#39;开启注册......&#39;;
}
public function stop(){
echo &#39;关闭注册......&#39;;
}
}
//门面类
class Facade{
//网站设置对象
private $webSet;
//博客设置对象
private $blogSet;
//注册功能设置对象
private $registerSet;
public function __construct(){
$this->webSet        = new webSet();
$this->blogSet       = new blogSet();
$this->registerSet   = new registerSet();
}
//设置共开关 - 开
public function turnOn(){
$this->webSet->start();
$this->blogSet->start();
$this->registerSet->start();
}
//设置共开关 - 关
public function turnOff(){
$this->webSet->stop();
$this->blogSet->stop();
$this->registerSet->stop();
}
}
//调用
$Facade = new Facade();
$Facade->turnOn();

The above is the detailed content of The role of appearance mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:hcoder.net. If there is any infringement, please contact admin@php.cn delete