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;
/**
* Interface BiosInterface.
*/
interface BiosInterface
{
/**
* Execute the BIOS.
*/
public function execute();
/**
* Wait for halt.
*/
public function waitForKeyPress();
/**
* Launches the OS.
*
* @param OsInterface $os
*/
public function launch(OsInterface $os);
/**
* Power down BIOS.
*/
public function powerDown();
}

View File

@@ -7,17 +7,14 @@ class Facade
/**
* @var OsInterface
*/
protected $os;
private $os;
/**
* @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 OsInterface $os
*/
@@ -27,9 +24,6 @@ class Facade
$this->os = $os;
}
/**
* Turn on the system.
*/
public function turnOn()
{
$this->bios->execute();
@@ -37,9 +31,6 @@ class Facade
$this->bios->launch($this->os);
}
/**
* Turn off the system.
*/
public function turnOff()
{
$this->os->halt();

View File

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

View File

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

View File

@@ -1,24 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<Diagram>
<ID>PHP</ID>
<OriginalElement>\DesignPatterns\Structural\Facade\BiosInterface</OriginalElement>
<nodes>
<node x="0.0" y="174.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>
</nodes>
<notes />
<edges />
<settings layout="Hierarchic Group" zoom="1.0" x="149.0" y="140.5" />
<SelectedNodes>
<node>\DesignPatterns\Structural\Facade\BiosInterface</node>
</SelectedNodes>
<Categories>
<Category>Fields</Category>
<Category>Constants</Category>
<Category>Constructors</Category>
<Category>Methods</Category>
</Categories>
<VISIBILITY>private</VISIBILITY>
</Diagram>
<?xml version="1.0" encoding="UTF-8"?>
<Diagram>
<ID>PHP</ID>
<OriginalElement>\DesignPatterns\Structural\Facade\BiosInterface</OriginalElement>
<nodes>
<node x="0.0" y="163.0">\DesignPatterns\Structural\Facade\BiosInterface</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>
<notes />
<edges />
<settings layout="Hierarchic Group" zoom="1.0" x="179.0" y="140.0" />
<SelectedNodes>
<node>\DesignPatterns\Structural\Facade\BiosInterface</node>
</SelectedNodes>
<Categories>
<Category>Methods</Category>
<Category>Constants</Category>
<Category>Fields</Category>
</Categories>
<VISIBILITY>private</VISIBILITY>
</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