mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-04 22:17:25 +02:00
PHP7 Facade
This commit is contained in:
@@ -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();
|
||||||
}
|
}
|
||||||
|
@@ -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();
|
||||||
|
@@ -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;
|
||||||
}
|
}
|
||||||
|
@@ -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()
|
||||||
{
|
{
|
||||||
|
/** @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')
|
$bios = $this->getMockBuilder('DesignPatterns\Structural\Facade\BiosInterface')
|
||||||
->setMethods(array('launch', 'execute', 'waitForKeyPress'))
|
->setMethods(['launch', 'execute', 'waitForKeyPress'])
|
||||||
->disableAutoload()
|
->disableAutoload()
|
||||||
->getMock();
|
->getMock();
|
||||||
$operatingSys = $this->getMockBuilder('DesignPatterns\Structural\Facade\OsInterface')
|
|
||||||
->setMethods(array('getName'))
|
|
||||||
->disableAutoload()
|
|
||||||
->getMock();
|
|
||||||
$bios->expects($this->once())
|
$bios->expects($this->once())
|
||||||
->method('launch')
|
->method('launch')
|
||||||
->with($operatingSys);
|
->with($os);
|
||||||
$operatingSys
|
|
||||||
->expects($this->once())
|
|
||||||
->method('getName')
|
|
||||||
->will($this->returnValue('Linux'));
|
|
||||||
|
|
||||||
$facade = new Computer($bios, $operatingSys);
|
$facade = new Facade($bios, $os);
|
||||||
|
|
||||||
return array(array($facade, $operatingSys));
|
// the facade interface is simple
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Computer $facade
|
|
||||||
* @param OsInterface $os
|
|
||||||
* @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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,24 +1,23 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<Diagram>
|
<Diagram>
|
||||||
<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>Methods</Category>
|
||||||
<Category>Constants</Category>
|
<Category>Constants</Category>
|
||||||
<Category>Constructors</Category>
|
<Category>Fields</Category>
|
||||||
<Category>Methods</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 |
Reference in New Issue
Block a user