PHP7 Facade

This commit is contained in:
Dominik Liebler
2016-09-23 10:19:17 +02:00
parent e6c67c5da5
commit b556436fa2
7 changed files with 698 additions and 413 deletions

View File

@@ -2,30 +2,13 @@
namespace DesignPatterns\Structural\Facade; namespace DesignPatterns\Structural\Facade;
/**
* Interface BiosInterface.
*/
interface BiosInterface interface BiosInterface
{ {
/**
* Execute the BIOS.
*/
public function execute(); public function execute();
/**
* Wait for halt.
*/
public function waitForKeyPress(); public function waitForKeyPress();
/**
* Launches the OS.
*
* @param OsInterface $os
*/
public function launch(OsInterface $os); public function launch(OsInterface $os);
/**
* Power down BIOS.
*/
public function powerDown(); public function powerDown();
} }

View File

@@ -7,17 +7,14 @@ class Facade
/** /**
* @var OsInterface * @var OsInterface
*/ */
protected $os; private $os;
/** /**
* @var BiosInterface * @var BiosInterface
*/ */
protected $bios; private $bios;
/** /**
* This is the perfect time to use a dependency injection container
* to create an instance of this class.
*
* @param BiosInterface $bios * @param BiosInterface $bios
* @param OsInterface $os * @param OsInterface $os
*/ */
@@ -27,9 +24,6 @@ class Facade
$this->os = $os; $this->os = $os;
} }
/**
* Turn on the system.
*/
public function turnOn() public function turnOn()
{ {
$this->bios->execute(); $this->bios->execute();
@@ -37,9 +31,6 @@ class Facade
$this->bios->launch($this->os); $this->bios->launch($this->os);
} }
/**
* Turn off the system.
*/
public function turnOff() public function turnOff()
{ {
$this->os->halt(); $this->os->halt();

View File

@@ -2,13 +2,9 @@
namespace DesignPatterns\Structural\Facade; namespace DesignPatterns\Structural\Facade;
/**
* Interface OsInterface.
*/
interface OsInterface interface OsInterface
{ {
/**
* Halt the OS.
*/
public function halt(); public function halt();
public function getName(): string;
} }

View File

@@ -2,47 +2,34 @@
namespace DesignPatterns\Structural\Facade\Tests; namespace DesignPatterns\Structural\Facade\Tests;
use DesignPatterns\Structural\Facade\Facade as Computer; use DesignPatterns\Structural\Facade\Facade;
use DesignPatterns\Structural\Facade\OsInterface; use DesignPatterns\Structural\Facade\OsInterface;
/**
* FacadeTest shows example of facades.
*/
class FacadeTest extends \PHPUnit_Framework_TestCase class FacadeTest extends \PHPUnit_Framework_TestCase
{ {
public function getComputer() public function testComputerOn()
{ {
$bios = $this->getMockBuilder('DesignPatterns\Structural\Facade\BiosInterface') /** @var OsInterface|\PHPUnit_Framework_MockObject_MockObject $os */
->setMethods(array('launch', 'execute', 'waitForKeyPress')) $os = $this->createMock('DesignPatterns\Structural\Facade\OsInterface');
->disableAutoload()
->getMock(); $os->method('getName')
$operatingSys = $this->getMockBuilder('DesignPatterns\Structural\Facade\OsInterface')
->setMethods(array('getName'))
->disableAutoload()
->getMock();
$bios->expects($this->once())
->method('launch')
->with($operatingSys);
$operatingSys
->expects($this->once())
->method('getName')
->will($this->returnValue('Linux')); ->will($this->returnValue('Linux'));
$facade = new Computer($bios, $operatingSys); $bios = $this->getMockBuilder('DesignPatterns\Structural\Facade\BiosInterface')
->setMethods(['launch', 'execute', 'waitForKeyPress'])
->disableAutoload()
->getMock();
return array(array($facade, $operatingSys)); $bios->expects($this->once())
} ->method('launch')
->with($os);
/** $facade = new Facade($bios, $os);
* @param Computer $facade
* @param OsInterface $os // the facade interface is simple
* @dataProvider getComputer
*/
public function testComputerOn(Computer $facade, OsInterface $os)
{
// interface is simpler :
$facade->turnOn(); $facade->turnOn();
// but I can access to lower component
// but you can also access the underlying components
$this->assertEquals('Linux', $os->getName()); $this->assertEquals('Linux', $os->getName());
} }
} }

View File

@@ -3,21 +3,20 @@
<ID>PHP</ID> <ID>PHP</ID>
<OriginalElement>\DesignPatterns\Structural\Facade\BiosInterface</OriginalElement> <OriginalElement>\DesignPatterns\Structural\Facade\BiosInterface</OriginalElement>
<nodes> <nodes>
<node x="0.0" y="174.0">\DesignPatterns\Structural\Facade\BiosInterface</node> <node x="0.0" y="163.0">\DesignPatterns\Structural\Facade\BiosInterface</node>
<node x="208.0" y="174.0">\DesignPatterns\Structural\Facade\OsInterface</node>
<node x="0.0" y="0.0">\DesignPatterns\Structural\Facade\Facade</node> <node x="0.0" y="0.0">\DesignPatterns\Structural\Facade\Facade</node>
<node x="234.0" y="163.0">\DesignPatterns\Structural\Facade\OsInterface</node>
</nodes> </nodes>
<notes /> <notes />
<edges /> <edges />
<settings layout="Hierarchic Group" zoom="1.0" x="149.0" y="140.5" /> <settings layout="Hierarchic Group" zoom="1.0" x="179.0" y="140.0" />
<SelectedNodes> <SelectedNodes>
<node>\DesignPatterns\Structural\Facade\BiosInterface</node> <node>\DesignPatterns\Structural\Facade\BiosInterface</node>
</SelectedNodes> </SelectedNodes>
<Categories> <Categories>
<Category>Fields</Category>
<Category>Constants</Category>
<Category>Constructors</Category>
<Category>Methods</Category> <Category>Methods</Category>
<Category>Constants</Category>
<Category>Fields</Category>
</Categories> </Categories>
<VISIBILITY>private</VISIBILITY> <VISIBILITY>private</VISIBILITY>
</Diagram> </Diagram>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 37 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 108 KiB