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

吾爱主题 阅读:147 2021-11-15 16:00:00 评论:0
目录
  • 目的
  • UML
  • 代码
  • 测试

目的

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

  • Facade 不会禁止你访问子系统
  • 你可以(应该)为一个子系统提供多个 Facade

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

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

UML

代码

Facade.php

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

OsInterface.php

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

BiosInterface.php

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

测试

Tests/FacadeTest.php

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

以上就是浅谈PHP设计模式之门面模式Facade的详细内容,更多关于PHP设计模式之门面模式Facade的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/phpyu/p/13681737.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容