门面模式(Facade)

目的

门面模式的最初目的并不是为了避免让你阅读复杂的 API 文档,这只是一个附带作用。其实它的本意是为了降低耦合性并且遵循 Demeter 定律。

一个门面旨在通过嵌入许多(但有时只有一个)接口来分离客户端和子系统。当然,也是为了降低复杂度。

  • 门面不会禁止你访问子系统。
  • 你可以(应该)有多个门面对应一个子系统。

这就是为什么一个好的门面里没有 new 的原因。如果每个方法都有多种创建,那并不是一个门面,而是一个构建器 [抽象的|静态的|简单的] 或是一个工厂 [方法] 。

最好的门面是没有 new 的,并且其构造函数带有接口类型提示的参数。 如果你需要创建新的实例,可以使用工厂作为变量。

UML 图

z5RZ820Gdy.png

代码

你也可以在 GitHub 上查看此代码

Facade.php

  1. <?php
  2. namespace DesignPatterns\Structural\Facade;
  3. class Facade
  4. {
  5. /**
  6. * @var OsInterface
  7. * 定义操作系统接口变量。
  8. */
  9. private $os;
  10. /**
  11. * @var BiosInterface
  12. * 定义基础输入输出系统接口变量。
  13. */
  14. private $bios;
  15. /**
  16. * @param BiosInterface $bios
  17. * @param OsInterface $os
  18. * 传入基础输入输出系统接口对象 $bios 。
  19. * 传入操作系统接口对象 $os 。
  20. */
  21. public function __construct(BiosInterface $bios, OsInterface $os)
  22. {
  23. $this->bios = $bios;
  24. $this->os = $os;
  25. }
  26. /**
  27. * 构建基础输入输出系统执行启动方法。
  28. */
  29. public function turnOn()
  30. {
  31. $this->bios->execute();
  32. $this->bios->waitForKeyPress();
  33. $this->bios->launch($this->os);
  34. }
  35. /**
  36. * 构建系统关闭方法。
  37. */
  38. public function turnOff()
  39. {
  40. $this->os->halt();
  41. $this->bios->powerDown();
  42. }
  43. }

OsInterface.php

  1. <?php
  2. namespace DesignPatterns\Structural\Facade;
  3. /**
  4. * 创建操作系统接口类 OsInterface 。
  5. */
  6. interface OsInterface
  7. {
  8. /**
  9. * 声明关机方法。
  10. */
  11. public function halt();
  12. /**
  13. * 声明获取名称方法,返回字符串格式数据。
  14. */
  15. public function getName(): string;
  16. }

BiosInterface.php

  1. <?php
  2. namespace DesignPatterns\Structural\Facade;
  3. /**
  4. * 创建基础输入输出系统接口类 BiosInterface 。
  5. */
  6. interface BiosInterface
  7. {
  8. /**
  9. * 声明执行方法。
  10. */
  11. public function execute();
  12. /**
  13. * 声明等待密码输入方法
  14. */
  15. public function waitForKeyPress();
  16. /**
  17. * 声明登录方法。
  18. */
  19. public function launch(OsInterface $os);
  20. /**
  21. * 声明关机方法。
  22. */
  23. public function powerDown();
  24. }

测试

Tests/FacadeTest.php

  1. <?php
  2. namespace DesignPatterns\Structural\Facade\Tests;
  3. use DesignPatterns\Structural\Facade\Facade;
  4. use DesignPatterns\Structural\Facade\OsInterface;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * 创建自动化测试单元 FacadeTest 。
  8. */
  9. class FacadeTest extends TestCase
  10. {
  11. public function testComputerOn()
  12. {
  13. /** @var OsInterface|\PHPUnit_Framework_MockObject_MockObject $os */
  14. $os = $this->createMock('DesignPatterns\Structural\Facade\OsInterface');
  15. $os->method('getName')
  16. ->will($this->returnValue('Linux'));
  17. $bios = $this->getMockBuilder('DesignPatterns\Structural\Facade\BiosInterface')
  18. ->setMethods(['launch', 'execute', 'waitForKeyPress'])
  19. ->disableAutoload()
  20. ->getMock();
  21. $bios->expects($this->once())
  22. ->method('launch')
  23. ->with($os);
  24. $facade = new Facade($bios, $os);
  25. // 门面接口很简单。
  26. $facade->turnOn();
  27. // 但你也可以访问底层组件。
  28. $this->assertEquals('Linux', $os->getName());
  29. }
  30. }