当前位置:首页 > PHP教程 > php高级应用 > 列表

浅谈PHP设计模式之门面模式Facade

发布:smiling 来源: PHP粉丝网  添加日期:2022-04-27 14:53:39 浏览: 评论:0 

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

目的

Facade通过嵌入多个(当然,有时只有一个)接口来解耦访客与子系统,同时也为了降低复杂度。

Facade 不会禁止你访问子系统

你可以(应该)为一个子系统提供多个 Facade

因此一个好的 Facade 里面不会有 new 。如果每个方法里都要构造多个对象,那么它就不是 Facade,而是生成器或者[抽象|静态|简单] 工厂 [方法]。

优秀的 Facade 不会有 new,并且构造函数参数是接口类型的。如果你需要创建一个新实例,则在参数中传入一个工厂对象。

UML

浅谈PHP设计模式之门面模式Facade

代码

Facade.php

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

OsInterface.php

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

BiosInterface.php

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

测试

Tests/FacadeTest.php

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

Tags: PHP设计模式 Facade

分享到: