The role of appearance mode

藏色散人
Release: 2023-04-05 20:48:01
forward
6809 people have browsed it

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();
Copy after login

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!

Related labels:
source:hcoder.net
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!