门面模式(Facade) 作者: lovingyu_er 时间: 2020-11-30 00:01:14 分类: 设计模式|思想 ###介绍 少不了繁琐的定义描述,门面模式又叫外观模式,属于一种结构型设计模式,主要的目的是为了避免阅读复杂的API文档。降低耦合并且遵循([Demeter定律](https://darrykinger.com/archives/559.html "Demeter定律")) ** 为子系统中一组接口,提供一个统一高层的接口,使子系统更加容易使用** ### 案例 ``` os = $os; $this->bios = $bios; } /** * 打开 */ public function turnOn() { $this->bios->execute(); $this->bios->waitKeyPress(); $this->bios->launch($this->os); } /** * 关闭 */ public function trunOff() { $this->os->halt(); $this->bios->powerDown(); } } interface OsInterface { public function halt(); public function getName(); } class OsClass implements OsInterface { public function halt() { // TODO: Implement halt() method. } public function getName() { // TODO: Implement getName() method. } } interface BiosInterface { public function execute(); public function waitKeyPress(); public function launch(OsInterface $os); public function powerDown(); } class BiosClass implements BiosInterface { public function execute() { // TODO: Implement execute() method. } public function waitKeyPress() { // TODO: Implement waitKeyPress() method. } public function launch(OsInterface $os) { // TODO: Implement launch() method. } public function powerDown() { // TODO: Implement powerDown() method. } } $bios = new OsClass(); $os = new BiosClass(); $facade_service = new Facade($bios, $os); $facade_service->turnOn(); $facade_service->trunOff(); /** * */ interface cpuinfo { public function freeze(); public function jump(); public function execute(); } class Cpu implements cpuinfo { public function freeze() { } public function jump() { // TODO: Implement jump() method. } public function execute() { // TODO: Implement execute() method. } } interface memoryinfo { public function load(); } class memory implements memoryinfo { public function load() { // TODO: Implement load() method. } } interface HardDriveinfo { public function read(); } class HardDrive implements HardDriveinfo { public function read() { // TODO: Implement read() method. } } //facade //标准的 class Computer { function startComputer() { $cpu = new Cpu(); $cpu->freeze(); $memory = new memory(); $memory->load(); $cpu->jump(); $cpu->execute(); } } //用户 class Client { public static function doSomething() { $computer = new Computer(); $computer->startComputer(); } } ``` ```Client```类,就相当于用户,而```Computer```类就代表了电脑的门面.```Client```只需要调用```static```方法,调用```doSomething```方法就完成了对```Computer```的操作,在Laravel中,命名空间``` Illuminate\Support\Facades```中定义类一系列的```Facade``` 一个门面皆在通过嵌入许多接口,来分离客户端和子系统,这些都是为了降低系统的复杂度的。 1. 门面不会禁止你访问子系统 2. 你可以有多个门面对应一个子系统 标签: none