On facade of PHP design pattern

Time:2022-7-30
catalogue
  • objective
  • UML
  • code
  • test

objective

Facade decouples visitors and subsystems by embedding multiple (of course, sometimes only one) interfaces, but also to reduce complexity.

  • The facade will not prohibit you from accessing the subsystem
  • You can (should) provide multiple facades for a subsystem

Therefore, there will be no new in a good facade. If multiple objects are constructed in each method, it is not a facade, but a generator or [Abstract | static | simple] factory [method].

A good facade will not have new, and the constructor parameter is of interface type. If you need to create a new instance, pass in a factory object in the parameter.

UML

code

Facade.php

<?php

namespace DesignPatterns\Structural\Facade;

class Facade
{
    /**
    * @var OsInterface
    *Define operating system interface variables.
    */
    private $os;

    /**
    * @var BiosInterface
    *Define the basic input and output system interface variables.
    */
    private $bios;

    /**
    * @param BiosInterface $bios
    * @param OsInterface $os
    *Pass in the basic I / O system interface object $bios.
    *Pass in the operating system interface object $os.
    */
    public function __construct(BiosInterface $bios, OsInterface $os)
    {
        $this->bios = $bios;
        $this->os = $os;
    }

    /**
    *Build the basic input and output system to execute the startup method.
    */
    public function turnOn()
    {
        $this->bios->execute();
        $this->bios->waitForKeyPress();
        $this->bios->launch($this->os);
    }

    /**
    *Build the system shutdown method.
    */
    public function turnOff()
    {
        $this->os->halt();
        $this->bios->powerDown();
    }
}

OsInterface.php

<?php

namespace DesignPatterns\Structural\Facade;

/**
*Create the OS interface class osinterface.
*/
interface OsInterface
{
    /**
    *Declare shutdown method.
    */
    public function halt();

    /** 
    *Declare the method to get the name and return string format data.
    */
    public function getName(): string;
}

BiosInterface.php

<?php

namespace DesignPatterns\Structural\Facade;

/**
*Create the basic input and output system interface class biosinterface.
*/
interface  BiosInterface
{
    /**
    *Declare the execution method.
    */
    public function execute();

    /**
    *Declare waiting password input method
    */
    public function waitForKeyPress();

    /**
    *Declare the login method.
    */
    public function launch(OsInterface $os);

    /**
    *Declare shutdown method.
    */
    public function powerDown();
}

test

Tests/FacadeTest.php

<?php

namespace DesignPatterns\Structural\Facade\Tests;

use DesignPatterns\Structural\Facade\Facade;
use DesignPatterns\Structural\Facade\OsInterface;
use PHPUnit\Framework\TestCase;

/**
*Create an automated test unit 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);

        //The facade interface is very simple.
        $facade->turnOn();

        //But you can also access the underlying components.
        $this->assertEquals('Linux', $os->getName());
    }
}

The above is the details of the facade of PHP design patterns. For more information about the facade of PHP design patterns, please pay attention to other related articles of developeppaer!