返回
热门搜索

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

IT博客 开发工具 eclipse 点击量 281

基本信息 收藏 - 举报 - 海报

详细介绍

目录
目的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博客社区其它相关文章!

没有更多内容。

用户评价(0)

好评度100%
  • 还没有人评论此条信息!
+ 加载更多

联系方式

提示:联系我时,请说明在看到的,谢谢!
  • 联系人:
  • 地  区:
  • 电  话: 共发布信息(2004)条 所在地:未填写
看了又看
加载中
首页 首页 收藏 收藏

电话联系