巅云php学苑
近期车展
最新发布
快速导航

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

开发工具 / eclipse / 2022-04-03 22:14
visits visits 671 collect - report - QRcode

Basicinformation

content

目录
目的UML代码测试

目的

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

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

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

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

UML

代码

Facade.php

<?php

namespace DesignPatternsStructuralFacade;

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

<?php

namespace DesignPatternsStructuralFacade;

/**
* 创建操作系统接口类 OsInterface 。
*/
interface OsInterface
{
    /**
    * 声明关机方法。
    */
    public function halt();

    /** 
    * 声明获取名称方法,返回字符串格式数据。
    */
    public function getName(): string;
}

BiosInterface.php

<?php

namespace DesignPatternsStructuralFacade;

/**
* 创建基础输入输出系统接口类 BiosInterface 。
*/
interface  BiosInterface
{
    /**
    * 声明执行方法。
    */
    public function execute();

    /**
    * 声明等待密码输入方法
    */
    public function waitForKeyPress();

    /**
    * 声明登录方法。
    */
    public function launch(OsInterface $os);

    /**
    * 声明关机方法。
    */
    public function powerDown();
}

测试

Tests/FacadeTest.php

<?php

namespace DesignPatternsStructuralFacadeTests;

use DesignPatternsStructuralFacadeFacade;
use DesignPatternsStructuralFacadeOsInterface;
use PHPUnitFrameworkTestCase;

/**
* 创建自动化测试单元 FacadeTest 。
*/
class FacadeTest extends TestCase
{
    public function testComputerOn()
    {
        /** @var OsInterface|PHPUnit_Framework_MockObject_MockObject $os */
        $os = $this->createMock("DesignPatternsStructuralFacadeOsInterface");

        $os->method("getName")
            ->will($this->returnValue("Linux"));

        $bios = $this->getMockBuilder("DesignPatternsStructuralFacadeBiosInterface")
            ->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的资料请关注IT博客社区其它相关文章!

notmore

Users comments(0)

rate100%
  • notmore
+ more
  • contacts:
  • area:
  • tel: total(2007)twigs area:unknown

联系我们

合作或咨询可通过如下方式:

QQ/微信:123456

网址:www.xxxx.cn

微信公众号:车展门票

关于本站

「车展网www.xxxxxxx.cn」是一个集全国各地品牌经销车商的平台,让您在所在城市,第一时间收到车展的最新消息,让您的试驾、选车、购车、贷车一步到位,更多优惠直达客户,无需东奔西走,带上中意的车回家,让您省钱,更省心。

Copyright 车展网 www.xxxx.cn Reserved渝ICP备xxxxxxxx号

关注我们