mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-10 00:46:38 +02:00
start a restructure
This commit is contained in:
31
Structural/Facade/BiosInterface.php
Normal file
31
Structural/Facade/BiosInterface.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Facade;
|
||||
|
||||
/**
|
||||
* Class 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();
|
||||
}
|
52
Structural/Facade/Facade.php
Normal file
52
Structural/Facade/Facade.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Facade;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
class Facade
|
||||
{
|
||||
/**
|
||||
* @var OsInterface
|
||||
*/
|
||||
protected $os;
|
||||
|
||||
/**
|
||||
* @var BiosInterface
|
||||
*/
|
||||
protected $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
|
||||
*/
|
||||
public function __construct(BiosInterface $bios, OsInterface $os)
|
||||
{
|
||||
$this->bios = $bios;
|
||||
$this->os = $os;
|
||||
}
|
||||
|
||||
/**
|
||||
* turn on the system
|
||||
*/
|
||||
public function turnOn()
|
||||
{
|
||||
$this->bios->execute();
|
||||
$this->bios->waitForKeyPress();
|
||||
$this->bios->launch($this->os);
|
||||
}
|
||||
|
||||
/**
|
||||
* turn off the system
|
||||
*/
|
||||
public function turnOff()
|
||||
{
|
||||
$this->os->halt();
|
||||
$this->bios->powerDown();
|
||||
}
|
||||
}
|
14
Structural/Facade/OsInterface.php
Normal file
14
Structural/Facade/OsInterface.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Facade;
|
||||
|
||||
/**
|
||||
* Class OsInterface
|
||||
*/
|
||||
interface OsInterface
|
||||
{
|
||||
/**
|
||||
* halt the OS
|
||||
*/
|
||||
public function halt();
|
||||
}
|
17
Structural/Facade/README.md
Normal file
17
Structural/Facade/README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Facade
|
||||
|
||||
## Purpose
|
||||
|
||||
The primary goal of a Facade Pattern is not to avoid you to read the manual of a complex API. It's only a side-effect.
|
||||
The first goal is to reduce coupling and follow the Law of Demeter.
|
||||
|
||||
A Facade is meant to decouple a client and a sub-system by embedding many (but sometimes just one) interface, and of course to reduce complexity.
|
||||
|
||||
* A facade does not forbid you the access to the sub-system
|
||||
* You can (you should) have multiple facades for one sub-system
|
||||
|
||||
That's why a good facade has no `new` in it. If there are multiple creations for each method, it is not a Facade, it's a Builder or a
|
||||
[Abstract|Static|Simple] Factory [Method].
|
||||
|
||||
The best facade has no `new` and a constructor with interface-type-hinted parameters.
|
||||
If you need creation of new instances, use a Factory as argument.
|
45
Structural/Facade/Test/FacadeTest.php
Normal file
45
Structural/Facade/Test/FacadeTest.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Tests\Facade;
|
||||
|
||||
use DesignPatterns\Facade\Facade as Computer;
|
||||
|
||||
/**
|
||||
* FacadeTest shows example of facades
|
||||
*/
|
||||
class FacadeTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function getComputer()
|
||||
{
|
||||
$bios = $this->getMockBuilder('DesignPatterns\Facade\BiosInterface')
|
||||
->setMethods(array('launch', 'execute', 'waitForKeyPress'))
|
||||
->disableAutoload()
|
||||
->getMock();
|
||||
$operatingSys = $this->getMockBuilder('DesignPatterns\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'));
|
||||
|
||||
$facade = new Computer($bios, $operatingSys);
|
||||
return array(array($facade, $operatingSys));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getComputer
|
||||
*/
|
||||
public function testComputerOn(Computer $facade, $os)
|
||||
{
|
||||
// interface is simpler :
|
||||
$facade->turnOn();
|
||||
// but I can access to lower component
|
||||
$this->assertEquals('Linux', $os->getName());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user