2.7. 外观模式
2.7.1. 目的
外观模式的目的不是为了让你避免阅读烦人的API文档(当然,它有这样的作用),它的主要目的是为了减少耦合并且遵循得墨忒耳定律(Law of Demeter)
Facade通过嵌入多个(当然,有时只有一个)接口来解耦访客与子系统,当然也降低复杂度。
Facade 不会禁止你访问子系统
你可以(应该)为一个子系统提供多个 Facade
因此一个好的 Facade 里面不会有 new
。如果每个方法里都要构造多个对象,那么它就不是 Facade,而是生成器或者[抽象|静态|简单] 工厂 [方法]。
优秀的 Facade 不会有 new
,并且构造函数参数是接口类型的。如果你需要创建一个新实例,则在参数中传入一个工厂对象。
2.7.2. UML 图

2.7.3. 代码
你可以在 GitHub 上找到这些代码
Facade.php
1<?php
2
3declare(strict_types=1);
4
5namespace DesignPatterns\Structural\Facade;
6
7class Facade
8{
9 public function __construct(private Bios $bios, private OperatingSystem $os)
10 {
11 }
12
13 public function turnOn()
14 {
15 $this->bios->execute();
16 $this->bios->waitForKeyPress();
17 $this->bios->launch($this->os);
18 }
19
20 public function turnOff()
21 {
22 $this->os->halt();
23 $this->bios->powerDown();
24 }
25}
OperatingSystem.php
1<?php
2
3declare(strict_types=1);
4
5namespace DesignPatterns\Structural\Facade;
6
7interface OperatingSystem
8{
9 public function halt();
10
11 public function getName(): string;
12}
Bios.php
1<?php
2
3declare(strict_types=1);
4
5namespace DesignPatterns\Structural\Facade;
6
7interface Bios
8{
9 public function execute();
10
11 public function waitForKeyPress();
12
13 public function launch(OperatingSystem $os);
14
15 public function powerDown();
16}
2.7.4. 测试
Tests/FacadeTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace DesignPatterns\Structural\Facade\Tests;
6
7use DesignPatterns\Structural\Facade\Bios;
8use DesignPatterns\Structural\Facade\Facade;
9use DesignPatterns\Structural\Facade\OperatingSystem;
10use PHPUnit\Framework\TestCase;
11
12class FacadeTest extends TestCase
13{
14 public function testComputerOn()
15 {
16 $os = $this->createMock(OperatingSystem::class);
17
18 $os->method('getName')
19 ->will($this->returnValue('Linux'));
20
21 $bios = $this->createMock(Bios::class);
22
23 $bios->method('launch')
24 ->with($os);
25
26 /** @noinspection PhpParamsInspection */
27 $facade = new Facade($bios, $os);
28 $facade->turnOn();
29
30 $this->assertSame('Linux', $os->getName());
31 }
32}